github.com/labd/commercetools-go-sdk
go get github.com/labd/commercetools-go-sdk

github.com/labd/commercetools-go-sdk

Go SDK for commercetools

by labd

v0.3.1 (see all)License:MIT
go get github.com/labd/commercetools-go-sdk
Readme

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.

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:

GitHub Stars

12

LAST COMMIT

5mos ago

MAINTAINERS

0

CONTRIBUTORS

17

OPEN ISSUES

2

OPEN PRs

2
VersionTagPublished
v0.3.2-0.20211211165304-d79e0eec61d7
5mos ago
v0.3.2-0.20211211151936-cc5b8285c69d
5mos ago
v0.3.2-0.20211209135518-fe00485c9331
5mos ago
v0.3.2-0.20211209115247-fe740f90de80
5mos ago
No alternatives found
No tutorials found
Add a tutorial