newRequest returns a new HTTP request but adds the current user's API key and sets the accept & content type headers to use JSON.
(ctx context.Context, method, url string, body io.Reader)
| 36 | // newRequest returns a new HTTP request but adds the current user's API key |
| 37 | // and sets the accept & content type headers to use JSON. |
| 38 | func (c *Client) newRequest(ctx context.Context, method, url string, body io.Reader) (*http.Request, error) { |
| 39 | // Build new request with base URL. |
| 40 | req, err := http.NewRequest(method, c.URL+url, body) |
| 41 | if err != nil { |
| 42 | return nil, err |
| 43 | } |
| 44 | |
| 45 | // Set API key in header. |
| 46 | if user := wtf.UserFromContext(ctx); user != nil && user.APIKey != "" { |
| 47 | req.Header.Set("Authorization", "Bearer "+user.APIKey) |
| 48 | } |
| 49 | |
| 50 | // Default to JSON format. |
| 51 | req.Header.Set("Accept", "application/json") |
| 52 | req.Header.Set("Content-type", "application/json") |
| 53 | |
| 54 | return req, nil |
| 55 | } |
| 56 | |
| 57 | // SessionCookieName is the name of the cookie used to store the session. |
| 58 | const SessionCookieName = "session" |
no test coverage detected