This commit is contained in:
Anatoly Prohacky 2023-01-18 17:17:36 +10:00
parent 216e805cd0
commit 11072693c7
3 changed files with 135 additions and 0 deletions

5
go.mod Normal file
View File

@ -0,0 +1,5 @@
module gitstore.ru/tolikproh/mchstest
go 1.19
require github.com/google/uuid v1.3.0

2
go.sum Normal file
View File

@ -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=

128
main.go Normal file
View File

@ -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
}