From 4ee66c3d3dd0fdd8b6685234cf0bde2ef04ece23 Mon Sep 17 00:00:00 2001 From: Abin Simon Date: Fri, 9 Dec 2022 10:29:17 +0530 Subject: [PATCH] Refactor http client creation for onedrive file download (#1736) ## Description This way we are able to use any middlewares added in both places. ## Type of change - [ ] :sunflower: Feature - [ ] :bug: Bugfix - [ ] :world_map: Documentation - [ ] :robot: Test - [ ] :computer: CI/Deployment - [x] :hamster: Trivial/Minor ## Issue(s) * # ## Test Plan - [ ] :muscle: Manual - [ ] :zap: Unit test - [x] :green_heart: E2E --- src/internal/connector/graph/service_helper.go | 11 +++++++++-- src/internal/connector/onedrive/item.go | 9 ++------- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/src/internal/connector/graph/service_helper.go b/src/internal/connector/graph/service_helper.go index 23fad0274..94700b96a 100644 --- a/src/internal/connector/graph/service_helper.go +++ b/src/internal/connector/graph/service_helper.go @@ -41,6 +41,14 @@ func CreateAdapter(tenant, client, secret string) (*msgraphsdk.GraphRequestAdapt return nil, errors.Wrap(err, "creating new AzureIdentityAuthentication") } + httpClient := CreateHTTPClient() + + return msgraphsdk.NewGraphRequestAdapterWithParseNodeFactoryAndSerializationWriterFactoryAndHttpClient( + auth, nil, nil, httpClient) +} + +// CreateHTTPClient creates the httpClient with middlewares and timeout configured +func CreateHTTPClient() *nethttp.Client { clientOptions := msgraphsdk.GetDefaultClientOptions() middlewares := msgraphgocore.GetDefaultMiddlewaresWithOptions(&clientOptions) @@ -52,8 +60,7 @@ func CreateAdapter(tenant, client, secret string) (*msgraphsdk.GraphRequestAdapt httpClient := msgraphgocore.GetDefaultClient(&clientOptions, middlewares...) httpClient.Timeout = time.Second * 90 - return msgraphsdk.NewGraphRequestAdapterWithParseNodeFactoryAndSerializationWriterFactoryAndHttpClient( - auth, nil, nil, httpClient) + return httpClient } // LoggingMiddleware can be used to log the http request sent by the graph client diff --git a/src/internal/connector/onedrive/item.go b/src/internal/connector/onedrive/item.go index e29a2ec9a..09cc2711c 100644 --- a/src/internal/connector/onedrive/item.go +++ b/src/internal/connector/onedrive/item.go @@ -5,8 +5,6 @@ import ( "io" "time" - msgraphsdk "github.com/microsoftgraph/msgraph-sdk-go" - msgraphgocore "github.com/microsoftgraph/msgraph-sdk-go-core" "github.com/microsoftgraph/msgraph-sdk-go/models" "github.com/pkg/errors" @@ -88,11 +86,8 @@ func driveItemReader( downloadURL := item.GetAdditionalData()[downloadURLKey].(*string) - clientOptions := msgraphsdk.GetDefaultClientOptions() - middlewares := msgraphgocore.GetDefaultMiddlewaresWithOptions(&clientOptions) - - httpClient := msgraphgocore.GetDefaultClient(&clientOptions, middlewares...) - httpClient.Timeout = 0 // need infinite timeout for pulling large files + httpClient := graph.CreateHTTPClient() + httpClient.Timeout = 0 // infinite timeout for pulling large files resp, err := httpClient.Get(*downloadURL) if err != nil {