From a9d20906c11cfe890c6304276047c9701b38764d Mon Sep 17 00:00:00 2001 From: HandsomeJack Date: Mon, 15 Apr 2024 10:55:19 +0200 Subject: [PATCH] refactor(oauth): use constructor for new client in comm.HTTPClient.JSONCall Parsing the url, then setting qv.Encode() manually to u.RawQuery feel like an overcomplicated approach of creating url for http request. Use the native constructor, which enforces ctx to be passed into the request, and do simple sprintf for url + query. --- apps/internal/oauth/ops/internal/comm/comm.go | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/apps/internal/oauth/ops/internal/comm/comm.go b/apps/internal/oauth/ops/internal/comm/comm.go index 7d9ec7cd..d62aac74 100644 --- a/apps/internal/oauth/ops/internal/comm/comm.go +++ b/apps/internal/oauth/ops/internal/comm/comm.go @@ -18,10 +18,11 @@ import ( "strings" "time" + "github.com/google/uuid" + "github.com/AzureAD/microsoft-authentication-library-for-go/apps/errors" customJSON "github.com/AzureAD/microsoft-authentication-library-for-go/apps/internal/json" "github.com/AzureAD/microsoft-authentication-library-for-go/apps/internal/version" - "github.com/google/uuid" ) // HTTPClient represents an HTTP client. @@ -70,15 +71,13 @@ func (c *Client) JSONCall(ctx context.Context, endpoint string, headers http.Hea unmarshal = customJSON.Unmarshal } - u, err := url.Parse(endpoint) + req, err := http.NewRequestWithContext(ctx, http.MethodGet, fmt.Sprintf("%s?%s", endpoint, qv.Encode()), nil) if err != nil { - return fmt.Errorf("could not parse path URL(%s): %w", endpoint, err) + return fmt.Errorf("could not create request: %w", err) } - u.RawQuery = qv.Encode() addStdHeaders(headers) - - req := &http.Request{Method: http.MethodGet, URL: u, Header: headers} + req.Header = headers if body != nil { // Note: In case your wondering why we are not gzip encoding....