From c13614bf6c14134098bb4b48839a4f0bb9ce2e67 Mon Sep 17 00:00:00 2001 From: Mikhail Knyazhev Date: Fri, 22 Jan 2021 03:10:22 +0300 Subject: [PATCH] Implementation of basic mechanics --- README.md | 143 +++++++++++- account.go | 8 +- callback.go | 16 +- client.go | 99 +++++--- client_utils.go | 125 ++++++++++ document.go | 73 ++++++ document_easyjson.go | 518 +++++++++++++++++++++++++++++++++++++++++ error.go | 2 + example/create/main.go | 69 ++++++ example/list/main.go | 74 ++++++ example/main.go | 38 --- project.go | 99 ++++---- project_easyjson.go | 438 ++++++++++++---------------------- 13 files changed, 1270 insertions(+), 432 deletions(-) create mode 100644 client_utils.go create mode 100644 document.go create mode 100644 document_easyjson.go create mode 100644 example/create/main.go create mode 100644 example/list/main.go delete mode 100644 example/main.go diff --git a/README.md b/README.md index 68ee2cd..9e7e822 100644 --- a/README.md +++ b/README.md @@ -6,8 +6,14 @@ Unofficial golang client for smartcat.com [![Go Report Card](https://goreportcard.com/badge/github.com/markus621/go-smartcat-client)](https://goreportcard.com/report/github.com/markus621/go-smartcat-client) [![Build Status](https://travis-ci.com/markus621/go-smartcat-client.svg?branch=master)](https://travis-ci.com/markus621/go-smartcat-client) +## Official documentation + +[Swagger 2.0](https://smartcat.com/api/swagger/docs/v1) + [Swagger UI](https://smartcat.com/api/methods/) + # Example +### _Create project and upload documents_ + ```go package main @@ -18,6 +24,7 @@ import ( cli "github.com/markus621/go-smartcat-client" ) +//nolint: errcheck func main() { conf := cli.Config{ @@ -29,16 +36,130 @@ func main() { client := cli.NewClient(conf) client.Debug(true, os.Stdout) - _, _ = client.GetAccount() - _, _ = client.GetAccountMTEngines() - _, _ = client.SetCallback(cli.Callback{ - URL: "https://demo.example/callback", - AdditionalHeaders: []cli.AdditionalHeader{ - {Name: "x-header", Value: "demo"}, - }, - }) - _, _ = client.GetCallback() - _ = client.DelCallback() - _, _ = client.GetCallbackLastErrors(10) + project, err := client.CreateProject(cli.NewProject{ + Name: "TS-10", + Description: "Перевод с русского на английский", + SourceLanguage: "en-US", + TargetLanguages: []string{"ru", "id"}, + AssignToVendor: false, + UseMT: false, + Pretranslate: false, + UseTranslationMemory: false, + AutoPropagateRepetitions: false, + WorkflowStages: []string{"translation"}, + IsForTesting: false, + }) + if err != nil { + panic(err) + } + + form := cli.NewForm() + form.AddFile("base1.json", []byte(`{"main":"hello world"}`)) + form.AddFile("base2.json", []byte(`{"main2":"hello my world"}`)) + + docs, err := client.CreateDocument(project.ID, form) + if err != nil { + panic(err) + } + + fmt.Println("Create new docs") + for _, doc := range docs { + fmt.Println(doc.Status, doc.Name) + for _, ws := range doc.WorkflowStages { + fmt.Println(ws.Status, ws.Progress) + } + } + + fmt.Println("Get status all docs") + project, err = client.GetProject(project.ID) + if err != nil { + panic(err) + } + + for _, doc := range project.Documents { + fmt.Println(doc.Status, doc.Name) + for _, ws := range doc.WorkflowStages { + fmt.Println(ws.Status, ws.Progress) + } + } + } + +``` + +### _Get all documents and export_ +```go +import ( + "archive/zip" + "bytes" + "fmt" + "io/ioutil" + "os" + "time" + + cli "github.com/markus621/go-smartcat-client" +) + +//nolint: errcheck +func main() { + + conf := cli.Config{ + AccountID: os.Getenv(`SMARTCAT_ACCOUNT_ID`), + AuthKey: os.Getenv(`SMARTCAT_AUTH_KEY`), + URL: cli.HostURL, + } + + client := cli.NewClient(conf) + client.Debug(true, os.Stdout) + + list, err := client.ListProject() + if err != nil { + panic(err) + } + + ids := make([]string, 0) + for _, project := range list { + fmt.Println(project.ID, project.Name, project.Status) + for _, doc := range project.Documents { + fmt.Println(doc.ID, doc.Name, doc.Status, doc.SourceLanguage, doc.TargetLanguage, doc.WorkflowStages[0].Progress) + + if doc.WorkflowStages[0].Progress == 100 { + ids = append(ids, doc.ID) + } + } + } + + task, err := client.ExportDocument(ids) + if err != nil { + panic(err) + } + + <-time.After(3 * time.Second) + + data, err := client.ExportDocumentByTaskID(task.ID) + if err != nil { + panic(err) + } + + // as text file if one document + if len(ids) == 1 { + fmt.Println(string(data)) + os.Exit(0) + } + + // as zip file if many documents + z, err := zip.NewReader(bytes.NewReader(data), int64(len(data))) + if err != nil { + panic(err) + } + for _, f := range z.File { + reader, err := f.Open() + if err != nil { + panic(err) + } + b, err := ioutil.ReadAll(reader) + fmt.Println(f.Name, string(b), err) + } +} + ``` \ No newline at end of file diff --git a/account.go b/account.go index 4f51ab9..4f02449 100644 --- a/account.go +++ b/account.go @@ -30,13 +30,13 @@ type ( ) //GetAccount Receiving the account details -func (v *Client) GetAccount() (out Account, err error) { - _, err = v.call(http.MethodGet, uriAccount, nil, &out) +func (c *Client) GetAccount() (out Account, err error) { + _, err = c.json(http.MethodGet, uriAccount, nil, &out) return } //GetAccountMTEngines Receiving MT engines available for the account -func (v *Client) GetAccountMTEngines() (out AccountMTEngines, err error) { - _, err = v.call(http.MethodGet, uriAccountMTEngines, nil, &out) +func (c *Client) GetAccountMTEngines() (out AccountMTEngines, err error) { + _, err = c.json(http.MethodGet, uriAccountMTEngines, nil, &out) return } diff --git a/callback.go b/callback.go index ddd3a60..f77725e 100644 --- a/callback.go +++ b/callback.go @@ -38,25 +38,25 @@ type ( ) //DelCallback Resetting the configuration of notifications reception -func (v *Client) DelCallback() (err error) { - _, err = v.call(http.MethodDelete, uriCallback, nil, nil) +func (c *Client) DelCallback() (err error) { + _, err = c.json(http.MethodDelete, uriCallback, nil, nil) return } //GetCallback Reading configurations of notifications reception of the account -func (v *Client) GetCallback() (out Callback, err error) { - _, err = v.call(http.MethodGet, uriCallback, nil, &out) +func (c *Client) GetCallback() (out Callback, err error) { + _, err = c.json(http.MethodGet, uriCallback, nil, &out) return } //SetCallback Setting configurations of notifications reception of the account -func (v *Client) SetCallback(in Callback) (out Callback, err error) { - _, err = v.call(http.MethodPost, uriCallback, &in, &out) +func (c *Client) SetCallback(in Callback) (out Callback, err error) { + _, err = c.json(http.MethodPost, uriCallback, &in, &out) return } //GetCallbackLastErrors Reading the recent sending errors -func (v *Client) GetCallbackLastErrors(limit int) (out LastErrors, err error) { - _, err = v.call(http.MethodGet, fmt.Sprintf("%s?limit=%d", uriCallbackLastErrors, limit), nil, &out) +func (c *Client) GetCallbackLastErrors(limit int) (out LastErrors, err error) { + _, err = c.json(http.MethodGet, fmt.Sprintf("%s?limit=%d", uriCallbackLastErrors, limit), nil, &out) return } diff --git a/client.go b/client.go index 4644a30..896c0bc 100644 --- a/client.go +++ b/client.go @@ -14,7 +14,7 @@ import ( ) const ( - userAgent = "go-smartcat-client" + userAgent = `go-smartcat-client` ) type ( @@ -54,83 +54,108 @@ func NewCustomClient(c Config, cli *http.Client) *Client { } //Debug enable logging of responses -func (v *Client) Debug(is bool, w io.Writer) { - v.debug, v.writer = is, w +func (c *Client) Debug(is bool, w io.Writer) { + c.debug, c.writer = is, w } -func (v *Client) call(method, path string, req json.Marshaler, resp json.Unmarshaler) (int, error) { - var ( - body []byte - err error - ) +func (c *Client) raw(method, path string, req []byte) (code int, body []byte, err error) { + code, body, err = c.call(method, path, body, `application/json`) + return +} +func (c *Client) json(method, path string, req json.Marshaler, resp json.Unmarshaler) (code int, err error) { + var body []byte if req != nil { body, err = req.MarshalJSON() if err != nil { return 0, errors.Wrap(err, "marshal request") } } + code, body, err = c.call(method, path, body, `application/json`) + if err != nil { + return + } + err = json.Unmarshal(body, &resp) + return +} + +func (c *Client) form(method, path string, fields *Form, resp json.Unmarshaler) (code int, err error) { + if fields == nil { + err = ErrEmptyRequest + return + } + var body []byte + code, body, err = c.call(method, path, fields.Bytes(), fields.GetContentType()) + if err != nil { + return + } + err = json.Unmarshal(body, &resp) + return +} - creq, err := http.NewRequest(method, v.conf.URL+path, bytes.NewReader(body)) +func (c *Client) call(method, path string, body []byte, contentType string) (int, []byte, error) { + creq, err := http.NewRequest(method, c.conf.URL+path, bytes.NewReader(body)) + c.requestDebug(method, path, body, err) if err != nil { - return 0, errors.Wrap(err, "create request") + return 0, nil, errors.Wrap(err, "create request") } creq.Header.Set("User-Agent", userAgent) creq.Header.Set("Connection", "keep-alive") creq.Header.Set("Accept", "*/*") - creq.Header.Set("Content-Type", "application/json") - creq.Header.Set("Authorization", v.conf.AuthToken()) + creq.Header.Set("Content-Type", contentType) + creq.Header.Set("Authorization", c.conf.AuthToken()) - cresp, err := v.cli.Do(creq) + cresp, err := c.cli.Do(creq) if err != nil { - return 0, errors.Wrap(err, "make request") + return 0, nil, errors.Wrap(err, "make request") } code := cresp.StatusCode switch code { case 200: - body, err = v.readBody(cresp.Body, resp) + body, err = c.read(cresp.Body) case 204: - case 404: + case 404, 415: body, err = nil, errors.New(cresp.Status) - case 400, 401, 403, 202: - msg := ErrorResponse{} - body, err = v.readBody(cresp.Body, &msg) - if err != nil { - err = ErrorResponse{Message: string(body)} - } default: - var raw json.RawMessage - body, err = v.readBody(cresp.Body, &raw) + msg := ErrorResponse{} + body, err = c.read(cresp.Body) if err == nil { - err = ErrUnknown + if err = json.Unmarshal(body, &msg); err != nil { + err = ErrorResponse{Message: string(body)} + } else { + err = msg + } } } - v.writeDebug(code, method, path, body, err) + c.responseDebug(code, err, body) switch err { case nil: - return code, nil + return code, body, nil case io.EOF: - return code, errors.New(cresp.Status) + return code, body, errors.New(cresp.Status) default: - return code, errors.Wrap(err, "unmarshal response") + return code, body, err } } -func (v *Client) readBody(rc io.ReadCloser, resp json.Unmarshaler) (b []byte, err error) { +func (c *Client) read(rc io.ReadCloser) (b []byte, err error) { + defer rc.Close() b, err = ioutil.ReadAll(rc) - if err != nil || resp == nil { - return - } - err = resp.UnmarshalJSON(b) return } -func (v *Client) writeDebug(code int, method, path string, body []byte, err error) { - if v.debug { - fmt.Fprintf(v.writer, "[%d] %s:%s err: %+v raw:%s \n", code, method, path, err, body) +func (c *Client) requestDebug(method, path string, body []byte, err error) { + if c.debug { + fmt.Fprintf(c.writer, "REQ: %s:%s err: %v raw:%s \n", method, path, err, body) + } +} + +func (c *Client) responseDebug(code int, err error, body []byte) { + if c.debug { + fmt.Fprintf(c.writer, "RES: [%d] err: %v raw:%s \n", code, err, body) } } diff --git a/client_utils.go b/client_utils.go new file mode 100644 index 0000000..d10277f --- /dev/null +++ b/client_utils.go @@ -0,0 +1,125 @@ +package smartcatclient + +import ( + "bytes" + "encoding/json" + "fmt" + "io/ioutil" + "math/rand" + "os" + "strings" + "time" +) + +func init() { + rand.Seed(time.Now().UnixNano()) +} + +var letterRunes = []rune("abcdefghijklmnopqrstuvwxyz0123456789") + +func randomString(n int) string { + b := make([]rune, n) + for i := range b { + b[i] = letterRunes[rand.Intn(len(letterRunes))] + } + return string(b) +} + +//================================================================================== + +var quoteEscaper = strings.NewReplacer("\\", "\\\\", `"`, "\\\"") + +func escapeQuotes(s string) string { + return quoteEscaper.Replace(s) +} + +//================================================================================== + +type ( + //Form fields map for upload form + Form struct { + data *bytes.Buffer + boundary string + fileID int + } +) + +//NewForm init new form fields +func NewForm() *Form { + return &Form{ + data: &bytes.Buffer{}, + boundary: randomString(23), + fileID: 0, + } +} + +//GetContentType getting content type for form +func (f *Form) GetContentType() string { + return fmt.Sprintf(`multipart/form-data; boundary="%s"`, f.boundary) +} + +func (f *Form) writeHeader(name string, length int, isfile bool) { + f.data.WriteString("--" + f.boundary + "\r\n") + if isfile { + f.fileID++ + f.data.WriteString("Content-Type: application/octet-stream\r\n") + f.data.WriteString(fmt.Sprintf("Content-Disposition: form-data; name=\"file_%d\"; filename=\"%s\"\r\n", f.fileID, escapeQuotes(name))) + } else { + f.data.WriteString("Content-Type: application/json\r\n") + f.data.WriteString(fmt.Sprintf("Content-Disposition: form-data; name=\"%s\"\r\n", escapeQuotes(name))) + } + f.data.WriteString(fmt.Sprintf("Content-Length: %d\r\n\r\n", length)) +} + +//Add field to form +func (f *Form) Add(name string, value []byte) { + f.writeHeader(name, len(value), false) + f.data.Write(value) + f.data.WriteString("\r\n") +} + +//AddJSON convert object to json and add as field +func (f *Form) AddJSON(name string, value interface{}) error { + b, err := json.Marshal(value) + if err != nil { + return err + } + f.Add(name, b) + return nil +} + +//AddFile add text as file +func (f *Form) AddFile(name string, value []byte) { + f.writeHeader(name, len(value), true) + f.data.Write(value) + f.data.WriteString("\r\n") +} + +//LoadFile read file to form +func (f *Form) LoadFile(path string) error { + file, err := os.Open(path) + if err != nil { + return err + } + value, err := ioutil.ReadAll(file) + if err != nil { + return err + } + info, err := file.Stat() + if err != nil { + return err + } + if err = file.Close(); err != nil { + return err + } + f.AddFile(info.Name(), value) + return nil +} + +//Bytes close form, read, and reset +func (f *Form) Bytes() (r []byte) { + f.data.WriteString(fmt.Sprintf("--%s--\r\n", f.boundary)) + r, f.fileID = f.data.Bytes(), 0 + f.data.Reset() + return +} diff --git a/document.go b/document.go new file mode 100644 index 0000000..b2ab52e --- /dev/null +++ b/document.go @@ -0,0 +1,73 @@ +package smartcatclient + +import ( + "fmt" + "net/http" + "net/url" + "time" +) + +//go:generate easyjson + +const ( + uriDocumentCreate = "/api/integration/v1/project/document" + uriDocumentExport = "/api/integration/v1/document/export" + uriDocumentTaskExport = "/api/integration/v1/document/export/%s" +) + +//easyjson:json +type ( + //DocumentWorkflowStage model + DocumentWorkflowStage struct { + Progress float64 `json:"progress"` + WordsTranslated uint64 `json:"wordsTranslated"` + UnassignedWordsCount uint64 `json:"unassignedWordsCount"` + Status string `json:"status"` + Executives []Executive `json:"executives"` + } + //Document model + Document struct { + ID string `json:"id"` + Name string `json:"name"` + CreationDate time.Time `json:"creationDate"` + Deadline time.Time `json:"deadline"` + SourceLanguage string `json:"sourceLanguage"` + DisassemblingStatus string `json:"documentDisassemblingStatus"` + TargetLanguage string `json:"targetLanguage"` + Status string `json:"status"` + WordsCount uint64 `json:"wordsCount"` + StatusModificationDate time.Time `json:"statusModificationDate"` + PretranslateCompleted bool `json:"pretranslateCompleted"` + WorkflowStages []DocumentWorkflowStage `json:"workflowStages"` + ExternalID string `json:"externalId"` + MetaInfo string `json:"metaInfo"` + PlaceholdersAreEnabled bool `json:"placeholdersAreEnabled"` + } + //DocumentList model + DocumentList []Document + //ExportTask model + ExportTask struct { + ID string `json:"id"` + DocumentIds []string `json:"documentIds"` + } +) + +//CreateDocument Create new document in project +func (c *Client) CreateDocument(projectID string, form *Form) (out DocumentList, err error) { + _, err = c.form(http.MethodPost, uriDocumentCreate+"?projectId="+projectID, form, &out) + return +} + +//ExportDocument Creating a document export task +func (c *Client) ExportDocument(ids []string) (out ExportTask, err error) { + v := url.Values{"documentIds": ids} + u := url.URL{Path: uriDocumentExport, RawQuery: v.Encode()} + _, err = c.json(http.MethodPost, u.String(), nil, &out) + return +} + +//ExportDocumentByTaskID Downloading files by task number for export +func (c *Client) ExportDocumentByTaskID(id string) (out []byte, err error) { + _, out, err = c.raw(http.MethodGet, fmt.Sprintf(uriDocumentTaskExport, id), nil) + return +} diff --git a/document_easyjson.go b/document_easyjson.go new file mode 100644 index 0000000..fc522e8 --- /dev/null +++ b/document_easyjson.go @@ -0,0 +1,518 @@ +// Code generated by easyjson for marshaling/unmarshaling. DO NOT EDIT. + +package smartcatclient + +import ( + json "encoding/json" + easyjson "github.com/mailru/easyjson" + jlexer "github.com/mailru/easyjson/jlexer" + jwriter "github.com/mailru/easyjson/jwriter" +) + +// suppress unused package warning +var ( + _ *json.RawMessage + _ *jlexer.Lexer + _ *jwriter.Writer + _ easyjson.Marshaler +) + +func easyjson18605acbDecodeGithubComMarkus621GoSmartcatClient(in *jlexer.Lexer, out *ExportTask) { + isTopLevel := in.IsStart() + if in.IsNull() { + if isTopLevel { + in.Consumed() + } + in.Skip() + return + } + in.Delim('{') + for !in.IsDelim('}') { + key := in.UnsafeFieldName(false) + in.WantColon() + if in.IsNull() { + in.Skip() + in.WantComma() + continue + } + switch key { + case "id": + out.ID = string(in.String()) + case "documentIds": + if in.IsNull() { + in.Skip() + out.DocumentIds = nil + } else { + in.Delim('[') + if out.DocumentIds == nil { + if !in.IsDelim(']') { + out.DocumentIds = make([]string, 0, 4) + } else { + out.DocumentIds = []string{} + } + } else { + out.DocumentIds = (out.DocumentIds)[:0] + } + for !in.IsDelim(']') { + var v1 string + v1 = string(in.String()) + out.DocumentIds = append(out.DocumentIds, v1) + in.WantComma() + } + in.Delim(']') + } + default: + in.SkipRecursive() + } + in.WantComma() + } + in.Delim('}') + if isTopLevel { + in.Consumed() + } +} +func easyjson18605acbEncodeGithubComMarkus621GoSmartcatClient(out *jwriter.Writer, in ExportTask) { + out.RawByte('{') + first := true + _ = first + { + const prefix string = ",\"id\":" + out.RawString(prefix[1:]) + out.String(string(in.ID)) + } + { + const prefix string = ",\"documentIds\":" + out.RawString(prefix) + if in.DocumentIds == nil && (out.Flags&jwriter.NilSliceAsEmpty) == 0 { + out.RawString("null") + } else { + out.RawByte('[') + for v2, v3 := range in.DocumentIds { + if v2 > 0 { + out.RawByte(',') + } + out.String(string(v3)) + } + out.RawByte(']') + } + } + out.RawByte('}') +} + +// MarshalJSON supports json.Marshaler interface +func (v ExportTask) MarshalJSON() ([]byte, error) { + w := jwriter.Writer{} + easyjson18605acbEncodeGithubComMarkus621GoSmartcatClient(&w, v) + return w.Buffer.BuildBytes(), w.Error +} + +// MarshalEasyJSON supports easyjson.Marshaler interface +func (v ExportTask) MarshalEasyJSON(w *jwriter.Writer) { + easyjson18605acbEncodeGithubComMarkus621GoSmartcatClient(w, v) +} + +// UnmarshalJSON supports json.Unmarshaler interface +func (v *ExportTask) UnmarshalJSON(data []byte) error { + r := jlexer.Lexer{Data: data} + easyjson18605acbDecodeGithubComMarkus621GoSmartcatClient(&r, v) + return r.Error() +} + +// UnmarshalEasyJSON supports easyjson.Unmarshaler interface +func (v *ExportTask) UnmarshalEasyJSON(l *jlexer.Lexer) { + easyjson18605acbDecodeGithubComMarkus621GoSmartcatClient(l, v) +} +func easyjson18605acbDecodeGithubComMarkus621GoSmartcatClient1(in *jlexer.Lexer, out *DocumentWorkflowStage) { + isTopLevel := in.IsStart() + if in.IsNull() { + if isTopLevel { + in.Consumed() + } + in.Skip() + return + } + in.Delim('{') + for !in.IsDelim('}') { + key := in.UnsafeFieldName(false) + in.WantColon() + if in.IsNull() { + in.Skip() + in.WantComma() + continue + } + switch key { + case "progress": + out.Progress = float64(in.Float64()) + case "wordsTranslated": + out.WordsTranslated = uint64(in.Uint64()) + case "unassignedWordsCount": + out.UnassignedWordsCount = uint64(in.Uint64()) + case "status": + out.Status = string(in.String()) + case "executives": + if in.IsNull() { + in.Skip() + out.Executives = nil + } else { + in.Delim('[') + if out.Executives == nil { + if !in.IsDelim(']') { + out.Executives = make([]Executive, 0, 1) + } else { + out.Executives = []Executive{} + } + } else { + out.Executives = (out.Executives)[:0] + } + for !in.IsDelim(']') { + var v4 Executive + (v4).UnmarshalEasyJSON(in) + out.Executives = append(out.Executives, v4) + in.WantComma() + } + in.Delim(']') + } + default: + in.SkipRecursive() + } + in.WantComma() + } + in.Delim('}') + if isTopLevel { + in.Consumed() + } +} +func easyjson18605acbEncodeGithubComMarkus621GoSmartcatClient1(out *jwriter.Writer, in DocumentWorkflowStage) { + out.RawByte('{') + first := true + _ = first + { + const prefix string = ",\"progress\":" + out.RawString(prefix[1:]) + out.Float64(float64(in.Progress)) + } + { + const prefix string = ",\"wordsTranslated\":" + out.RawString(prefix) + out.Uint64(uint64(in.WordsTranslated)) + } + { + const prefix string = ",\"unassignedWordsCount\":" + out.RawString(prefix) + out.Uint64(uint64(in.UnassignedWordsCount)) + } + { + const prefix string = ",\"status\":" + out.RawString(prefix) + out.String(string(in.Status)) + } + { + const prefix string = ",\"executives\":" + out.RawString(prefix) + if in.Executives == nil && (out.Flags&jwriter.NilSliceAsEmpty) == 0 { + out.RawString("null") + } else { + out.RawByte('[') + for v5, v6 := range in.Executives { + if v5 > 0 { + out.RawByte(',') + } + (v6).MarshalEasyJSON(out) + } + out.RawByte(']') + } + } + out.RawByte('}') +} + +// MarshalJSON supports json.Marshaler interface +func (v DocumentWorkflowStage) MarshalJSON() ([]byte, error) { + w := jwriter.Writer{} + easyjson18605acbEncodeGithubComMarkus621GoSmartcatClient1(&w, v) + return w.Buffer.BuildBytes(), w.Error +} + +// MarshalEasyJSON supports easyjson.Marshaler interface +func (v DocumentWorkflowStage) MarshalEasyJSON(w *jwriter.Writer) { + easyjson18605acbEncodeGithubComMarkus621GoSmartcatClient1(w, v) +} + +// UnmarshalJSON supports json.Unmarshaler interface +func (v *DocumentWorkflowStage) UnmarshalJSON(data []byte) error { + r := jlexer.Lexer{Data: data} + easyjson18605acbDecodeGithubComMarkus621GoSmartcatClient1(&r, v) + return r.Error() +} + +// UnmarshalEasyJSON supports easyjson.Unmarshaler interface +func (v *DocumentWorkflowStage) UnmarshalEasyJSON(l *jlexer.Lexer) { + easyjson18605acbDecodeGithubComMarkus621GoSmartcatClient1(l, v) +} +func easyjson18605acbDecodeGithubComMarkus621GoSmartcatClient2(in *jlexer.Lexer, out *DocumentList) { + isTopLevel := in.IsStart() + if in.IsNull() { + in.Skip() + *out = nil + } else { + in.Delim('[') + if *out == nil { + if !in.IsDelim(']') { + *out = make(DocumentList, 0, 0) + } else { + *out = DocumentList{} + } + } else { + *out = (*out)[:0] + } + for !in.IsDelim(']') { + var v7 Document + (v7).UnmarshalEasyJSON(in) + *out = append(*out, v7) + in.WantComma() + } + in.Delim(']') + } + if isTopLevel { + in.Consumed() + } +} +func easyjson18605acbEncodeGithubComMarkus621GoSmartcatClient2(out *jwriter.Writer, in DocumentList) { + if in == nil && (out.Flags&jwriter.NilSliceAsEmpty) == 0 { + out.RawString("null") + } else { + out.RawByte('[') + for v8, v9 := range in { + if v8 > 0 { + out.RawByte(',') + } + (v9).MarshalEasyJSON(out) + } + out.RawByte(']') + } +} + +// MarshalJSON supports json.Marshaler interface +func (v DocumentList) MarshalJSON() ([]byte, error) { + w := jwriter.Writer{} + easyjson18605acbEncodeGithubComMarkus621GoSmartcatClient2(&w, v) + return w.Buffer.BuildBytes(), w.Error +} + +// MarshalEasyJSON supports easyjson.Marshaler interface +func (v DocumentList) MarshalEasyJSON(w *jwriter.Writer) { + easyjson18605acbEncodeGithubComMarkus621GoSmartcatClient2(w, v) +} + +// UnmarshalJSON supports json.Unmarshaler interface +func (v *DocumentList) UnmarshalJSON(data []byte) error { + r := jlexer.Lexer{Data: data} + easyjson18605acbDecodeGithubComMarkus621GoSmartcatClient2(&r, v) + return r.Error() +} + +// UnmarshalEasyJSON supports easyjson.Unmarshaler interface +func (v *DocumentList) UnmarshalEasyJSON(l *jlexer.Lexer) { + easyjson18605acbDecodeGithubComMarkus621GoSmartcatClient2(l, v) +} +func easyjson18605acbDecodeGithubComMarkus621GoSmartcatClient3(in *jlexer.Lexer, out *Document) { + isTopLevel := in.IsStart() + if in.IsNull() { + if isTopLevel { + in.Consumed() + } + in.Skip() + return + } + in.Delim('{') + for !in.IsDelim('}') { + key := in.UnsafeFieldName(false) + in.WantColon() + if in.IsNull() { + in.Skip() + in.WantComma() + continue + } + switch key { + case "id": + out.ID = string(in.String()) + case "name": + out.Name = string(in.String()) + case "creationDate": + if data := in.Raw(); in.Ok() { + in.AddError((out.CreationDate).UnmarshalJSON(data)) + } + case "deadline": + if data := in.Raw(); in.Ok() { + in.AddError((out.Deadline).UnmarshalJSON(data)) + } + case "sourceLanguage": + out.SourceLanguage = string(in.String()) + case "documentDisassemblingStatus": + out.DisassemblingStatus = string(in.String()) + case "targetLanguage": + out.TargetLanguage = string(in.String()) + case "status": + out.Status = string(in.String()) + case "wordsCount": + out.WordsCount = uint64(in.Uint64()) + case "statusModificationDate": + if data := in.Raw(); in.Ok() { + in.AddError((out.StatusModificationDate).UnmarshalJSON(data)) + } + case "pretranslateCompleted": + out.PretranslateCompleted = bool(in.Bool()) + case "workflowStages": + if in.IsNull() { + in.Skip() + out.WorkflowStages = nil + } else { + in.Delim('[') + if out.WorkflowStages == nil { + if !in.IsDelim(']') { + out.WorkflowStages = make([]DocumentWorkflowStage, 0, 1) + } else { + out.WorkflowStages = []DocumentWorkflowStage{} + } + } else { + out.WorkflowStages = (out.WorkflowStages)[:0] + } + for !in.IsDelim(']') { + var v10 DocumentWorkflowStage + (v10).UnmarshalEasyJSON(in) + out.WorkflowStages = append(out.WorkflowStages, v10) + in.WantComma() + } + in.Delim(']') + } + case "externalId": + out.ExternalID = string(in.String()) + case "metaInfo": + out.MetaInfo = string(in.String()) + case "placeholdersAreEnabled": + out.PlaceholdersAreEnabled = bool(in.Bool()) + default: + in.SkipRecursive() + } + in.WantComma() + } + in.Delim('}') + if isTopLevel { + in.Consumed() + } +} +func easyjson18605acbEncodeGithubComMarkus621GoSmartcatClient3(out *jwriter.Writer, in Document) { + out.RawByte('{') + first := true + _ = first + { + const prefix string = ",\"id\":" + out.RawString(prefix[1:]) + out.String(string(in.ID)) + } + { + const prefix string = ",\"name\":" + out.RawString(prefix) + out.String(string(in.Name)) + } + { + const prefix string = ",\"creationDate\":" + out.RawString(prefix) + out.Raw((in.CreationDate).MarshalJSON()) + } + { + const prefix string = ",\"deadline\":" + out.RawString(prefix) + out.Raw((in.Deadline).MarshalJSON()) + } + { + const prefix string = ",\"sourceLanguage\":" + out.RawString(prefix) + out.String(string(in.SourceLanguage)) + } + { + const prefix string = ",\"documentDisassemblingStatus\":" + out.RawString(prefix) + out.String(string(in.DisassemblingStatus)) + } + { + const prefix string = ",\"targetLanguage\":" + out.RawString(prefix) + out.String(string(in.TargetLanguage)) + } + { + const prefix string = ",\"status\":" + out.RawString(prefix) + out.String(string(in.Status)) + } + { + const prefix string = ",\"wordsCount\":" + out.RawString(prefix) + out.Uint64(uint64(in.WordsCount)) + } + { + const prefix string = ",\"statusModificationDate\":" + out.RawString(prefix) + out.Raw((in.StatusModificationDate).MarshalJSON()) + } + { + const prefix string = ",\"pretranslateCompleted\":" + out.RawString(prefix) + out.Bool(bool(in.PretranslateCompleted)) + } + { + const prefix string = ",\"workflowStages\":" + out.RawString(prefix) + if in.WorkflowStages == nil && (out.Flags&jwriter.NilSliceAsEmpty) == 0 { + out.RawString("null") + } else { + out.RawByte('[') + for v11, v12 := range in.WorkflowStages { + if v11 > 0 { + out.RawByte(',') + } + (v12).MarshalEasyJSON(out) + } + out.RawByte(']') + } + } + { + const prefix string = ",\"externalId\":" + out.RawString(prefix) + out.String(string(in.ExternalID)) + } + { + const prefix string = ",\"metaInfo\":" + out.RawString(prefix) + out.String(string(in.MetaInfo)) + } + { + const prefix string = ",\"placeholdersAreEnabled\":" + out.RawString(prefix) + out.Bool(bool(in.PlaceholdersAreEnabled)) + } + out.RawByte('}') +} + +// MarshalJSON supports json.Marshaler interface +func (v Document) MarshalJSON() ([]byte, error) { + w := jwriter.Writer{} + easyjson18605acbEncodeGithubComMarkus621GoSmartcatClient3(&w, v) + return w.Buffer.BuildBytes(), w.Error +} + +// MarshalEasyJSON supports easyjson.Marshaler interface +func (v Document) MarshalEasyJSON(w *jwriter.Writer) { + easyjson18605acbEncodeGithubComMarkus621GoSmartcatClient3(w, v) +} + +// UnmarshalJSON supports json.Unmarshaler interface +func (v *Document) UnmarshalJSON(data []byte) error { + r := jlexer.Lexer{Data: data} + easyjson18605acbDecodeGithubComMarkus621GoSmartcatClient3(&r, v) + return r.Error() +} + +// UnmarshalEasyJSON supports easyjson.Unmarshaler interface +func (v *Document) UnmarshalEasyJSON(l *jlexer.Lexer) { + easyjson18605acbDecodeGithubComMarkus621GoSmartcatClient3(l, v) +} diff --git a/error.go b/error.go index 513e21f..12baf5c 100644 --- a/error.go +++ b/error.go @@ -7,6 +7,8 @@ import "github.com/pkg/errors" var ( //ErrUnknown unknown error ErrUnknown = errors.New("unknown error") + //ErrEmptyRequest empty request error + ErrEmptyRequest = errors.New("request is empty") ) //ErrorResponse model error response from the server diff --git a/example/create/main.go b/example/create/main.go new file mode 100644 index 0000000..b1643f2 --- /dev/null +++ b/example/create/main.go @@ -0,0 +1,69 @@ +package main + +import ( + "fmt" + "os" + + cli "github.com/markus621/go-smartcat-client" +) + +//nolint: errcheck +func main() { + + conf := cli.Config{ + AccountID: os.Getenv(`SMARTCAT_ACCOUNT_ID`), + AuthKey: os.Getenv(`SMARTCAT_AUTH_KEY`), + URL: cli.HostURL, + } + + client := cli.NewClient(conf) + client.Debug(true, os.Stdout) + + project, err := client.CreateProject(cli.NewProject{ + Name: "TS-10", + Description: "Перевод с русского на английский", + SourceLanguage: "en-US", + TargetLanguages: []string{"ru", "id"}, + AssignToVendor: false, + UseMT: false, + Pretranslate: false, + UseTranslationMemory: false, + AutoPropagateRepetitions: false, + WorkflowStages: []string{"translation"}, + IsForTesting: false, + }) + if err != nil { + panic(err) + } + + form := cli.NewForm() + form.AddFile("base1.json", []byte(`{"main":"hello world"}`)) + form.AddFile("base2.json", []byte(`{"main2":"hello my world"}`)) + + docs, err := client.CreateDocument(project.ID, form) + if err != nil { + panic(err) + } + + fmt.Println("Create new docs") + for _, doc := range docs { + fmt.Println(doc.Status, doc.Name) + for _, ws := range doc.WorkflowStages { + fmt.Println(ws.Status, ws.Progress) + } + } + + fmt.Println("Get status all docs") + project, err = client.GetProject(project.ID) + if err != nil { + panic(err) + } + + for _, doc := range project.Documents { + fmt.Println(doc.Status, doc.Name) + for _, ws := range doc.WorkflowStages { + fmt.Println(ws.Status, ws.Progress) + } + } + +} diff --git a/example/list/main.go b/example/list/main.go new file mode 100644 index 0000000..7eb1b8a --- /dev/null +++ b/example/list/main.go @@ -0,0 +1,74 @@ +package main + +import ( + "archive/zip" + "bytes" + "fmt" + "io/ioutil" + "os" + "time" + + cli "github.com/markus621/go-smartcat-client" +) + +//nolint: errcheck +func main() { + + conf := cli.Config{ + AccountID: os.Getenv(`SMARTCAT_ACCOUNT_ID`), + AuthKey: os.Getenv(`SMARTCAT_AUTH_KEY`), + URL: cli.HostURL, + } + + client := cli.NewClient(conf) + client.Debug(true, os.Stdout) + + list, err := client.ListProject() + if err != nil { + panic(err) + } + + ids := make([]string, 0) + for _, project := range list { + fmt.Println(project.ID, project.Name, project.Status) + for _, doc := range project.Documents { + fmt.Println(doc.ID, doc.Name, doc.Status, doc.SourceLanguage, doc.TargetLanguage, doc.WorkflowStages[0].Progress) + + if doc.WorkflowStages[0].Progress == 100 { + ids = append(ids, doc.ID) + } + } + } + + task, err := client.ExportDocument(ids) + if err != nil { + panic(err) + } + + <-time.After(3 * time.Second) + + data, err := client.ExportDocumentByTaskID(task.ID) + if err != nil { + panic(err) + } + + // as text file if one document + if len(ids) == 1 { + fmt.Println(string(data)) + os.Exit(0) + } + + // as zip file if many documents + z, err := zip.NewReader(bytes.NewReader(data), int64(len(data))) + if err != nil { + panic(err) + } + for _, f := range z.File { + reader, err := f.Open() + if err != nil { + panic(err) + } + b, err := ioutil.ReadAll(reader) + fmt.Println(f.Name, string(b), err) + } +} diff --git a/example/main.go b/example/main.go deleted file mode 100644 index 9fa9837..0000000 --- a/example/main.go +++ /dev/null @@ -1,38 +0,0 @@ -package main - -import ( - "os" - - cli "github.com/markus621/go-smartcat-client" -) - -//nolint: errcheck -func main() { - - conf := cli.Config{ - AccountID: os.Getenv(`SMARTCAT_ACCOUNT_ID`), - AuthKey: os.Getenv(`SMARTCAT_AUTH_KEY`), - URL: cli.HostURL, - } - - client := cli.NewClient(conf) - client.Debug(true, os.Stdout) - - _, _ = client.GetAccount() - _, _ = client.GetAccountMTEngines() - - _, _ = client.SetCallback(cli.Callback{ - URL: "https://demo.example/callback", - AdditionalHeaders: []cli.AdditionalHeader{ - {Name: "x-header", Value: "demo"}, - }, - }) - _, _ = client.GetCallback() - _ = client.DelCallback() - _, _ = client.GetCallbackLastErrors(10) - - _, _ = client.ListProject() - _, _ = client.GetProject("0") - _, _ = client.SetProject("0", cli.PatchProject{}) - _ = client.DelProject("0") -} diff --git a/project.go b/project.go index b421536..bed9780 100644 --- a/project.go +++ b/project.go @@ -15,7 +15,6 @@ const ( uriProjectRestore = "/api/integration/v1/project/restore" uriProjectComplete = "/api/integration/v1/project/complete" uriProjectCreate = "/api/integration/v1/project/create" - uriProjectDocument = "/api/integration/v1/project/document" uriProjectStatistics = "/api/integration/v2/project/%s/statistics" uriProjectCWStatistics = "/api/integration/v2/project/%s/completedWorkStatistics" uriProjectTranslationMemories = "/api/integration/v2/project/%s/translationmemories" @@ -51,6 +50,20 @@ type ( Managers []string `json:"managers"` Number []string `json:"number"` } + //NewProject model + NewProject struct { + Name string `json:"name"` + Description string `json:"description"` + SourceLanguage string `json:"sourceLanguage"` + TargetLanguages []string `json:"targetLanguages"` + AssignToVendor bool `json:"assignToVendor"` + UseMT bool `json:"useMT"` + Pretranslate bool `json:"pretranslate"` + UseTranslationMemory bool `json:"useTranslationMemory"` + AutoPropagateRepetitions bool `json:"autoPropagateRepetitions"` + WorkflowStages []string `json:"workflowStages"` + IsForTesting bool `json:"isForTesting"` + } //Cost model Cost struct { Value float64 `json:"value"` @@ -68,16 +81,8 @@ type ( } //WorkflowStage model WorkflowStage struct { - Progress uint `json:"progress"` - StageType string `json:"stageType"` - } - //DocumentWorkflowStage model - DocumentWorkflowStage struct { - Progress uint `json:"progress"` - WordsTranslated uint64 `json:"wordsTranslated"` - UnassignedWordsCount uint64 `json:"unassignedWordsCount"` - Status string `json:"status"` - Executives []Executive `json:"executives"` + Progress float64 `json:"progress"` + StageType string `json:"stageType"` } //Executive model Executive struct { @@ -86,24 +91,6 @@ type ( Progress uint `json:"progress"` SupplierType string `json:"supplierType"` } - //Document model - Document struct { - ID string `json:"id"` - Name string `json:"name"` - CreationDate time.Time `json:"creationDate"` - Deadline time.Time `json:"deadline"` - SourceLanguage string `json:"sourceLanguage"` - DisassemblingStatus string `json:"documentDisassemblingStatus"` - TargetLanguage string `json:"targetLanguage"` - Status string `json:"status"` - WordsCount uint64 `json:"wordsCount"` - StatusModificationDate time.Time `json:"statusModificationDate"` - PretranslateCompleted bool `json:"pretranslateCompleted"` - WorkflowStages []DocumentWorkflowStage `json:"workflowStages"` - ExternalID string `json:"externalId"` - MetaInfo string `json:"metaInfo"` - PlaceholdersAreEnabled bool `json:"placeholdersAreEnabled"` - } //PatchProject model PatchProject struct { Name string `json:"name"` @@ -162,68 +149,78 @@ type ( } ) +//CreateProject Create new project +func (c *Client) CreateProject(in NewProject) (out Project, err error) { + form := NewForm() + if err = form.AddJSON("model", &in); err != nil { + return + } + _, err = c.form(http.MethodPost, uriProjectCreate, form, &out) + return +} + //DelProject Delete the project -func (v *Client) DelProject(id string) (err error) { - _, err = v.call(http.MethodDelete, fmt.Sprintf(uriProject, id), nil, nil) +func (c *Client) DelProject(id string) (err error) { + _, err = c.json(http.MethodDelete, fmt.Sprintf(uriProject, id), nil, nil) return } //GetProject Receive the project model -func (v *Client) GetProject(id string) (out Project, err error) { - _, err = v.call(http.MethodGet, fmt.Sprintf(uriProject, id), nil, &out) +func (c *Client) GetProject(id string) (out Project, err error) { + _, err = c.json(http.MethodGet, fmt.Sprintf(uriProject, id), nil, &out) return } //SetProject Change the project model -func (v *Client) SetProject(id string, in PatchProject) (out Project, err error) { - _, err = v.call(http.MethodPut, fmt.Sprintf(uriProject, id), &in, &out) +func (c *Client) SetProject(id string, in PatchProject) (out Project, err error) { + _, err = c.json(http.MethodPut, fmt.Sprintf(uriProject, id), &in, &out) return } //CancelProject Cancel the project -func (v *Client) CancelProject(id string) (err error) { - _, err = v.call(http.MethodPost, uriProjectCancel+"?projectId="+id, nil, nil) +func (c *Client) CancelProject(id string) (err error) { + _, err = c.json(http.MethodPost, uriProjectCancel+"?projectId="+id, nil, nil) return } //RestoreProject Restore the project -func (v *Client) RestoreProject(id string) (err error) { - _, err = v.call(http.MethodPost, uriProjectRestore+"?projectId="+id, nil, nil) +func (c *Client) RestoreProject(id string) (err error) { + _, err = c.json(http.MethodPost, uriProjectRestore+"?projectId="+id, nil, nil) return } //CompleteProject Complete the project -func (v *Client) CompleteProject(id string) (err error) { - _, err = v.call(http.MethodPost, uriProjectComplete+"?projectId="+id, nil, nil) +func (c *Client) CompleteProject(id string) (err error) { + _, err = c.json(http.MethodPost, uriProjectComplete+"?projectId="+id, nil, nil) return } //ListProject List all projects -func (v *Client) ListProject() (out ProjectsList, err error) { - _, err = v.call(http.MethodGet, uriProjectList, nil, &out) +func (c *Client) ListProject() (out ProjectsList, err error) { + _, err = c.json(http.MethodGet, uriProjectList, nil, &out) return } //GetProjectStatistics Receive statistics -func (v *Client) GetProjectStatistics(id string) (out StatisticsList, err error) { - _, err = v.call(http.MethodGet, uriProjectStatistics, nil, &out) +func (c *Client) GetProjectStatistics(id string) (out StatisticsList, err error) { + _, err = c.json(http.MethodGet, uriProjectStatistics, nil, &out) return } //GetProjectCompletedWorkStatistics Receiving statistics for the completed parts of the project -func (v *Client) GetProjectCompletedWorkStatistics(id string) (out CompletedWorkStatisticsList, err error) { - _, err = v.call(http.MethodGet, uriProjectCWStatistics, nil, &out) +func (c *Client) GetProjectCompletedWorkStatistics(id string) (out CompletedWorkStatisticsList, err error) { + _, err = c.json(http.MethodGet, uriProjectCWStatistics, nil, &out) return } //GetProjectTranslationMemories Receiving a list of the TMs plugged into the project -func (v *Client) GetProjectTranslationMemories(id string) (out TranslationMemories, err error) { - _, err = v.call(http.MethodGet, uriProjectTranslationMemories, nil, &out) +func (c *Client) GetProjectTranslationMemories(id string) (out TranslationMemories, err error) { + _, err = c.json(http.MethodGet, uriProjectTranslationMemories, nil, &out) return } //SetProjectTranslationMemories Receiving a list of the TMs plugged into the project -func (v *Client) SetProjectTranslationMemories(id string, in TranslationMemories) (err error) { - _, err = v.call(http.MethodGet, uriProjectTranslationMemories, &in, nil) +func (c *Client) SetProjectTranslationMemories(id string, in TranslationMemories) (err error) { + _, err = c.json(http.MethodGet, uriProjectTranslationMemories, &in, nil) return } diff --git a/project_easyjson.go b/project_easyjson.go index 15c3297..0f7b0a9 100644 --- a/project_easyjson.go +++ b/project_easyjson.go @@ -37,7 +37,7 @@ func easyjson7b166cadDecodeGithubComMarkus621GoSmartcatClient(in *jlexer.Lexer, } switch key { case "progress": - out.Progress = uint(in.Uint()) + out.Progress = float64(in.Float64()) case "stageType": out.StageType = string(in.String()) default: @@ -57,7 +57,7 @@ func easyjson7b166cadEncodeGithubComMarkus621GoSmartcatClient(out *jwriter.Write { const prefix string = ",\"progress\":" out.RawString(prefix[1:]) - out.Uint(uint(in.Progress)) + out.Float64(float64(in.Progress)) } { const prefix string = ",\"stageType\":" @@ -1479,7 +1479,7 @@ func (v *PatchProject) UnmarshalJSON(data []byte) error { func (v *PatchProject) UnmarshalEasyJSON(l *jlexer.Lexer) { easyjson7b166cadDecodeGithubComMarkus621GoSmartcatClient9(l, v) } -func easyjson7b166cadDecodeGithubComMarkus621GoSmartcatClient10(in *jlexer.Lexer, out *Executive) { +func easyjson7b166cadDecodeGithubComMarkus621GoSmartcatClient10(in *jlexer.Lexer, out *NewProject) { isTopLevel := in.IsStart() if in.IsNull() { if isTopLevel { @@ -1498,14 +1498,70 @@ func easyjson7b166cadDecodeGithubComMarkus621GoSmartcatClient10(in *jlexer.Lexer continue } switch key { - case "id": - out.ID = string(in.String()) - case "assignedWordsCount": - out.AssignedWordsCount = uint64(in.Uint64()) - case "progress": - out.Progress = uint(in.Uint()) - case "supplierType": - out.SupplierType = string(in.String()) + case "name": + out.Name = string(in.String()) + case "description": + out.Description = string(in.String()) + case "sourceLanguage": + out.SourceLanguage = string(in.String()) + case "targetLanguages": + if in.IsNull() { + in.Skip() + out.TargetLanguages = nil + } else { + in.Delim('[') + if out.TargetLanguages == nil { + if !in.IsDelim(']') { + out.TargetLanguages = make([]string, 0, 4) + } else { + out.TargetLanguages = []string{} + } + } else { + out.TargetLanguages = (out.TargetLanguages)[:0] + } + for !in.IsDelim(']') { + var v49 string + v49 = string(in.String()) + out.TargetLanguages = append(out.TargetLanguages, v49) + in.WantComma() + } + in.Delim(']') + } + case "assignToVendor": + out.AssignToVendor = bool(in.Bool()) + case "useMT": + out.UseMT = bool(in.Bool()) + case "pretranslate": + out.Pretranslate = bool(in.Bool()) + case "useTranslationMemory": + out.UseTranslationMemory = bool(in.Bool()) + case "autoPropagateRepetitions": + out.AutoPropagateRepetitions = bool(in.Bool()) + case "workflowStages": + if in.IsNull() { + in.Skip() + out.WorkflowStages = nil + } else { + in.Delim('[') + if out.WorkflowStages == nil { + if !in.IsDelim(']') { + out.WorkflowStages = make([]string, 0, 4) + } else { + out.WorkflowStages = []string{} + } + } else { + out.WorkflowStages = (out.WorkflowStages)[:0] + } + for !in.IsDelim(']') { + var v50 string + v50 = string(in.String()) + out.WorkflowStages = append(out.WorkflowStages, v50) + in.WantComma() + } + in.Delim(']') + } + case "isForTesting": + out.IsForTesting = bool(in.Bool()) default: in.SkipRecursive() } @@ -1516,183 +1572,114 @@ func easyjson7b166cadDecodeGithubComMarkus621GoSmartcatClient10(in *jlexer.Lexer in.Consumed() } } -func easyjson7b166cadEncodeGithubComMarkus621GoSmartcatClient10(out *jwriter.Writer, in Executive) { +func easyjson7b166cadEncodeGithubComMarkus621GoSmartcatClient10(out *jwriter.Writer, in NewProject) { out.RawByte('{') first := true _ = first { - const prefix string = ",\"id\":" + const prefix string = ",\"name\":" out.RawString(prefix[1:]) - out.String(string(in.ID)) + out.String(string(in.Name)) } { - const prefix string = ",\"assignedWordsCount\":" + const prefix string = ",\"description\":" out.RawString(prefix) - out.Uint64(uint64(in.AssignedWordsCount)) + out.String(string(in.Description)) } { - const prefix string = ",\"progress\":" + const prefix string = ",\"sourceLanguage\":" out.RawString(prefix) - out.Uint(uint(in.Progress)) + out.String(string(in.SourceLanguage)) } { - const prefix string = ",\"supplierType\":" + const prefix string = ",\"targetLanguages\":" out.RawString(prefix) - out.String(string(in.SupplierType)) - } - out.RawByte('}') -} - -// MarshalJSON supports json.Marshaler interface -func (v Executive) MarshalJSON() ([]byte, error) { - w := jwriter.Writer{} - easyjson7b166cadEncodeGithubComMarkus621GoSmartcatClient10(&w, v) - return w.Buffer.BuildBytes(), w.Error -} - -// MarshalEasyJSON supports easyjson.Marshaler interface -func (v Executive) MarshalEasyJSON(w *jwriter.Writer) { - easyjson7b166cadEncodeGithubComMarkus621GoSmartcatClient10(w, v) -} - -// UnmarshalJSON supports json.Unmarshaler interface -func (v *Executive) UnmarshalJSON(data []byte) error { - r := jlexer.Lexer{Data: data} - easyjson7b166cadDecodeGithubComMarkus621GoSmartcatClient10(&r, v) - return r.Error() -} - -// UnmarshalEasyJSON supports easyjson.Unmarshaler interface -func (v *Executive) UnmarshalEasyJSON(l *jlexer.Lexer) { - easyjson7b166cadDecodeGithubComMarkus621GoSmartcatClient10(l, v) -} -func easyjson7b166cadDecodeGithubComMarkus621GoSmartcatClient11(in *jlexer.Lexer, out *DocumentWorkflowStage) { - isTopLevel := in.IsStart() - if in.IsNull() { - if isTopLevel { - in.Consumed() - } - in.Skip() - return - } - in.Delim('{') - for !in.IsDelim('}') { - key := in.UnsafeFieldName(false) - in.WantColon() - if in.IsNull() { - in.Skip() - in.WantComma() - continue - } - switch key { - case "progress": - out.Progress = uint(in.Uint()) - case "wordsTranslated": - out.WordsTranslated = uint64(in.Uint64()) - case "unassignedWordsCount": - out.UnassignedWordsCount = uint64(in.Uint64()) - case "status": - out.Status = string(in.String()) - case "executives": - if in.IsNull() { - in.Skip() - out.Executives = nil - } else { - in.Delim('[') - if out.Executives == nil { - if !in.IsDelim(']') { - out.Executives = make([]Executive, 0, 1) - } else { - out.Executives = []Executive{} - } - } else { - out.Executives = (out.Executives)[:0] - } - for !in.IsDelim(']') { - var v49 Executive - (v49).UnmarshalEasyJSON(in) - out.Executives = append(out.Executives, v49) - in.WantComma() + if in.TargetLanguages == nil && (out.Flags&jwriter.NilSliceAsEmpty) == 0 { + out.RawString("null") + } else { + out.RawByte('[') + for v51, v52 := range in.TargetLanguages { + if v51 > 0 { + out.RawByte(',') } - in.Delim(']') + out.String(string(v52)) } - default: - in.SkipRecursive() + out.RawByte(']') } - in.WantComma() } - in.Delim('}') - if isTopLevel { - in.Consumed() + { + const prefix string = ",\"assignToVendor\":" + out.RawString(prefix) + out.Bool(bool(in.AssignToVendor)) } -} -func easyjson7b166cadEncodeGithubComMarkus621GoSmartcatClient11(out *jwriter.Writer, in DocumentWorkflowStage) { - out.RawByte('{') - first := true - _ = first { - const prefix string = ",\"progress\":" - out.RawString(prefix[1:]) - out.Uint(uint(in.Progress)) + const prefix string = ",\"useMT\":" + out.RawString(prefix) + out.Bool(bool(in.UseMT)) } { - const prefix string = ",\"wordsTranslated\":" + const prefix string = ",\"pretranslate\":" out.RawString(prefix) - out.Uint64(uint64(in.WordsTranslated)) + out.Bool(bool(in.Pretranslate)) } { - const prefix string = ",\"unassignedWordsCount\":" + const prefix string = ",\"useTranslationMemory\":" out.RawString(prefix) - out.Uint64(uint64(in.UnassignedWordsCount)) + out.Bool(bool(in.UseTranslationMemory)) } { - const prefix string = ",\"status\":" + const prefix string = ",\"autoPropagateRepetitions\":" out.RawString(prefix) - out.String(string(in.Status)) + out.Bool(bool(in.AutoPropagateRepetitions)) } { - const prefix string = ",\"executives\":" + const prefix string = ",\"workflowStages\":" out.RawString(prefix) - if in.Executives == nil && (out.Flags&jwriter.NilSliceAsEmpty) == 0 { + if in.WorkflowStages == nil && (out.Flags&jwriter.NilSliceAsEmpty) == 0 { out.RawString("null") } else { out.RawByte('[') - for v50, v51 := range in.Executives { - if v50 > 0 { + for v53, v54 := range in.WorkflowStages { + if v53 > 0 { out.RawByte(',') } - (v51).MarshalEasyJSON(out) + out.String(string(v54)) } out.RawByte(']') } } + { + const prefix string = ",\"isForTesting\":" + out.RawString(prefix) + out.Bool(bool(in.IsForTesting)) + } out.RawByte('}') } // MarshalJSON supports json.Marshaler interface -func (v DocumentWorkflowStage) MarshalJSON() ([]byte, error) { +func (v NewProject) MarshalJSON() ([]byte, error) { w := jwriter.Writer{} - easyjson7b166cadEncodeGithubComMarkus621GoSmartcatClient11(&w, v) + easyjson7b166cadEncodeGithubComMarkus621GoSmartcatClient10(&w, v) return w.Buffer.BuildBytes(), w.Error } // MarshalEasyJSON supports easyjson.Marshaler interface -func (v DocumentWorkflowStage) MarshalEasyJSON(w *jwriter.Writer) { - easyjson7b166cadEncodeGithubComMarkus621GoSmartcatClient11(w, v) +func (v NewProject) MarshalEasyJSON(w *jwriter.Writer) { + easyjson7b166cadEncodeGithubComMarkus621GoSmartcatClient10(w, v) } // UnmarshalJSON supports json.Unmarshaler interface -func (v *DocumentWorkflowStage) UnmarshalJSON(data []byte) error { +func (v *NewProject) UnmarshalJSON(data []byte) error { r := jlexer.Lexer{Data: data} - easyjson7b166cadDecodeGithubComMarkus621GoSmartcatClient11(&r, v) + easyjson7b166cadDecodeGithubComMarkus621GoSmartcatClient10(&r, v) return r.Error() } // UnmarshalEasyJSON supports easyjson.Unmarshaler interface -func (v *DocumentWorkflowStage) UnmarshalEasyJSON(l *jlexer.Lexer) { - easyjson7b166cadDecodeGithubComMarkus621GoSmartcatClient11(l, v) +func (v *NewProject) UnmarshalEasyJSON(l *jlexer.Lexer) { + easyjson7b166cadDecodeGithubComMarkus621GoSmartcatClient10(l, v) } -func easyjson7b166cadDecodeGithubComMarkus621GoSmartcatClient12(in *jlexer.Lexer, out *Document) { +func easyjson7b166cadDecodeGithubComMarkus621GoSmartcatClient11(in *jlexer.Lexer, out *Executive) { isTopLevel := in.IsStart() if in.IsNull() { if isTopLevel { @@ -1713,61 +1700,12 @@ func easyjson7b166cadDecodeGithubComMarkus621GoSmartcatClient12(in *jlexer.Lexer switch key { case "id": out.ID = string(in.String()) - case "name": - out.Name = string(in.String()) - case "creationDate": - if data := in.Raw(); in.Ok() { - in.AddError((out.CreationDate).UnmarshalJSON(data)) - } - case "deadline": - if data := in.Raw(); in.Ok() { - in.AddError((out.Deadline).UnmarshalJSON(data)) - } - case "sourceLanguage": - out.SourceLanguage = string(in.String()) - case "documentDisassemblingStatus": - out.DisassemblingStatus = string(in.String()) - case "targetLanguage": - out.TargetLanguage = string(in.String()) - case "status": - out.Status = string(in.String()) - case "wordsCount": - out.WordsCount = uint64(in.Uint64()) - case "statusModificationDate": - if data := in.Raw(); in.Ok() { - in.AddError((out.StatusModificationDate).UnmarshalJSON(data)) - } - case "pretranslateCompleted": - out.PretranslateCompleted = bool(in.Bool()) - case "workflowStages": - if in.IsNull() { - in.Skip() - out.WorkflowStages = nil - } else { - in.Delim('[') - if out.WorkflowStages == nil { - if !in.IsDelim(']') { - out.WorkflowStages = make([]DocumentWorkflowStage, 0, 1) - } else { - out.WorkflowStages = []DocumentWorkflowStage{} - } - } else { - out.WorkflowStages = (out.WorkflowStages)[:0] - } - for !in.IsDelim(']') { - var v52 DocumentWorkflowStage - (v52).UnmarshalEasyJSON(in) - out.WorkflowStages = append(out.WorkflowStages, v52) - in.WantComma() - } - in.Delim(']') - } - case "externalId": - out.ExternalID = string(in.String()) - case "metaInfo": - out.MetaInfo = string(in.String()) - case "placeholdersAreEnabled": - out.PlaceholdersAreEnabled = bool(in.Bool()) + case "assignedWordsCount": + out.AssignedWordsCount = uint64(in.Uint64()) + case "progress": + out.Progress = uint(in.Uint()) + case "supplierType": + out.SupplierType = string(in.String()) default: in.SkipRecursive() } @@ -1778,7 +1716,7 @@ func easyjson7b166cadDecodeGithubComMarkus621GoSmartcatClient12(in *jlexer.Lexer in.Consumed() } } -func easyjson7b166cadEncodeGithubComMarkus621GoSmartcatClient12(out *jwriter.Writer, in Document) { +func easyjson7b166cadEncodeGithubComMarkus621GoSmartcatClient11(out *jwriter.Writer, in Executive) { out.RawByte('{') first := true _ = first @@ -1788,113 +1726,47 @@ func easyjson7b166cadEncodeGithubComMarkus621GoSmartcatClient12(out *jwriter.Wri out.String(string(in.ID)) } { - const prefix string = ",\"name\":" - out.RawString(prefix) - out.String(string(in.Name)) - } - { - const prefix string = ",\"creationDate\":" - out.RawString(prefix) - out.Raw((in.CreationDate).MarshalJSON()) - } - { - const prefix string = ",\"deadline\":" - out.RawString(prefix) - out.Raw((in.Deadline).MarshalJSON()) - } - { - const prefix string = ",\"sourceLanguage\":" - out.RawString(prefix) - out.String(string(in.SourceLanguage)) - } - { - const prefix string = ",\"documentDisassemblingStatus\":" - out.RawString(prefix) - out.String(string(in.DisassemblingStatus)) - } - { - const prefix string = ",\"targetLanguage\":" - out.RawString(prefix) - out.String(string(in.TargetLanguage)) - } - { - const prefix string = ",\"status\":" - out.RawString(prefix) - out.String(string(in.Status)) - } - { - const prefix string = ",\"wordsCount\":" - out.RawString(prefix) - out.Uint64(uint64(in.WordsCount)) - } - { - const prefix string = ",\"statusModificationDate\":" - out.RawString(prefix) - out.Raw((in.StatusModificationDate).MarshalJSON()) - } - { - const prefix string = ",\"pretranslateCompleted\":" - out.RawString(prefix) - out.Bool(bool(in.PretranslateCompleted)) - } - { - const prefix string = ",\"workflowStages\":" - out.RawString(prefix) - if in.WorkflowStages == nil && (out.Flags&jwriter.NilSliceAsEmpty) == 0 { - out.RawString("null") - } else { - out.RawByte('[') - for v53, v54 := range in.WorkflowStages { - if v53 > 0 { - out.RawByte(',') - } - (v54).MarshalEasyJSON(out) - } - out.RawByte(']') - } - } - { - const prefix string = ",\"externalId\":" + const prefix string = ",\"assignedWordsCount\":" out.RawString(prefix) - out.String(string(in.ExternalID)) + out.Uint64(uint64(in.AssignedWordsCount)) } { - const prefix string = ",\"metaInfo\":" + const prefix string = ",\"progress\":" out.RawString(prefix) - out.String(string(in.MetaInfo)) + out.Uint(uint(in.Progress)) } { - const prefix string = ",\"placeholdersAreEnabled\":" + const prefix string = ",\"supplierType\":" out.RawString(prefix) - out.Bool(bool(in.PlaceholdersAreEnabled)) + out.String(string(in.SupplierType)) } out.RawByte('}') } // MarshalJSON supports json.Marshaler interface -func (v Document) MarshalJSON() ([]byte, error) { +func (v Executive) MarshalJSON() ([]byte, error) { w := jwriter.Writer{} - easyjson7b166cadEncodeGithubComMarkus621GoSmartcatClient12(&w, v) + easyjson7b166cadEncodeGithubComMarkus621GoSmartcatClient11(&w, v) return w.Buffer.BuildBytes(), w.Error } // MarshalEasyJSON supports easyjson.Marshaler interface -func (v Document) MarshalEasyJSON(w *jwriter.Writer) { - easyjson7b166cadEncodeGithubComMarkus621GoSmartcatClient12(w, v) +func (v Executive) MarshalEasyJSON(w *jwriter.Writer) { + easyjson7b166cadEncodeGithubComMarkus621GoSmartcatClient11(w, v) } // UnmarshalJSON supports json.Unmarshaler interface -func (v *Document) UnmarshalJSON(data []byte) error { +func (v *Executive) UnmarshalJSON(data []byte) error { r := jlexer.Lexer{Data: data} - easyjson7b166cadDecodeGithubComMarkus621GoSmartcatClient12(&r, v) + easyjson7b166cadDecodeGithubComMarkus621GoSmartcatClient11(&r, v) return r.Error() } // UnmarshalEasyJSON supports easyjson.Unmarshaler interface -func (v *Document) UnmarshalEasyJSON(l *jlexer.Lexer) { - easyjson7b166cadDecodeGithubComMarkus621GoSmartcatClient12(l, v) +func (v *Executive) UnmarshalEasyJSON(l *jlexer.Lexer) { + easyjson7b166cadDecodeGithubComMarkus621GoSmartcatClient11(l, v) } -func easyjson7b166cadDecodeGithubComMarkus621GoSmartcatClient13(in *jlexer.Lexer, out *Cost) { +func easyjson7b166cadDecodeGithubComMarkus621GoSmartcatClient12(in *jlexer.Lexer, out *Cost) { isTopLevel := in.IsStart() if in.IsNull() { if isTopLevel { @@ -1933,7 +1805,7 @@ func easyjson7b166cadDecodeGithubComMarkus621GoSmartcatClient13(in *jlexer.Lexer in.Consumed() } } -func easyjson7b166cadEncodeGithubComMarkus621GoSmartcatClient13(out *jwriter.Writer, in Cost) { +func easyjson7b166cadEncodeGithubComMarkus621GoSmartcatClient12(out *jwriter.Writer, in Cost) { out.RawByte('{') first := true _ = first @@ -1968,27 +1840,27 @@ func easyjson7b166cadEncodeGithubComMarkus621GoSmartcatClient13(out *jwriter.Wri // MarshalJSON supports json.Marshaler interface func (v Cost) MarshalJSON() ([]byte, error) { w := jwriter.Writer{} - easyjson7b166cadEncodeGithubComMarkus621GoSmartcatClient13(&w, v) + easyjson7b166cadEncodeGithubComMarkus621GoSmartcatClient12(&w, v) return w.Buffer.BuildBytes(), w.Error } // MarshalEasyJSON supports easyjson.Marshaler interface func (v Cost) MarshalEasyJSON(w *jwriter.Writer) { - easyjson7b166cadEncodeGithubComMarkus621GoSmartcatClient13(w, v) + easyjson7b166cadEncodeGithubComMarkus621GoSmartcatClient12(w, v) } // UnmarshalJSON supports json.Unmarshaler interface func (v *Cost) UnmarshalJSON(data []byte) error { r := jlexer.Lexer{Data: data} - easyjson7b166cadDecodeGithubComMarkus621GoSmartcatClient13(&r, v) + easyjson7b166cadDecodeGithubComMarkus621GoSmartcatClient12(&r, v) return r.Error() } // UnmarshalEasyJSON supports easyjson.Unmarshaler interface func (v *Cost) UnmarshalEasyJSON(l *jlexer.Lexer) { - easyjson7b166cadDecodeGithubComMarkus621GoSmartcatClient13(l, v) + easyjson7b166cadDecodeGithubComMarkus621GoSmartcatClient12(l, v) } -func easyjson7b166cadDecodeGithubComMarkus621GoSmartcatClient14(in *jlexer.Lexer, out *CompletedWorkStatisticsList) { +func easyjson7b166cadDecodeGithubComMarkus621GoSmartcatClient13(in *jlexer.Lexer, out *CompletedWorkStatisticsList) { isTopLevel := in.IsStart() if in.IsNull() { in.Skip() @@ -2016,7 +1888,7 @@ func easyjson7b166cadDecodeGithubComMarkus621GoSmartcatClient14(in *jlexer.Lexer in.Consumed() } } -func easyjson7b166cadEncodeGithubComMarkus621GoSmartcatClient14(out *jwriter.Writer, in CompletedWorkStatisticsList) { +func easyjson7b166cadEncodeGithubComMarkus621GoSmartcatClient13(out *jwriter.Writer, in CompletedWorkStatisticsList) { if in == nil && (out.Flags&jwriter.NilSliceAsEmpty) == 0 { out.RawString("null") } else { @@ -2034,27 +1906,27 @@ func easyjson7b166cadEncodeGithubComMarkus621GoSmartcatClient14(out *jwriter.Wri // MarshalJSON supports json.Marshaler interface func (v CompletedWorkStatisticsList) MarshalJSON() ([]byte, error) { w := jwriter.Writer{} - easyjson7b166cadEncodeGithubComMarkus621GoSmartcatClient14(&w, v) + easyjson7b166cadEncodeGithubComMarkus621GoSmartcatClient13(&w, v) return w.Buffer.BuildBytes(), w.Error } // MarshalEasyJSON supports easyjson.Marshaler interface func (v CompletedWorkStatisticsList) MarshalEasyJSON(w *jwriter.Writer) { - easyjson7b166cadEncodeGithubComMarkus621GoSmartcatClient14(w, v) + easyjson7b166cadEncodeGithubComMarkus621GoSmartcatClient13(w, v) } // UnmarshalJSON supports json.Unmarshaler interface func (v *CompletedWorkStatisticsList) UnmarshalJSON(data []byte) error { r := jlexer.Lexer{Data: data} - easyjson7b166cadDecodeGithubComMarkus621GoSmartcatClient14(&r, v) + easyjson7b166cadDecodeGithubComMarkus621GoSmartcatClient13(&r, v) return r.Error() } // UnmarshalEasyJSON supports easyjson.Unmarshaler interface func (v *CompletedWorkStatisticsList) UnmarshalEasyJSON(l *jlexer.Lexer) { - easyjson7b166cadDecodeGithubComMarkus621GoSmartcatClient14(l, v) + easyjson7b166cadDecodeGithubComMarkus621GoSmartcatClient13(l, v) } -func easyjson7b166cadDecodeGithubComMarkus621GoSmartcatClient15(in *jlexer.Lexer, out *CompletedWorkStatistics) { +func easyjson7b166cadDecodeGithubComMarkus621GoSmartcatClient14(in *jlexer.Lexer, out *CompletedWorkStatistics) { isTopLevel := in.IsStart() if in.IsNull() { if isTopLevel { @@ -2137,7 +2009,7 @@ func easyjson7b166cadDecodeGithubComMarkus621GoSmartcatClient15(in *jlexer.Lexer in.Consumed() } } -func easyjson7b166cadEncodeGithubComMarkus621GoSmartcatClient15(out *jwriter.Writer, in CompletedWorkStatistics) { +func easyjson7b166cadEncodeGithubComMarkus621GoSmartcatClient14(out *jwriter.Writer, in CompletedWorkStatistics) { out.RawByte('{') first := true _ = first @@ -2199,25 +2071,25 @@ func easyjson7b166cadEncodeGithubComMarkus621GoSmartcatClient15(out *jwriter.Wri // MarshalJSON supports json.Marshaler interface func (v CompletedWorkStatistics) MarshalJSON() ([]byte, error) { w := jwriter.Writer{} - easyjson7b166cadEncodeGithubComMarkus621GoSmartcatClient15(&w, v) + easyjson7b166cadEncodeGithubComMarkus621GoSmartcatClient14(&w, v) return w.Buffer.BuildBytes(), w.Error } // MarshalEasyJSON supports easyjson.Marshaler interface func (v CompletedWorkStatistics) MarshalEasyJSON(w *jwriter.Writer) { - easyjson7b166cadEncodeGithubComMarkus621GoSmartcatClient15(w, v) + easyjson7b166cadEncodeGithubComMarkus621GoSmartcatClient14(w, v) } // UnmarshalJSON supports json.Unmarshaler interface func (v *CompletedWorkStatistics) UnmarshalJSON(data []byte) error { r := jlexer.Lexer{Data: data} - easyjson7b166cadDecodeGithubComMarkus621GoSmartcatClient15(&r, v) + easyjson7b166cadDecodeGithubComMarkus621GoSmartcatClient14(&r, v) return r.Error() } // UnmarshalEasyJSON supports easyjson.Unmarshaler interface func (v *CompletedWorkStatistics) UnmarshalEasyJSON(l *jlexer.Lexer) { - easyjson7b166cadDecodeGithubComMarkus621GoSmartcatClient15(l, v) + easyjson7b166cadDecodeGithubComMarkus621GoSmartcatClient14(l, v) } func easyjson7b166cadDecode1(in *jlexer.Lexer, out *struct { ID string `json:"id"`