RequestWithContext performs a REST request and returns the response for the caller to consume, rather than decoding it into a receiver as REST does. It exists for call sites that need access to the response itself: streaming bodies, non-JSON bodies, response headers, or the status code of a successf
(ctx context.Context, hostname string, method string, p string, body io.Reader, opts ...RequestOption)
| 219 | // Responses outside the 2xx range are returned as an HTTPError and their body is closed. On success |
| 220 | // the response is returned unread and the caller is responsible for closing its body. |
| 221 | func (c Client) RequestWithContext(ctx context.Context, hostname string, method string, p string, body io.Reader, opts ...RequestOption) (*http.Response, error) { |
| 222 | cfg := newRequestConfig(opts) |
| 223 | clientOpts := clientOptions(hostname, c.http.Transport) |
| 224 | maps.Copy(clientOpts.Headers, cfg.headers) |
| 225 | if cfg.noFollowRedirects { |
| 226 | clientOpts.CheckRedirect = func(req *http.Request, via []*http.Request) error { |
| 227 | return http.ErrUseLastResponse |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | restClient, err := ghAPI.NewRESTClient(clientOpts) |
| 232 | if err != nil { |
| 233 | return nil, err |
| 234 | } |
| 235 | |
| 236 | resp, err := restClient.RequestWithContext(ctx, method, p, body) |
| 237 | if err != nil { |
| 238 | return nil, handleRequestError(err, cfg.endpointScopes) |
| 239 | } |
| 240 | |
| 241 | return resp, nil |
| 242 | } |
| 243 | |
| 244 | // DoRequest sends a request that the caller has already built, and returns the response for the |
| 245 | // caller to consume. It applies the same error handling as Request. |