diff --git a/Makefile b/Makefile index 0684214..6ab9ef0 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,8 @@ lint: golangci-lint -v run ./... - generate: - go generate -v ./... \ No newline at end of file + go generate -v ./... + +demo_run: + cd ./example && go run main.go \ No newline at end of file diff --git a/README.md b/README.md index 864419f..68ee2cd 100644 --- a/README.md +++ b/README.md @@ -29,7 +29,16 @@ func main() { client := cli.NewClient(conf) client.Debug(true, os.Stdout) - acc, err := client.Account() - mte, err := client.AccountMTEngines() + _, _ = 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) } ``` \ No newline at end of file diff --git a/account.go b/account.go index 1af150b..047a307 100644 --- a/account.go +++ b/account.go @@ -26,14 +26,14 @@ type ( AccountMTEngines []AccountMTEngine ) -//Account Receiving the account details -func (v *Client) Account() (o Account, err error) { - err, _ = v.call(http.MethodGet, uriAccount, nil, &o) +//GetAccount Receiving the account details +func (v *Client) GetAccount() (out Account, err error) { + err, _ = v.call(http.MethodGet, uriAccount, nil, &out) return } -//AccountMtengines Receiving MT engines available for the account -func (v *Client) AccountMTEngines() (o AccountMTEngines, err error) { - err, _ = v.call(http.MethodGet, uriAccountMTEngines, nil, &o) +//GetAccountMTEngines Receiving MT engines available for the account +func (v *Client) GetAccountMTEngines() (out AccountMTEngines, err error) { + err, _ = v.call(http.MethodGet, uriAccountMTEngines, nil, &out) return } diff --git a/callback.go b/callback.go new file mode 100644 index 0000000..a341ff0 --- /dev/null +++ b/callback.go @@ -0,0 +1,58 @@ +package smartcatclient + +import ( + "fmt" + "net/http" +) + +//go:generate easyjson + +const ( + uriCallback = "/api/integration/v1/callback" + uriCallbackLastErrors = "/api/integration/v1/callback/lastErrors" +) + +//easyjson:json +type ( + AdditionalHeader struct { + Name string `json:"name"` + Value string `json:"value"` + } + Callback struct { + URL string `json:"url"` + AdditionalHeaders []AdditionalHeader `json:"additionalHeaders"` + } + LastError struct { + Created string `json:"created"` + URL string `json:"url"` + Reason string `json:"reason"` + Code int `json:"code"` + Content string `json:"content"` + SourceIds []string `json:"sourceIds"` + } + LastErrors []LastError +) + +//DelCallback Resetting the configuration of notifications reception +func (v *Client) DelCallback() (err error) { + err, _ = v.call(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) + 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) + 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) + return +} diff --git a/callback_easyjson.go b/callback_easyjson.go new file mode 100644 index 0000000..ca82f6d --- /dev/null +++ b/callback_easyjson.go @@ -0,0 +1,396 @@ +// 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 easyjsonEcffa49DecodeGithubComMarkus621GoSmartcatClient(in *jlexer.Lexer, out *LastErrors) { + isTopLevel := in.IsStart() + if in.IsNull() { + in.Skip() + *out = nil + } else { + in.Delim('[') + if *out == nil { + if !in.IsDelim(']') { + *out = make(LastErrors, 0, 0) + } else { + *out = LastErrors{} + } + } else { + *out = (*out)[:0] + } + for !in.IsDelim(']') { + var v1 LastError + (v1).UnmarshalEasyJSON(in) + *out = append(*out, v1) + in.WantComma() + } + in.Delim(']') + } + if isTopLevel { + in.Consumed() + } +} +func easyjsonEcffa49EncodeGithubComMarkus621GoSmartcatClient(out *jwriter.Writer, in LastErrors) { + if in == nil && (out.Flags&jwriter.NilSliceAsEmpty) == 0 { + out.RawString("null") + } else { + out.RawByte('[') + for v2, v3 := range in { + if v2 > 0 { + out.RawByte(',') + } + (v3).MarshalEasyJSON(out) + } + out.RawByte(']') + } +} + +// MarshalJSON supports json.Marshaler interface +func (v LastErrors) MarshalJSON() ([]byte, error) { + w := jwriter.Writer{} + easyjsonEcffa49EncodeGithubComMarkus621GoSmartcatClient(&w, v) + return w.Buffer.BuildBytes(), w.Error +} + +// MarshalEasyJSON supports easyjson.Marshaler interface +func (v LastErrors) MarshalEasyJSON(w *jwriter.Writer) { + easyjsonEcffa49EncodeGithubComMarkus621GoSmartcatClient(w, v) +} + +// UnmarshalJSON supports json.Unmarshaler interface +func (v *LastErrors) UnmarshalJSON(data []byte) error { + r := jlexer.Lexer{Data: data} + easyjsonEcffa49DecodeGithubComMarkus621GoSmartcatClient(&r, v) + return r.Error() +} + +// UnmarshalEasyJSON supports easyjson.Unmarshaler interface +func (v *LastErrors) UnmarshalEasyJSON(l *jlexer.Lexer) { + easyjsonEcffa49DecodeGithubComMarkus621GoSmartcatClient(l, v) +} +func easyjsonEcffa49DecodeGithubComMarkus621GoSmartcatClient1(in *jlexer.Lexer, out *LastError) { + 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 "created": + out.Created = string(in.String()) + case "url": + out.URL = string(in.String()) + case "reason": + out.Reason = string(in.String()) + case "code": + out.Code = int(in.Int()) + case "content": + out.Content = string(in.String()) + case "sourceIds": + if in.IsNull() { + in.Skip() + out.SourceIds = nil + } else { + in.Delim('[') + if out.SourceIds == nil { + if !in.IsDelim(']') { + out.SourceIds = make([]string, 0, 4) + } else { + out.SourceIds = []string{} + } + } else { + out.SourceIds = (out.SourceIds)[:0] + } + for !in.IsDelim(']') { + var v4 string + v4 = string(in.String()) + out.SourceIds = append(out.SourceIds, v4) + in.WantComma() + } + in.Delim(']') + } + default: + in.SkipRecursive() + } + in.WantComma() + } + in.Delim('}') + if isTopLevel { + in.Consumed() + } +} +func easyjsonEcffa49EncodeGithubComMarkus621GoSmartcatClient1(out *jwriter.Writer, in LastError) { + out.RawByte('{') + first := true + _ = first + { + const prefix string = ",\"created\":" + out.RawString(prefix[1:]) + out.String(string(in.Created)) + } + { + const prefix string = ",\"url\":" + out.RawString(prefix) + out.String(string(in.URL)) + } + { + const prefix string = ",\"reason\":" + out.RawString(prefix) + out.String(string(in.Reason)) + } + { + const prefix string = ",\"code\":" + out.RawString(prefix) + out.Int(int(in.Code)) + } + { + const prefix string = ",\"content\":" + out.RawString(prefix) + out.String(string(in.Content)) + } + { + const prefix string = ",\"sourceIds\":" + out.RawString(prefix) + if in.SourceIds == nil && (out.Flags&jwriter.NilSliceAsEmpty) == 0 { + out.RawString("null") + } else { + out.RawByte('[') + for v5, v6 := range in.SourceIds { + if v5 > 0 { + out.RawByte(',') + } + out.String(string(v6)) + } + out.RawByte(']') + } + } + out.RawByte('}') +} + +// MarshalJSON supports json.Marshaler interface +func (v LastError) MarshalJSON() ([]byte, error) { + w := jwriter.Writer{} + easyjsonEcffa49EncodeGithubComMarkus621GoSmartcatClient1(&w, v) + return w.Buffer.BuildBytes(), w.Error +} + +// MarshalEasyJSON supports easyjson.Marshaler interface +func (v LastError) MarshalEasyJSON(w *jwriter.Writer) { + easyjsonEcffa49EncodeGithubComMarkus621GoSmartcatClient1(w, v) +} + +// UnmarshalJSON supports json.Unmarshaler interface +func (v *LastError) UnmarshalJSON(data []byte) error { + r := jlexer.Lexer{Data: data} + easyjsonEcffa49DecodeGithubComMarkus621GoSmartcatClient1(&r, v) + return r.Error() +} + +// UnmarshalEasyJSON supports easyjson.Unmarshaler interface +func (v *LastError) UnmarshalEasyJSON(l *jlexer.Lexer) { + easyjsonEcffa49DecodeGithubComMarkus621GoSmartcatClient1(l, v) +} +func easyjsonEcffa49DecodeGithubComMarkus621GoSmartcatClient2(in *jlexer.Lexer, out *Callback) { + 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 "url": + out.URL = string(in.String()) + case "additionalHeaders": + if in.IsNull() { + in.Skip() + out.AdditionalHeaders = nil + } else { + in.Delim('[') + if out.AdditionalHeaders == nil { + if !in.IsDelim(']') { + out.AdditionalHeaders = make([]AdditionalHeader, 0, 2) + } else { + out.AdditionalHeaders = []AdditionalHeader{} + } + } else { + out.AdditionalHeaders = (out.AdditionalHeaders)[:0] + } + for !in.IsDelim(']') { + var v7 AdditionalHeader + (v7).UnmarshalEasyJSON(in) + out.AdditionalHeaders = append(out.AdditionalHeaders, v7) + in.WantComma() + } + in.Delim(']') + } + default: + in.SkipRecursive() + } + in.WantComma() + } + in.Delim('}') + if isTopLevel { + in.Consumed() + } +} +func easyjsonEcffa49EncodeGithubComMarkus621GoSmartcatClient2(out *jwriter.Writer, in Callback) { + out.RawByte('{') + first := true + _ = first + { + const prefix string = ",\"url\":" + out.RawString(prefix[1:]) + out.String(string(in.URL)) + } + { + const prefix string = ",\"additionalHeaders\":" + out.RawString(prefix) + if in.AdditionalHeaders == nil && (out.Flags&jwriter.NilSliceAsEmpty) == 0 { + out.RawString("null") + } else { + out.RawByte('[') + for v8, v9 := range in.AdditionalHeaders { + if v8 > 0 { + out.RawByte(',') + } + (v9).MarshalEasyJSON(out) + } + out.RawByte(']') + } + } + out.RawByte('}') +} + +// MarshalJSON supports json.Marshaler interface +func (v Callback) MarshalJSON() ([]byte, error) { + w := jwriter.Writer{} + easyjsonEcffa49EncodeGithubComMarkus621GoSmartcatClient2(&w, v) + return w.Buffer.BuildBytes(), w.Error +} + +// MarshalEasyJSON supports easyjson.Marshaler interface +func (v Callback) MarshalEasyJSON(w *jwriter.Writer) { + easyjsonEcffa49EncodeGithubComMarkus621GoSmartcatClient2(w, v) +} + +// UnmarshalJSON supports json.Unmarshaler interface +func (v *Callback) UnmarshalJSON(data []byte) error { + r := jlexer.Lexer{Data: data} + easyjsonEcffa49DecodeGithubComMarkus621GoSmartcatClient2(&r, v) + return r.Error() +} + +// UnmarshalEasyJSON supports easyjson.Unmarshaler interface +func (v *Callback) UnmarshalEasyJSON(l *jlexer.Lexer) { + easyjsonEcffa49DecodeGithubComMarkus621GoSmartcatClient2(l, v) +} +func easyjsonEcffa49DecodeGithubComMarkus621GoSmartcatClient3(in *jlexer.Lexer, out *AdditionalHeader) { + 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 "name": + out.Name = string(in.String()) + case "value": + out.Value = string(in.String()) + default: + in.SkipRecursive() + } + in.WantComma() + } + in.Delim('}') + if isTopLevel { + in.Consumed() + } +} +func easyjsonEcffa49EncodeGithubComMarkus621GoSmartcatClient3(out *jwriter.Writer, in AdditionalHeader) { + out.RawByte('{') + first := true + _ = first + { + const prefix string = ",\"name\":" + out.RawString(prefix[1:]) + out.String(string(in.Name)) + } + { + const prefix string = ",\"value\":" + out.RawString(prefix) + out.String(string(in.Value)) + } + out.RawByte('}') +} + +// MarshalJSON supports json.Marshaler interface +func (v AdditionalHeader) MarshalJSON() ([]byte, error) { + w := jwriter.Writer{} + easyjsonEcffa49EncodeGithubComMarkus621GoSmartcatClient3(&w, v) + return w.Buffer.BuildBytes(), w.Error +} + +// MarshalEasyJSON supports easyjson.Marshaler interface +func (v AdditionalHeader) MarshalEasyJSON(w *jwriter.Writer) { + easyjsonEcffa49EncodeGithubComMarkus621GoSmartcatClient3(w, v) +} + +// UnmarshalJSON supports json.Unmarshaler interface +func (v *AdditionalHeader) UnmarshalJSON(data []byte) error { + r := jlexer.Lexer{Data: data} + easyjsonEcffa49DecodeGithubComMarkus621GoSmartcatClient3(&r, v) + return r.Error() +} + +// UnmarshalEasyJSON supports easyjson.Unmarshaler interface +func (v *AdditionalHeader) UnmarshalEasyJSON(l *jlexer.Lexer) { + easyjsonEcffa49DecodeGithubComMarkus621GoSmartcatClient3(l, v) +} diff --git a/client.go b/client.go index 2e92fe5..a1d09b7 100644 --- a/client.go +++ b/client.go @@ -63,7 +63,7 @@ func (v *Client) call(method, path string, req json.Marshaler, resp json.Unmarsh ) switch method { - case http.MethodGet, http.MethodHead, http.MethodConnect, http.MethodOptions: + case http.MethodGet, http.MethodHead, http.MethodConnect, http.MethodOptions, http.MethodDelete: body = nil default: body, err = req.MarshalJSON() @@ -89,36 +89,33 @@ func (v *Client) call(method, path string, req json.Marshaler, resp json.Unmarsh } code := cresp.StatusCode - if code == 200 { + switch code { + case 200: body, err = v.readBody(cresp.Body, resp) - v.writeDebug(code, path, body, err) - switch err { - case nil: - return nil, code - case io.EOF: - return errors.New(cresp.Status), code - default: - return errors.Wrap(err, "unmarshal response"), code - } - } - if code >= 400 && code < 500 { + case 204: + case 404: + body, err = nil, errors.New(cresp.Status) + case 401, 403: msg := ErrorResponse{} body, err = v.readBody(cresp.Body, &msg) - v.writeDebug(code, path, body, err) - switch err { - case nil: - return msg, code - case io.EOF: - return errors.New(cresp.Status), code - default: - return errors.Wrap(err, "unmarshal error response"), code + default: + var raw json.RawMessage + body, err = v.readBody(cresp.Body, &raw) + if err == nil { + err = ErrUnknown } } - var raw json.RawMessage - body, err = v.readBody(cresp.Body, &raw) - v.writeDebug(code, path, body, err) - return ErrUnknown, code + v.writeDebug(code, method, path, body, err) + + switch err { + case nil: + return nil, code + case io.EOF: + return errors.New(cresp.Status), code + default: + return errors.Wrap(err, "unmarshal response"), code + } } func (v *Client) readBody(rc io.ReadCloser, resp json.Unmarshaler) (b []byte, err error) { @@ -130,8 +127,8 @@ func (v *Client) readBody(rc io.ReadCloser, resp json.Unmarshaler) (b []byte, er return } -func (v *Client) writeDebug(code int, url string, body []byte, err error) { +func (v *Client) writeDebug(code int, method, path string, body []byte, err error) { if v.debug { - fmt.Fprintf(v.writer, "[%d] %s err: %+v raw:%s \n", code, url, err, body) + fmt.Fprintf(v.writer, "[%d] %s:%s err: %+v raw:%s \n", code, method, path, err, body) } } diff --git a/example/main.go b/example/main.go index 7901a76..3376fd4 100644 --- a/example/main.go +++ b/example/main.go @@ -1,12 +1,12 @@ package main import ( - "fmt" "os" cli "github.com/markus621/go-smartcat-client" ) +//nolint: errcheck func main() { conf := cli.Config{ @@ -18,17 +18,15 @@ func main() { client := cli.NewClient(conf) client.Debug(true, os.Stdout) - acc, err := client.Account() - if err != nil { - panic(err) - } - fmt.Println("==> ", acc) - - mte, err := client.AccountMTEngines() - if err != nil { - panic(err) - } - - fmt.Println("==> ", mte) - + _, _ = 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) }