|
| 1 | +package service |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "io" |
| 8 | + "io/ioutil" |
| 9 | + "net/http" |
| 10 | + "net/url" |
| 11 | +) |
| 12 | + |
| 13 | +// Client defines the interface exposed by our API. |
| 14 | +type Client interface { |
| 15 | + AddContact(contact AddContactRequest) (*Contact, error) |
| 16 | + GetContactByEmail(email string) (*Contact, error) |
| 17 | +} |
| 18 | + |
| 19 | +// ErrorResponse is returned by our service when an error occurs. |
| 20 | +type ErrorResponse struct { |
| 21 | + StatusCode int `json:"status_code"` |
| 22 | + Message string `json:"message"` |
| 23 | +} |
| 24 | + |
| 25 | +func (e ErrorResponse) Error() string { |
| 26 | + return fmt.Sprintf("%v: %v", e.StatusCode, e.Message) |
| 27 | +} |
| 28 | + |
| 29 | +// NewClient creates a Client that accesses a service at the given base URL. |
| 30 | +func NewClient(baseURL string) Client { |
| 31 | + httpClient := http.DefaultClient |
| 32 | + return &DefaultClient{ |
| 33 | + http: httpClient, |
| 34 | + BaseURL: baseURL, |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +// ===== DefaultClient ================================================================================================= |
| 39 | + |
| 40 | +// DefaultClient provides an implementation of the Client interface. |
| 41 | +type DefaultClient struct { |
| 42 | + http *http.Client |
| 43 | + BaseURL string |
| 44 | +} |
| 45 | + |
| 46 | +// performRequestMethod constructs a request and uses `performRequest` to execute it. |
| 47 | +func (c *DefaultClient) performRequestMethod(method string, path string, headers map[string]string, data interface{}, response interface{}) error { |
| 48 | + |
| 49 | + req, err := c.newRequest(method, path, headers, data) |
| 50 | + if err != nil { |
| 51 | + return err |
| 52 | + } |
| 53 | + |
| 54 | + return c.performRequest(req, response) |
| 55 | +} |
| 56 | + |
| 57 | +// performRequest executes the given request, and uses `response` to parse the JSON response. |
| 58 | +func (c *DefaultClient) performRequest(req *http.Request, response interface{}) error { |
| 59 | + // perform the request |
| 60 | + httpResponse, err := c.http.Do(req) |
| 61 | + if err != nil { |
| 62 | + return err |
| 63 | + } |
| 64 | + |
| 65 | + defer httpResponse.Body.Close() |
| 66 | + |
| 67 | + // read the response |
| 68 | + var responseBody []byte |
| 69 | + if responseBody, err = ioutil.ReadAll(httpResponse.Body); err != nil { |
| 70 | + return err |
| 71 | + } |
| 72 | + |
| 73 | + if httpResponse.StatusCode >= 400 { |
| 74 | + contentTypeHeader := httpResponse.Header["Content-Type"] |
| 75 | + if len(contentTypeHeader) != 0 && contentTypeHeader[0] == "application/json" { |
| 76 | + var errResponse ErrorResponse |
| 77 | + err := json.Unmarshal(responseBody, &errResponse) |
| 78 | + if err == nil { |
| 79 | + return errResponse |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + return &ErrorResponse{ |
| 84 | + StatusCode: httpResponse.StatusCode, |
| 85 | + Message: httpResponse.Status, |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + // map the response to an object value |
| 90 | + if err := json.Unmarshal(responseBody, &response); err != nil { |
| 91 | + return err |
| 92 | + } |
| 93 | + |
| 94 | + return nil |
| 95 | +} |
| 96 | + |
| 97 | +// newRequest builds a new request using the given parameters. |
| 98 | +func (c *DefaultClient) newRequest(method string, path string, headers map[string]string, data interface{}) (*http.Request, error) { |
| 99 | + |
| 100 | + // Construct request body |
| 101 | + var body io.Reader |
| 102 | + if data != nil { |
| 103 | + requestJSON, err := json.Marshal(data) |
| 104 | + if err != nil { |
| 105 | + return nil, err |
| 106 | + } |
| 107 | + |
| 108 | + body = bytes.NewReader(requestJSON) |
| 109 | + } |
| 110 | + |
| 111 | + // construct the request |
| 112 | + req, err := http.NewRequest(method, c.BaseURL+path, body) |
| 113 | + if err != nil { |
| 114 | + return nil, err |
| 115 | + } |
| 116 | + |
| 117 | + if body != nil { |
| 118 | + req.Header.Set("Content-Type", "application/json") |
| 119 | + } |
| 120 | + |
| 121 | + for k, v := range headers { |
| 122 | + req.Header.Set(k, v) |
| 123 | + } |
| 124 | + |
| 125 | + return req, nil |
| 126 | +} |
| 127 | + |
| 128 | + |
| 129 | + |
| 130 | +// ----- Add Contact --------------------------------------------------------------------------------------------------- |
| 131 | + |
| 132 | +type AddContactRequest struct { |
| 133 | + Email string `json:"email"` |
| 134 | + Name string `json:"name"` |
| 135 | +} |
| 136 | + |
| 137 | +type ContactResponse struct { |
| 138 | + Contact *Contact `json:"contact"` |
| 139 | +} |
| 140 | + |
| 141 | +func (c *DefaultClient) AddContact(contact AddContactRequest) (*Contact, error) { |
| 142 | + var response ContactResponse |
| 143 | + err := c.performRequestMethod(http.MethodPost, "/contacts", nil, contact, &response) |
| 144 | + if err != nil { |
| 145 | + return nil, err |
| 146 | + } |
| 147 | + |
| 148 | + return response.Contact, nil |
| 149 | +} |
| 150 | + |
| 151 | +func (c *DefaultClient) GetContactByEmail(email string) (*Contact, error) { |
| 152 | + var response ContactResponse |
| 153 | + var path = fmt.Sprintf("/contacts/%v", url.QueryEscape(email)) |
| 154 | + err := c.performRequestMethod(http.MethodGet, path, nil, nil, &response) |
| 155 | + if err != nil { |
| 156 | + return nil, err |
| 157 | + } |
| 158 | + |
| 159 | + return response.Contact, nil |
| 160 | +} |
0 commit comments