Skip to content

Commit

Permalink
we have a context, so use it
Browse files Browse the repository at this point in the history
  • Loading branch information
mildwonkey committed Oct 30, 2024
1 parent 687ac14 commit 98022e9
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
5 changes: 3 additions & 2 deletions src/pkg/domains/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ func (a ApiDomain) makeRequests(ctx context.Context) (types.DomainResources, err
// This isn't likely to be nil in real usage, since CreateApiDomain
// parses and mutates specs.
defaultOpts = new(ApiOpts)
defaultOpts.timeout = &defaultTimeout
} else {
defaultOpts = a.Spec.Options
}
Expand All @@ -33,10 +34,10 @@ func (a ApiDomain) makeRequests(ctx context.Context) (types.DomainResources, err
var responseType interface{}
var err error
if request.Options == nil {
responseType, err = doHTTPReq(defaultClient, *request.reqURL, defaultOpts.Headers, request.reqParameters, responseType)
responseType, err = doHTTPReq(ctx, defaultClient, *request.reqURL, defaultOpts.Headers, request.reqParameters, responseType)
} else {
client := clientFromOpts(request.Options)
responseType, err = doHTTPReq(client, *request.reqURL, request.Options.Headers, request.reqParameters, responseType)
responseType, err = doHTTPReq(ctx, client, *request.reqURL, request.Options.Headers, request.reqParameters, responseType)
}
if err != nil {
return nil, err
Expand Down
6 changes: 3 additions & 3 deletions src/pkg/domains/api/http_request.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package api

import (
"context"
"encoding/json"
"fmt"
"io"
Expand All @@ -9,7 +10,7 @@ import (
"strings"
)

func doHTTPReq[T any](client http.Client, url url.URL, headers map[string]string, queryParameters url.Values, respTy T) (T, error) {
func doHTTPReq[T any](ctx context.Context, client http.Client, url url.URL, headers map[string]string, queryParameters url.Values, respTy T) (T, error) {
// append any query parameters.
q := url.Query()

Expand All @@ -20,11 +21,10 @@ func doHTTPReq[T any](client http.Client, url url.URL, headers map[string]string
// set the query to the encoded parameters
url.RawQuery = q.Encode()

req, err := http.NewRequest(http.MethodGet, url.String(), nil)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url.String(), nil)
if err != nil {
return respTy, err
}

// add each header to the request
for k, v := range headers {
req.Header.Set(k, v)
Expand Down

0 comments on commit 98022e9

Please sign in to comment.