-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathclient.go
More file actions
129 lines (105 loc) · 2.92 KB
/
client.go
File metadata and controls
129 lines (105 loc) · 2.92 KB
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
123
124
125
126
127
128
129
package requestgen
import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/http"
"net/url"
"time"
)
type AuthenticatedRequestBuilder interface {
// NewAuthenticatedRequest builds up the http request for authentication-required endpoints
NewAuthenticatedRequest(
ctx context.Context, method, refURL string, params url.Values, payload interface{},
) (*http.Request, error)
}
// APIClient defines the request builder method and request method for the API service
type APIClient interface {
// NewRequest builds up the http request for public endpoints
NewRequest(
ctx context.Context, method, refURL string, params url.Values, payload interface{},
) (*http.Request, error)
// SendRequest sends the request object to the api gateway
SendRequest(req *http.Request) (*Response, error)
}
type AuthenticatedAPIClient interface {
APIClient
AuthenticatedRequestBuilder
}
const defaultHTTPTimeout = time.Second * 30
var defaultHttpClient = &http.Client{
Timeout: defaultHTTPTimeout,
}
// BaseAPIClient provides the basic public API transport and the request builder.
// Usage
/*
client := &BaseAPIClient{
BaseURL: u,
HttpClient: defaultHttpClient,
}
*/
type BaseAPIClient struct {
BaseURL *url.URL
HttpClient *http.Client
}
// NewRequest create new API request. Relative url can be provided in refURL.
func (c *BaseAPIClient) NewRequest(
ctx context.Context, method, refPath string, params url.Values, payload interface{},
) (*http.Request, error) {
body, err := castPayload(payload)
if err != nil {
return nil, err
}
ref, err := url.Parse(refPath)
if err != nil {
return nil, err
}
pathURL := c.BaseURL.ResolveReference(ref)
if params != nil {
pathURL.RawQuery = params.Encode()
}
return http.NewRequestWithContext(ctx, method, pathURL.String(), bytes.NewReader(body))
}
// SendRequest sends the request to the API server and handle the response
func (c *BaseAPIClient) SendRequest(req *http.Request) (*Response, error) {
if c.HttpClient == nil {
c.HttpClient = defaultHttpClient
}
resp, err := c.HttpClient.Do(req)
if err != nil {
return nil, err
}
// newResponse reads the response body and return a new Response object
response, err := NewResponse(resp)
if err != nil {
return response, err
}
// Check error, if there is an error, return the ErrorResponse struct type
if response.IsError() {
return response, &ErrResponse{Response: response, Body: response.Body, Request: req}
}
return response, nil
}
func castPayload(payload interface{}) ([]byte, error) {
if payload != nil {
switch v := payload.(type) {
case string:
return []byte(v), nil
case []byte:
return v, nil
default:
body, err := json.Marshal(v)
return body, err
}
}
return nil, nil
}
type ErrResponse struct {
*Response
Request *http.Request
Body []byte
}
func (e *ErrResponse) Error() string {
return fmt.Sprintf("request failed with status code: %d, body: %q", e.Response.StatusCode, string(e.Body))
}