update commercetools to v1.1.0

Signed-off-by: Bernd Verst <4535280+berndverst@users.noreply.github.com>
This commit is contained in:
Bernd Verst 2022-10-11 14:55:15 -07:00
parent 83a562d71a
commit e7367a4bbe
1 changed files with 23 additions and 11 deletions

View File

@ -17,16 +17,19 @@ import (
"context" "context"
"encoding/json" "encoding/json"
"errors" "errors"
"fmt"
"github.com/dapr/components-contrib/bindings" "github.com/dapr/components-contrib/bindings"
"github.com/dapr/kit/logger" "github.com/dapr/kit/logger"
"github.com/labd/commercetools-go-sdk/commercetools" "github.com/labd/commercetools-go-sdk/platform"
"golang.org/x/oauth2/clientcredentials"
) )
type Binding struct { type Binding struct {
client *commercetools.Client client *platform.Client
logger logger.Logger logger logger.Logger
projectKey string
} }
type Data struct { type Data struct {
@ -53,14 +56,22 @@ func (ct *Binding) Init(metadata bindings.Metadata) error {
if err != nil { if err != nil {
return err return err
} }
ct.projectKey = commercetoolsM.projectKey
// The helper method NewClientEndpoint no longer exists, so URLs need to be manually constructed.
// Reference: https://github.com/labd/commercetools-go-sdk/blob/15f4e7e85260cf206301504dced00a8bbf4d8682/commercetools/client.go#L115
baseURLdomain := fmt.Sprintf("%s.%s.commercetools.com", commercetoolsM.region, commercetoolsM.provider)
authURL := fmt.Sprintf("https://auth.%s/oauth/token", baseURLdomain)
apiURL := fmt.Sprintf("https://api.%s", baseURLdomain)
// Create the new client. When an empty value is passed it will use the CTP_* // Create the new client. When an empty value is passed it will use the CTP_*
// environment variables to get the value. The HTTPClient arg is optional, // environment variables to get the value. The HTTPClient arg is optional,
// and when empty will automatically be created using the env values. // and when empty will automatically be created using the env values.
client, err := commercetools.NewClient(&commercetools.ClientConfig{ client, err := platform.NewClient(&platform.ClientConfig{
ProjectKey: commercetoolsM.projectKey, URL: apiURL,
Endpoints: commercetools.NewClientEndpoints(commercetoolsM.region, commercetoolsM.provider), Credentials: &clientcredentials.Config{
Credentials: &commercetools.ClientCredentials{ TokenURL: authURL,
ClientID: commercetoolsM.clientID, ClientID: commercetoolsM.clientID,
ClientSecret: commercetoolsM.clientSecret, ClientSecret: commercetoolsM.clientSecret,
Scopes: []string{commercetoolsM.scopes}, Scopes: []string{commercetoolsM.scopes},
@ -116,14 +127,15 @@ func handleGraphQLQuery(ctx context.Context, ct *Binding, query string) (*bindin
res := &bindings.InvokeResponse{Data: nil, Metadata: nil} res := &bindings.InvokeResponse{Data: nil, Metadata: nil}
if len(query) > 0 { if len(query) > 0 {
gql := ct.client.NewGraphQLQuery(query) gql := ct.client.WithProjectKey(ct.projectKey).Graphql().Post(platform.GraphQLRequest{
var gqlResp interface{} Query: query,
errGQL := gql.Execute(&gqlResp) })
gqlResp, errGQL := gql.Execute(ctx)
if errGQL != nil { if errGQL != nil {
return nil, errors.New("commercetools error: Error executing the provided GraphQL query") return nil, errors.New("commercetools error: Error executing the provided GraphQL query")
} }
bQuery, errM := json.Marshal(gqlResp) bQuery, errM := json.Marshal(gqlResp.Data)
if errM != nil { if errM != nil {
return nil, errors.New("commercetools error: Error marshalling GraphQL query result") return nil, errors.New("commercetools error: Error marshalling GraphQL query result")
} }