newRequest creates an API request. A relative URL can be provided in uri, in which case it is resolved relative to the BaseURL of the Client. URI's should always be specified without a preceding slash.
(ctx context.Context, method, uri string, body any)
| 55 | // in which case it is resolved relative to the BaseURL of the Client. |
| 56 | // URI's should always be specified without a preceding slash. |
| 57 | func (client *Client) newRequest(ctx context.Context, method, uri string, body any) (*http.Request, error) { |
| 58 | var buf io.ReadWriter |
| 59 | if body != nil { |
| 60 | buf = &bytes.Buffer{} |
| 61 | enc := json.NewEncoder(buf) |
| 62 | enc.SetEscapeHTML(false) |
| 63 | err := enc.Encode(body) |
| 64 | if err != nil { |
| 65 | return nil, err |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | req, err := http.NewRequestWithContext(ctx, method, client.baseURL+uri, buf) |
| 70 | if err != nil { |
| 71 | return nil, err |
| 72 | } |
| 73 | |
| 74 | req.Header.Set("Content-Type", "application/json") |
| 75 | req.Header.Set("Accept", "application/json") |
| 76 | req.Header.Set("Authorization", "Bot "+client.botToken) |
| 77 | |
| 78 | return req, nil |
| 79 | } |
| 80 | |
| 81 | // do carries out an HTTP request and returns a Response |
| 82 | func (client *Client) do(req *http.Request) (*Response, error) { |
no test coverage detected