NewRequest returns an http.Request with information for the Exercism API.
(method, url string, body io.Reader)
| 39 | |
| 40 | // NewRequest returns an http.Request with information for the Exercism API. |
| 41 | func (c *Client) NewRequest(method, url string, body io.Reader) (*http.Request, error) { |
| 42 | if c.Client == nil { |
| 43 | c.Client = HTTPClient |
| 44 | } |
| 45 | |
| 46 | req, err := http.NewRequest(method, url, body) |
| 47 | if err != nil { |
| 48 | return nil, err |
| 49 | } |
| 50 | |
| 51 | req.Header.Set("User-Agent", UserAgent) |
| 52 | if c.ContentType == "" { |
| 53 | req.Header.Set("Content-Type", "application/json") |
| 54 | } else { |
| 55 | req.Header.Set("Content-Type", c.ContentType) |
| 56 | } |
| 57 | if c.Token != "" { |
| 58 | req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", c.Token)) |
| 59 | } |
| 60 | |
| 61 | return req, nil |
| 62 | } |
| 63 | |
| 64 | // Do performs an http.Request and optionally parses the response body into the given interface. |
| 65 | func (c *Client) Do(req *http.Request) (*http.Response, error) { |
no outgoing calls