Skip to content

Commit

Permalink
fix lint
Browse files Browse the repository at this point in the history
  • Loading branch information
markus621 committed Nov 26, 2020
1 parent 1b0cc44 commit 9abcaab
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 15 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@
*.out

.idea/
.vscode/
coverage.txt
7 changes: 5 additions & 2 deletions account.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,30 @@ const (

//easyjson:json
type (
//Account information about your current account.
Account struct {
ID string `json:"id"`
Name string `json:"name"`
IsPersonal bool `json:"isPersonal"`
Type string `json:"type"`
}
//AccountMTEngine information about the machine translation engine
AccountMTEngine struct {
ID string `json:"id"`
Name string `json:"name"`
}
//AccountMTEngines list of available machine translation engines
AccountMTEngines []AccountMTEngine
)

//GetAccount Receiving the account details
func (v *Client) GetAccount() (out Account, err error) {
err, _ = v.call(http.MethodGet, uriAccount, nil, &out)
_, err = v.call(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)
_, err = v.call(http.MethodGet, uriAccountMTEngines, nil, &out)
return
}
2 changes: 2 additions & 0 deletions auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ const (
EAHostURL = `https://ea.smartcat.ai`
)

//Config client settings for connecting to the server
type Config struct {
AccountID string
AuthKey string
URL string
}

//AuthToken generating an authorization token
func (c Config) AuthToken() string {
return `Basic ` + base64.StdEncoding.EncodeToString([]byte(c.AccountID+`:`+c.AuthKey))
}
12 changes: 8 additions & 4 deletions callback.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,17 @@ const (

//easyjson:json
type (
//AdditionalHeader additional headers for transmission with a callback
AdditionalHeader struct {
Name string `json:"name"`
Value string `json:"value"`
}
//Callback callback settings
Callback struct {
URL string `json:"url"`
AdditionalHeaders []AdditionalHeader `json:"additionalHeaders"`
}
//LastError information about callback errors
LastError struct {
Created string `json:"created"`
URL string `json:"url"`
Expand All @@ -30,29 +33,30 @@ type (
Content string `json:"content"`
SourceIds []string `json:"sourceIds"`
}
//LastErrors error list
LastErrors []LastError
)

//DelCallback Resetting the configuration of notifications reception
func (v *Client) DelCallback() (err error) {
err, _ = v.call(http.MethodDelete, uriCallback, nil, nil)
_, 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)
_, 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)
_, 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)
_, err = v.call(http.MethodGet, fmt.Sprintf("%s?limit=%d", uriCallbackLastErrors, limit), nil, &out)
return
}
18 changes: 10 additions & 8 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const (
)

type (
//Client client connecting to the server
Client struct {
conf Config
cli *http.Client
Expand All @@ -44,19 +45,20 @@ func NewClient(c Config) *Client {
return NewCustomClient(c, cli)
}

//NewClient init client
//NewCustomClient init client
func NewCustomClient(c Config, cli *http.Client) *Client {
return &Client{
conf: c,
cli: cli,
}
}

//Debug enable logging of responses
func (v *Client) Debug(is bool, w io.Writer) {
v.debug, v.writer = is, w
}

func (v *Client) call(method, path string, req json.Marshaler, resp json.Unmarshaler) (error, int) {
func (v *Client) call(method, path string, req json.Marshaler, resp json.Unmarshaler) (int, error) {
var (
body []byte
err error
Expand All @@ -68,13 +70,13 @@ func (v *Client) call(method, path string, req json.Marshaler, resp json.Unmarsh
default:
body, err = req.MarshalJSON()
if err != nil {
return errors.Wrap(err, "marshal request"), 0
return 0, errors.Wrap(err, "marshal request")
}
}

creq, err := http.NewRequest(method, v.conf.URL+path, bytes.NewReader(body))
if err != nil {
return errors.Wrap(err, "create request"), 0
return 0, errors.Wrap(err, "create request")
}

creq.Header.Set("User-Agent", userAgent)
Expand All @@ -85,7 +87,7 @@ func (v *Client) call(method, path string, req json.Marshaler, resp json.Unmarsh

cresp, err := v.cli.Do(creq)
if err != nil {
return errors.Wrap(err, "make request"), 0
return 0, errors.Wrap(err, "make request")
}

code := cresp.StatusCode
Expand All @@ -110,11 +112,11 @@ func (v *Client) call(method, path string, req json.Marshaler, resp json.Unmarsh

switch err {
case nil:
return nil, code
return code, nil
case io.EOF:
return errors.New(cresp.Status), code
return code, errors.New(cresp.Status)
default:
return errors.Wrap(err, "unmarshal response"), code
return code, errors.Wrap(err, "unmarshal response")
}
}

Expand Down
4 changes: 3 additions & 1 deletion error.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,17 @@ import "github.com/pkg/errors"
//go:generate easyjson

var (
//ErrUnknown unknown error
ErrUnknown = errors.New("unknown error")
)

//ErrorResponse ...
//ErrorResponse model error response from the server
//easyjson:json
type ErrorResponse struct {
Message string `json:"Message"`
}

//Error error interface
func (v ErrorResponse) Error() string {
return v.Message
}

0 comments on commit 9abcaab

Please sign in to comment.