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

<!--- Please check the type of change your PR introduces: --->
- [x] 🌻 Feature
- [ ] 🐛 Bugfix
- [ ] 🗺️ Documentation
- [ ] 🤖 Test
- [ ] 💻 CI/Deployment
- [ ] 🐹 Trivial/Minor

## Issue(s)

* #1612 

## Test Plan

<!-- How will this be tested prior to merging.-->
- [x] 💪 Manual
- [ ]  Unit test
- [ ] 💚 E2E
This commit is contained in:
ashmrtn 2022-11-30 12:24:31 -08:00 committed by GitHub
parent 17a8c87168
commit bd373bbfd4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 142 additions and 0 deletions

View File

@ -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
}

View File

@ -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 {