Request sends an HTTP request and returns an HTTP response. It unmarshals the response body to the given interface.
(
res interface{},
method string,
path string,
body interface{},
urlParams map[string]string,
)
| 43 | // Request sends an HTTP request and returns an HTTP response. |
| 44 | // It unmarshals the response body to the given interface. |
| 45 | func (c *Client) request( |
| 46 | res interface{}, |
| 47 | method string, |
| 48 | path string, |
| 49 | body interface{}, |
| 50 | urlParams map[string]string, |
| 51 | ) error { |
| 52 | r, err := c.buildRequest(method, path, body, urlParams) |
| 53 | if err != nil { |
| 54 | return err |
| 55 | } |
| 56 | |
| 57 | resp, err := c.client.Do(r) |
| 58 | if err != nil { |
| 59 | return err |
| 60 | } |
| 61 | |
| 62 | if resp.StatusCode >= 400 { |
| 63 | var errResp ErrResponse |
| 64 | if err := unmarshalTo(resp, &errResp); err != nil { |
| 65 | return err |
| 66 | } |
| 67 | |
| 68 | if errResp.Err.Errors != nil { |
| 69 | var errs []string |
| 70 | for _, e := range errResp.Err.Errors { |
| 71 | errs = append(errs, e.Message) |
| 72 | } |
| 73 | return fmt.Errorf("[%s] %s", errResp.Err.Code, errs) |
| 74 | } |
| 75 | |
| 76 | // Message might be empty |
| 77 | if errResp.Err.Message == "" { |
| 78 | return errors.New(errResp.Err.Code) |
| 79 | } else { |
| 80 | return fmt.Errorf("[%s] %s", errResp.Err.Code, errResp.Err.Message) |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | if res != nil { |
| 85 | if err := unmarshalTo(resp, res); err != nil { |
| 86 | return err |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | return nil |
| 91 | } |
| 92 | |
| 93 | // buildRequestWithoutBody builds an HTTP request without a body. |
| 94 | func (c *Client) buildRequestWithoutBody(method, url string) (*http.Request, error) { |