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

incentro-dc/commercetools-go-sdk

 
 

Repository files navigation

commercetools-go-sdk

Build Status codecov Go Report Card GoDoc

The Commercetools Go SDK is automatically generated based on the official API specifications of Commercetools. It should therefore be nearly feature complete.

The SDK was initially created for enabling the creation of the Terraform Provider for Commercetools That provider enables you to use infrastructure-as-code principles with Commercetools.

Note that since this SDK is automatically generated we cannot guarantee backwards compatibility between releases. Please pin the dependency correctly and be aware of potential changes when updating

Using the SDK

package main

import (
    "context"
    "fmt"
    "log"
    "math/rand"
    "time"
    "golang.org/x/oauth2/clientcredentials"
    "github.com/labd/commercetools-go-sdk/platform"
)

func main() {

    // 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,
    // and when empty will automatically be created using the env values.
    client, err := platform.NewClient(&platform.ClientConfig{
        URL: "https://api.europe-west1.gcp.commercetools.com",
        Credentials: &clientcredentials.Config{
            TokenURL:     "https://auth.europe-west1.gcp.commercetools.com/oauth/token",
            ClientID:     "<client-id>",
            ClientSecret: "<client-secret>",
            Scopes:       []string{"manage_project:<project-key>"},
        },
    })

    projectClient := client.WithProjectKey("<project-key>")

    ctx := context.Background()

    // Get or Createa product type
    productTypeDraft := platform.ProductTypeDraft{
        Name: "a-product-type",
        Key:  "a-product-type",
    }

    productType, err := (
        projectClient.
        ProductTypes().
        WithKey(productTypeDraft.Key).
        Execute(ctx))

    if productType == nil {
        productType, err = (
            projectClient.
            ProductTypes().
            Post(&productTypeDraft).
            Execute(ctx))

        if err != nil {
            log.Println(err)
        }
    }

    r := rand.New(rand.NewSource(time.Now().UnixNano()))
    randomID := r.Int()
    productDraft := &platform.ProductDraft{
        Key: fmt.Sprintf("test-product-%d", randomID),
        Name: &platform.LocalizedString{
            "nl": "Een test product",
            "en": "A test product",
        },
        ProductType: &platform.ProductTypeResourceIdentifier{
            ID: productType.ID,
        },
        Slug: &platform.LocalizedString{
            "nl": fmt.Sprintf("een-test-product-%d", randomID),
            "en": fmt.Sprintf("a-test-product-%d", randomID),
        },
    }

    // The last argument is optional for reference expansion
    product, err := projectClient.
        Products().
        Post(productDraft).
        WithQueryParams(
            &platform.ByProjectKeyProductsRequestMethodPostInput{
                Expand: []{"foobar"},
            }
        ).
        Execute(ctx)

    // Alternatively you can pass query params via methods
    projectClient.
        Products().
        Post(productDraft).
        Expand([]{"foobar"})
        Execute(ctx)

    if err != nil {
        log.Fatal(err)
    }

    log.Print(product)
}

Generating code

To re-generate the API based on new RAML files take the following steps:

Packages

No packages published

Languages

  • Go 100.0%