-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalculator-service.go
47 lines (35 loc) · 955 Bytes
/
calculator-service.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package cdekapi
import (
"context"
"errors"
"fmt"
"time"
)
func (c *Client) Calculator(ctx context.Context, request CalculatorRequest) (CalculatorResponse, error) {
var ret CalculatorResponse
req := c.calculatorPrepare(&request)
resp, err := c.getRequest(ctx).
SetResult(&CalculatorResponse{}).
SetBody(req).
SetError(&ErrorApiV2{}).
Post(apiCalculator)
if err != nil {
return ret, err
}
if resp.StatusCode() > 399 {
return ret, fmt.Errorf("http error: %s", resp.Status())
}
if result, ok := resp.Result().(*CalculatorResponse); ok {
if len(result.Errors) > 0 {
return ret, errors.New(result.Errors[0].Message)
}
return *result, nil
}
return ret, errors.New("can't convert response to calculator response struct")
}
func (c Client) calculatorPrepare(request *CalculatorRequest) *CalculatorRequest {
if request.Date == "" {
request.Date = time.Now().Format("2006-01-02T15:04:05-0700")
}
return request
}