emmenko / commercetools-go-sdk

Go SDK for commercetools

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

commercetools-go-sdk

Build Status codecov Go Report Card GoDoc

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

This means that the SDK is not feature complete at the moment. The SDK is currently not meant for building e-commerce front-ends with it, but aimed at maintaining the configuration of such a site. A front-end can be built using one of the existing SDK's, provided and maintained by commercetools. Or use our unofficial Python SDK for Commercetools!

Using the SDK

package main

import (
    "log"

    "golang.org/x/oauth2/clientcredentials"
    "github.com/labd/commercetools-go-sdk/commercetools"
)

func main() {

    oauth2Config := &clientcredentials.Config{
        ClientID:     "<client-id>",
        ClientSecret: "<client-secret>",
        Scopes:       []string{"manage_project:<scopes>"},
        TokenURL:     "https://auth.sphere.io/oauth/token",
    }
    httpClient := oauth2Config.Client(context.TODO())

    // 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 := commercetools.New(&commercetools.Config{
        ProjectKey: "<project-key>",
        URL:        "https://api.sphere.io",
        HTTPClient: httpClient,
        LibraryName: "some-library",
        LibraryVersion: "v1.0.0",
        ContactURL: "https://example.org",
        ContactEmail: "opensource@example.org",
    })

    product, err := client.Products.Create(&commercetools.ProductDraft{
        Key: "test-product",
        Name: commercetools.LocalizedString{
            "nl": "Een test product",
            "en": "A test product",
        },
        ProductType: commercetools.ProductTypeReference{
            ID:     "8750e1fd-f431-481f-9296-967b1e56bf49",
        },
        Slug: commercetools.LocalizedString{
            "nl": "een-test-product",
            "en": "a-test-product",
        },
    }
    if err != nil {
        log.Fatal(err)
    }

    log.Print(product)
}

Generating code

To generate code do the following steps:

  • pip install pyyaml
  • install yq (f.e. brew install yq on macOS)
  • git clone https://github.com/commercetools/commercetools-api-reference.git in the folder on the same level as the commercetools-go-sdk folder
  • make generate

Service implementation

At the moment the SDK has service coverage for primarily Terraform configuration use-cases, for example declaring API Extensions, Subscriptions and Project Settings. Broader coverage will be implemented if our use-cases require it or via contributions of course!

TODO for code generating the services:

  • Currently only /{ID} and /key={key} is supported, support other actions (f.e. Replicate cart)
  • Parse and use trait definitions (traits folder)
  • CustomObject service implementation
  • Implement other HTTPMethods/actions apart from get/post/delete (f.e. /images on product)
  • Implement all traits (f.e. priceSelecting)
  • Implement reference expansion

About

Go SDK for commercetools

License:MIT License


Languages

Language:Go 99.9%Language:Python 0.1%Language:Makefile 0.1%