From 013eeab7cb7a872c50b181811b05d2fe339ecf72 Mon Sep 17 00:00:00 2001 From: Danny Date: Tue, 31 Jan 2023 15:46:31 -0500 Subject: [PATCH] GC: Add Service to `graph/betasdk` package (#2298) ## Description Creates Beta Service for betasdk package. Abstraction loosely complies with the methods found in `graph.Servicer()`. Will aid when Sharepoint functionality is supported in v1.0 and `betasdk` package is removed. ## Does this PR need a docs update or release note? - [x] :no_entry: No ## Type of change - [x] :sunflower: Feature ## Issue(s) - related to * #2174 ## Test Plan - [x] :muscle: Manual --- .../connector/discovery/api/beta_service.go | 43 ++++++++++++++++ .../discovery/api/beta_service_test.go | 49 +++++++++++++++++++ .../connector/graph/betasdk/beta_client.go | 5 ++ 3 files changed, 97 insertions(+) create mode 100644 src/internal/connector/discovery/api/beta_service.go create mode 100644 src/internal/connector/discovery/api/beta_service_test.go diff --git a/src/internal/connector/discovery/api/beta_service.go b/src/internal/connector/discovery/api/beta_service.go new file mode 100644 index 0000000000..df2b1533bc --- /dev/null +++ b/src/internal/connector/discovery/api/beta_service.go @@ -0,0 +1,43 @@ +package api + +import ( + "github.com/alcionai/corso/src/internal/connector/graph/betasdk" + absser "github.com/microsoft/kiota-abstractions-go/serialization" + msgraphsdk "github.com/microsoftgraph/msgraph-sdk-go" + "github.com/pkg/errors" +) + +// Service wraps BetaClient's functionality. +// Abstraction created to comply loosely with graph.Servicer +// methods for ease of switching between v1.0 and beta connnectors +type Service struct { + client *betasdk.BetaClient +} + +func (s Service) Client() *betasdk.BetaClient { + return s.client +} + +func NewBetaService(adpt *msgraphsdk.GraphRequestAdapter) *Service { + return &Service{ + client: betasdk.NewBetaClient(adpt), + } +} + +// Seraialize writes an M365 parsable object into a byte array using the built-in +// application/json writer within the adapter. +func (s Service) Serialize(object absser.Parsable) ([]byte, error) { + writer, err := s.client.Adapter(). + GetSerializationWriterFactory(). + GetSerializationWriter("application/json") + if err != nil || writer == nil { + return nil, errors.Wrap(err, "creating json serialization writer") + } + + err = writer.WriteObjectValue("", object) + if err != nil { + return nil, errors.Wrap(err, "writeObjecValue serialization") + } + + return writer.GetSerializedContent() +} diff --git a/src/internal/connector/discovery/api/beta_service_test.go b/src/internal/connector/discovery/api/beta_service_test.go new file mode 100644 index 0000000000..ad67b3877f --- /dev/null +++ b/src/internal/connector/discovery/api/beta_service_test.go @@ -0,0 +1,49 @@ +package api + +import ( + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + "github.com/stretchr/testify/suite" + + "github.com/alcionai/corso/src/internal/connector/graph" + "github.com/alcionai/corso/src/internal/connector/graph/betasdk/models" + "github.com/alcionai/corso/src/internal/tester" +) + +type BetaUnitSuite struct { + suite.Suite +} + +func TestBetaUnitSuite(t *testing.T) { + suite.Run(t, new(BetaUnitSuite)) +} + +func (suite *BetaUnitSuite) TestBetaService_Adapter() { + t := suite.T() + a := tester.NewM365Account(t) + m365, err := a.M365Config() + require.NoError(t, err) + + adpt, err := graph.CreateAdapter( + m365.AzureTenantID, + m365.AzureClientID, + m365.AzureClientSecret, + ) + require.NoError(t, err) + + service := NewBetaService(adpt) + require.NotNil(t, service) + + testPage := models.NewSitePage() + name := "testFile" + desc := "working with parsing" + + testPage.SetName(&name) + testPage.SetDescription(&desc) + + byteArray, err := service.Serialize(testPage) + assert.NotEmpty(t, byteArray) + assert.NoError(t, err) +} diff --git a/src/internal/connector/graph/betasdk/beta_client.go b/src/internal/connector/graph/betasdk/beta_client.go index 515e1de95f..ef77b81693 100644 --- a/src/internal/connector/graph/betasdk/beta_client.go +++ b/src/internal/connector/graph/betasdk/beta_client.go @@ -84,3 +84,8 @@ func (m *BetaClient) SitesById(id string) *i1a3c1a5501c5e41b7fd169f2d4c768dce9b0 } return i1a3c1a5501c5e41b7fd169f2d4c768dce9b096ac28fb5431bf02afcc57295411.NewSiteItemRequestBuilderInternal(urlTplParams, m.requestAdapter) } + +// Adapter() helper method to export Adapter for iterating +func (m *BetaClient) Adapter() *msgraphsdk.GraphRequestAdapter { + return m.requestAdapter +}