-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
122 lines (104 loc) · 2.66 KB
/
client.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// Copyright 2019 RetailNext, Inc.
//
// Licensed under the BSD 3-Clause License (the "License");
// you may not use this file except in compliance with the License.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package easypost
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"net/url"
)
var apiURL = "https://api.easypost.com/v2"
const (
trackerURL = "trackers"
addressURL = "addresses"
)
type Logger interface {
Printf(format string, v ...interface{})
}
type Client struct {
c http.Client
apiKey string
errorLogger Logger
}
func (c *Client) SetErrorLog(l Logger) {
c.errorLogger = l
}
func (c Client) errorf(f string, attr ...interface{}) {
if c.errorLogger != nil {
c.errorLogger.Printf(f, attr)
}
}
func NewClient(apiKey string) *Client {
return &Client{
c: http.Client{},
apiKey: apiKey,
}
}
func (c *Client) post(objectURL string, parameters url.Values) ([]byte, error) {
if c == nil {
panic("client is not initialized")
}
rawURL, err := url.ParseRequestURI(fmt.Sprintf("%s/%s?%s", apiURL, objectURL, parameters.Encode()))
if err != nil {
panic(err)
}
r, err := http.NewRequest("POST", rawURL.String(), nil)
if err != nil {
panic(err)
}
r.SetBasicAuth(c.apiKey, "")
response, err := c.c.Do(r)
if err != nil {
return nil, fmt.Errorf("error sending request: %s", err)
}
defer response.Body.Close()
if response.StatusCode >= 500 {
return nil, fmt.Errorf("request can't be processed by server")
}
if response.StatusCode == http.StatusOK || response.StatusCode == http.StatusCreated {
body, err := ioutil.ReadAll(response.Body)
if err != nil {
err := fmt.Errorf("error read response: %s", err)
c.errorf("%s\n", err)
return nil, err
}
return body, nil
}
err = c.processErrorResponse(response)
if err != nil {
c.errorf("%s\n", err)
}
return nil, err
}
func (c Client) processErrorResponse(response *http.Response) error {
switch response.StatusCode {
case http.StatusUnauthorized:
return unauthorizedError
case http.StatusPaymentRequired:
return paymentError
case http.StatusNotFound:
return errors.New("resource is not reachable")
}
b, err := ioutil.ReadAll(response.Body)
if err != nil {
return fmt.Errorf("error reading error reponse: %s", err)
}
c.errorf("error response body: %s\n", b)
errorResponse := ErrorResponse{}
if err := json.Unmarshal(b, &errorResponse); err != nil {
return fmt.Errorf("error parse not success response: %s", err)
}
errorMessage := errorResponse.Error
return ProcessingError{
msg: errorMessage.Message,
code: errorMessage.Code,
details: errorMessage.FieldErrors,
}
}