From bd373bbfd4b9adcef691456a64a930767d850167 Mon Sep 17 00:00:00 2001 From: ashmrtn Date: Wed, 30 Nov 2022 12:24:31 -0800 Subject: [PATCH] Custom function to send get requests on delta queries (#1643) ## Description The current version of graph SDK (v0.40.0) Corso uses does not support adding query parameters to delta requests on exchange mail folder messages or contact folder contacts. This reimplements the logic from a newer version of graph SDK so that we can add those parameters. Much of the code is lifted from the updated version of the SDK, with some combining of types that have the same fields. ## Type of change - [x] :sunflower: Feature - [ ] :bug: Bugfix - [ ] :world_map: Documentation - [ ] :robot: Test - [ ] :computer: CI/Deployment - [ ] :hamster: Trivial/Minor ## Issue(s) * #1612 ## Test Plan - [x] :muscle: Manual - [ ] :zap: Unit test - [ ] :green_heart: E2E --- src/internal/connector/exchange/delta_get.go | 123 ++++++++++++++++++ .../connector/exchange/query_options.go | 19 +++ 2 files changed, 142 insertions(+) create mode 100644 src/internal/connector/exchange/delta_get.go diff --git a/src/internal/connector/exchange/delta_get.go b/src/internal/connector/exchange/delta_get.go new file mode 100644 index 000000000..24c883617 --- /dev/null +++ b/src/internal/connector/exchange/delta_get.go @@ -0,0 +1,123 @@ +package exchange + +import ( + "context" + + abs "github.com/microsoft/kiota-abstractions-go" + "github.com/microsoftgraph/msgraph-sdk-go/models/odataerrors" + mscontactdelta "github.com/microsoftgraph/msgraph-sdk-go/users/item/contactfolders/item/contacts/delta" + msmaildelta "github.com/microsoftgraph/msgraph-sdk-go/users/item/mailfolders/item/messages/delta" +) + +//nolint:lll +const ( + mailURLTemplate = "{+baseurl}/users/{user%2Did}/mailFolders/{mailFolder%2Did}/messages/microsoft.graph.delta(){?%24top,%24skip,%24search,%24filter,%24count,%24select,%24orderby}" + contactsURLTemplate = "{+baseurl}/users/{user%2Did}/contactFolders/{contactFolder%2Did}/contacts/microsoft.graph.delta(){?%24top,%24skip,%24search,%24filter,%24count,%24select,%24orderby}" +) + +// The following functions are based off the code in v0.41.0 of msgraph-sdk-go +// for sending delta requests with query parameters. + +func createGetRequestInformationWithRequestConfiguration( + baseRequestInfoFunc func() (*abs.RequestInformation, error), + requestConfig *DeltaRequestBuilderGetRequestConfiguration, + template string, +) (*abs.RequestInformation, error) { + requestInfo, err := baseRequestInfoFunc() + if err != nil { + return nil, err + } + + requestInfo.UrlTemplate = template + + if requestConfig != nil { + if requestConfig.QueryParameters != nil { + requestInfo.AddQueryParameters(*(requestConfig.QueryParameters)) + } + + requestInfo.AddRequestHeaders(requestConfig.Headers) + requestInfo.AddRequestOptions(requestConfig.Options) + } + + return requestInfo, nil +} + +//nolint:deadcode +func sendMessagesDeltaGet( + ctx context.Context, + m *msmaildelta.DeltaRequestBuilder, + requestConfiguration *DeltaRequestBuilderGetRequestConfiguration, + adapter abs.RequestAdapter, +) (msmaildelta.DeltaResponseable, error) { + requestInfo, err := createGetRequestInformationWithRequestConfiguration( + func() (*abs.RequestInformation, error) { + return m.CreateGetRequestInformationWithRequestConfiguration(nil) + }, + requestConfiguration, + mailURLTemplate, + ) + if err != nil { + return nil, err + } + + errorMapping := abs.ErrorMappings{ + "4XX": odataerrors.CreateODataErrorFromDiscriminatorValue, + "5XX": odataerrors.CreateODataErrorFromDiscriminatorValue, + } + + res, err := adapter.SendAsync( + ctx, + requestInfo, + msmaildelta.CreateDeltaResponseFromDiscriminatorValue, + errorMapping, + ) + if err != nil { + return nil, err + } + + if res == nil { + return nil, nil + } + + return res.(msmaildelta.DeltaResponseable), nil +} + +//nolint:deadcode +func sendContactsDeltaGet( + ctx context.Context, + m *mscontactdelta.DeltaRequestBuilder, + requestConfiguration *DeltaRequestBuilderGetRequestConfiguration, + adapter abs.RequestAdapter, +) (mscontactdelta.DeltaResponseable, error) { + requestInfo, err := createGetRequestInformationWithRequestConfiguration( + func() (*abs.RequestInformation, error) { + return m.CreateGetRequestInformationWithRequestConfiguration(nil) + }, + requestConfiguration, + contactsURLTemplate, + ) + if err != nil { + return nil, err + } + + errorMapping := abs.ErrorMappings{ + "4XX": odataerrors.CreateODataErrorFromDiscriminatorValue, + "5XX": odataerrors.CreateODataErrorFromDiscriminatorValue, + } + + res, err := adapter.SendAsync( + ctx, + requestInfo, + mscontactdelta.CreateDeltaResponseFromDiscriminatorValue, + errorMapping, + ) + if err != nil { + return nil, err + } + + if res == nil { + return nil, nil + } + + return res.(mscontactdelta.DeltaResponseable), nil +} diff --git a/src/internal/connector/exchange/query_options.go b/src/internal/connector/exchange/query_options.go index 2805d1c67..51e4a2bc0 100644 --- a/src/internal/connector/exchange/query_options.go +++ b/src/internal/connector/exchange/query_options.go @@ -3,6 +3,7 @@ package exchange import ( "fmt" + abs "github.com/microsoft/kiota-abstractions-go" msuser "github.com/microsoftgraph/msgraph-sdk-go/users" mscalendars "github.com/microsoftgraph/msgraph-sdk-go/users/item/calendars" mscontactfolder "github.com/microsoftgraph/msgraph-sdk-go/users/item/contactfolders" @@ -125,6 +126,24 @@ func CategoryToOptionIdentifier(category path.CategoryType) optionIdentifier { // which reduces the overall latency of complex calls // ----------------------------------------------------------------------- +// Delta requests for mail and contacts have the same parameters and config +// structs. +type DeltaRequestBuilderGetQueryParameters struct { + Count *bool `uriparametername:"%24count"` + Filter *string `uriparametername:"%24filter"` + Orderby []string `uriparametername:"%24orderby"` + Search *string `uriparametername:"%24search"` + Select []string `uriparametername:"%24select"` + Skip *int32 `uriparametername:"%24skip"` + Top *int32 `uriparametername:"%24top"` +} + +type DeltaRequestBuilderGetRequestConfiguration struct { + Headers map[string]string + Options []abs.RequestOption + QueryParameters *DeltaRequestBuilderGetQueryParameters +} + func optionsForFolderMessages(moreOps []string) (*msmfmessage.MessagesRequestBuilderGetRequestConfiguration, error) { selecting, err := buildOptions(moreOps, messages) if err != nil {