From 11072693c776f03e9de00e0e776fec8815480ce2 Mon Sep 17 00:00:00 2001 From: Anatoly Prohacky Date: Wed, 18 Jan 2023 17:17:36 +1000 Subject: [PATCH] Begin --- go.mod | 5 +++ go.sum | 2 + main.go | 128 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 135 insertions(+) create mode 100644 go.mod create mode 100644 go.sum create mode 100644 main.go diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..0a51858 --- /dev/null +++ b/go.mod @@ -0,0 +1,5 @@ +module gitstore.ru/tolikproh/mchstest + +go 1.19 + +require github.com/google/uuid v1.3.0 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..3dfe1c9 --- /dev/null +++ b/go.sum @@ -0,0 +1,2 @@ +github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I= +github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= diff --git a/main.go b/main.go new file mode 100644 index 0000000..5b56955 --- /dev/null +++ b/main.go @@ -0,0 +1,128 @@ +package main + +import ( + "bytes" + "encoding/json" + "fmt" + "io/ioutil" + "net/http" + + "github.com/google/uuid" +) + +const ( + url string = "https://digital.mchs.gov.ru/testing/api/instance/" + uidMchs string = "b400430a-07ce-4012-89ec-a2ddf0a2a208" +) + +type ResMchsTestType struct { + Id uuid.UUID `json:"id"` + CurrentQuestion CQType `json:"current_question"` + CurrentAnswers []CAType `json:"current_answers"` + QuestionCount int `json:"questions_count"` + QuestionNumber int `json:"question_number"` + QuestionStatus []int `json:"questions_statuses"` + ValidAnswer uuid.UUID `json:"valid_answer"` + QuestionPassed int `json:"questions_passed"` + ValidAnswers []uuid.UUID `json:"valid_answers"` +} + +type CQType struct { + Id uuid.UUID `json:"id"` + Content string `json:"content"` + ResourcesPath []string `json:"resources_path"` + Types string `json:"type"` + IsAdd bool `json:"id_additional"` +} + +type CAType struct { + Id uuid.UUID `json:"id"` + Title string `json:"title"` + ResourcesPath []string `json:"resources_path"` +} + +// func ResMchsTestToJSON(data ResMchsTestType) string { +// req, _ := json.Marshal(data) +// return string(req) +// } + +func UnmarshalJSONToType(in []byte) *ResMchsTestType { + var data ResMchsTestType + _ = json.Unmarshal(in, &data) + return &data +} + +type ReqAnswer struct { + Answer uuid.UUID `json:"answer"` +} + +type ReqNull struct { + Answers string `json:"answers"` +} + +func main() { + var rN ReqNull + var rA ReqAnswer + body, _ := ReqestToSiteJSON(rN) + fmt.Print("[") + fmt.Printf("%s", body) + fmt.Print(",\n") + for i := 0; i < 520; i++ { + d := UnmarshalJSONToType(body) + rA.Answer = d.ValidAnswer + body, _ = ReqestToSiteJSON(rA) + fmt.Printf("%s", body) + fmt.Print(",\n") + + } + fmt.Println("]") + +} + +func ReqestToSiteJSON(req any) ([]byte, error) { + + reqBodyBytes, err := json.Marshal(req) + if err != nil { + return nil, err + } + + urlr := url + uidMchs + resp, err := http.Post( + urlr, + "application/json", bytes.NewBuffer(reqBodyBytes)) + if err != nil { + return nil, err + } + defer resp.Body.Close() + + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return nil, err + } + + return body, nil +} + +func ReqestToSite(req any) (*ResMchsTestType, error) { + + reqBodyBytes, err := json.Marshal(req) + if err != nil { + return nil, err + } + + urlr := url + uidMchs + resp, err := http.Post( + urlr, + "application/json", bytes.NewBuffer(reqBodyBytes)) + if err != nil { + return nil, err + } + defer resp.Body.Close() + + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return nil, err + } + + return UnmarshalJSONToType(body), nil +}