Skip to content
This repository has been archived by the owner on Oct 11, 2024. It is now read-only.

Commit

Permalink
GC: Add Service to graph/betasdk package (#2298)
Browse files Browse the repository at this point in the history
## 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. 
<!-- Insert PR description-->

## Does this PR need a docs update or release note?
- [x] ⛔ No 

## Type of change

<!--- Please check the type of change your PR introduces: --->
- [x] 🌻 Feature

## Issue(s)

<!-- Can reference multiple issues. Use one of the following "magic words" - "closes, fixes" to auto-close the Github issue. -->
- related to * #2174<issue>

## Test Plan

<!-- How will this be tested prior to merging.-->
- [x] 💪 Manual
  • Loading branch information
Danny authored Jan 31, 2023
1 parent c270536 commit 013eeab
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 0 deletions.
43 changes: 43 additions & 0 deletions src/internal/connector/discovery/api/beta_service.go
Original file line number Diff line number Diff line change
@@ -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()
}
49 changes: 49 additions & 0 deletions src/internal/connector/discovery/api/beta_service_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
5 changes: 5 additions & 0 deletions src/internal/connector/graph/betasdk/beta_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit 013eeab

Please sign in to comment.