92 } |
96 } |
93 |
97 |
94 // BuildResponse builds the response struct. |
98 // BuildResponse builds the response struct. |
95 func BuildResponse(res *http.Response) (*Response, error) { |
99 func BuildResponse(res *http.Response) (*Response, error) { |
96 body, err := ioutil.ReadAll(res.Body) |
100 body, err := ioutil.ReadAll(res.Body) |
97 if err != nil { |
|
98 return nil, err |
|
99 } |
|
100 defer func() { |
|
101 if err := res.Body.Close(); err != nil { |
|
102 return // maybe log in the future |
|
103 } |
|
104 }() |
|
105 response := Response{ |
101 response := Response{ |
106 StatusCode: res.StatusCode, |
102 StatusCode: res.StatusCode, |
107 Body: string(body), |
103 Body: string(body), |
108 Headers: res.Header, |
104 Headers: res.Header, |
109 } |
105 } |
110 return &response, nil |
106 res.Body.Close() // nolint |
|
107 return &response, err |
111 } |
108 } |
112 |
109 |
113 // API supports old implementation (deprecated) |
110 // Deprecated: API supports old implementation |
114 func API(request Request) (*Response, error) { |
111 func API(request Request) (*Response, error) { |
115 return Send(request) |
112 return Send(request) |
116 } |
113 } |
117 |
114 |
118 // Send uses the DefaultClient to send your request |
115 // Send uses the DefaultClient to send your request |
119 func Send(request Request) (*Response, error) { |
116 func Send(request Request) (*Response, error) { |
120 return DefaultClient.Send(request) |
117 return SendWithContext(context.Background(), request) |
|
118 } |
|
119 |
|
120 // SendWithContext uses the DefaultClient to send your request with the provided context. |
|
121 func SendWithContext(ctx context.Context, request Request) (*Response, error) { |
|
122 return DefaultClient.SendWithContext(ctx, request) |
121 } |
123 } |
122 |
124 |
123 // The following functions enable the ability to define a |
125 // The following functions enable the ability to define a |
124 // custom HTTP Client |
126 // custom HTTP Client |
125 |
127 |
126 // MakeRequest makes the API call. |
128 // MakeRequest makes the API call. |
127 func (c *Client) MakeRequest(req *http.Request) (*http.Response, error) { |
129 func (c *Client) MakeRequest(req *http.Request) (*http.Response, error) { |
128 return c.HTTPClient.Do(req) |
130 return c.HTTPClient.Do(req) |
129 } |
131 } |
130 |
132 |
131 // API supports old implementation (deprecated) |
133 // Deprecated: API supports old implementation |
132 func (c *Client) API(request Request) (*Response, error) { |
134 func (c *Client) API(request Request) (*Response, error) { |
133 return c.Send(request) |
135 return c.Send(request) |
134 } |
136 } |
135 |
137 |
136 // Send will build your request, make the request, and build your response. |
138 // Send will build your request, make the request, and build your response. |
137 func (c *Client) Send(request Request) (*Response, error) { |
139 func (c *Client) Send(request Request) (*Response, error) { |
|
140 return c.SendWithContext(context.Background(), request) |
|
141 } |
|
142 |
|
143 // SendWithContext will build your request passing in the provided context, make the request, and build your response. |
|
144 func (c *Client) SendWithContext(ctx context.Context, request Request) (*Response, error) { |
138 // Build the HTTP request object. |
145 // Build the HTTP request object. |
139 req, err := BuildRequestObject(request) |
146 req, err := BuildRequestObject(request) |
140 if err != nil { |
147 if err != nil { |
141 return nil, err |
148 return nil, err |
142 } |
149 } |
|
150 // Pass in the user provided context |
|
151 req = req.WithContext(ctx) |
143 |
152 |
144 // Build the HTTP client and make the request. |
153 // Build the HTTP client and make the request. |
145 res, err := c.MakeRequest(req) |
154 res, err := c.MakeRequest(req) |
146 if err != nil { |
155 if err != nil { |
147 return nil, err |
156 return nil, err |
148 } |
157 } |
149 |
158 |
150 // Build Response object. |
159 // Build Response object. |
151 response, err := BuildResponse(res) |
160 return BuildResponse(res) |
152 if err != nil { |
|
153 return nil, err |
|
154 } |
|
155 |
|
156 return response, nil |
|
157 } |
161 } |