Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/labd/commercetools-go-sdk
Go SDK for commercetools
https://github.com/labd/commercetools-go-sdk
commercetools commercetools-sdk
Last synced: 5 days ago
JSON representation
Go SDK for commercetools
- Host: GitHub
- URL: https://github.com/labd/commercetools-go-sdk
- Owner: labd
- License: mit
- Created: 2018-06-18T18:03:30.000Z (over 6 years ago)
- Default Branch: main
- Last Pushed: 2024-10-22T08:51:42.000Z (22 days ago)
- Last Synced: 2024-10-23T12:45:00.861Z (21 days ago)
- Topics: commercetools, commercetools-sdk
- Language: Go
- Homepage:
- Size: 2.89 MB
- Stars: 27
- Watchers: 15
- Forks: 19
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Funding: .github/FUNDING.yml
- License: LICENSE
- Code of conduct: .github/CODE_OF_CONDUCT.md
- Codeowners: .github/CODEOWNERS
Awesome Lists containing this project
README
# commercetools-go-sdk
[![Build Status](https://github.com/labd/commercetools-go-sdk/workflows/Go%20Tests/badge.svg)](https://github.com/labd/commercetools-go-sdk/workflows/)
[![codecov](https://codecov.io/gh/LabD/commercetools-go-sdk/branch/master/graph/badge.svg)](https://codecov.io/gh/LabD/commercetools-go-sdk)
[![Go Report Card](https://goreportcard.com/badge/github.com/labd/commercetools-go-sdk)](https://goreportcard.com/report/github.com/labd/commercetools-go-sdk)
[![GoDoc](https://godoc.org/github.com/labd/commercetools-go-sdk?status.svg)](https://godoc.org/github.com/labd/commercetools-go-sdk)The Commercetools Go SDK is automatically generated based on the official [API specifications](https://github.com/commercetools/commercetools-api-reference)
of Commercetools. It should therefore be nearly feature complete.The SDK was initially created for enabling the creation of the
[Terraform Provider for Commercetools](https://github.com/labd/terraform-provider-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
```go
package mainimport (
"context"
"errors"
"fmt"
"github.com/labd/commercetools-go-sdk/ctutils"
"log"
"math/rand"
"time""github.com/davecgh/go-spew/spew"
"github.com/labd/commercetools-go-sdk/platform"
"golang.org/x/oauth2/clientcredentials"
)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: "",
ClientSecret: "",
Scopes: []string{"manage_project:"},
},
})projectClient := client.WithProjectKey("")
ctx := context.Background()
// Get or Createa product type
productTypeDraft := platform.ProductTypeDraft{
Name: "a-product-type",
Key: ctutils.StringRef("a-product-type"),
}productType, err := projectClient.
ProductTypes().
WithKey(*productTypeDraft.Key).
Get().
Execute(ctx)if err != nil {
if errors.As(err, &platform.ErrNotFound) {
productType, err = projectClient.
ProductTypes().
Post(productTypeDraft).
Execute(ctx)
}if err != nil {
log.Fatal(err)
}
}r := rand.New(rand.NewSource(time.Now().UnixNano()))
randomID := r.Int()
productDraft := platform.ProductDraft{
Key: ctutils.StringRef(fmt.Sprintf("test-product-%d", randomID)),
Name: platform.LocalizedString{
"nl": "Een test product",
"en": "A test product",
},
ProductType: platform.ProductTypeResourceIdentifier{
ID: ctutils.StringRef(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: []string{"foobar"},
},
).
Execute(ctx)// Alternatively you can pass query params via methods
//product, err := projectClient.
// Products().
// Post(productDraft).
// Expand([]string{"foobar"}).
// Execute(ctx)if err != nil {
log.Fatal(err)
}spew.Dump(product)
}```
## Generating code
To re-generate the API based on new RAML files take the following steps:
- Install rmf-codegen (see https://github.com/commercetools/rmf-codegen)
- Clone the API specifications https://github.com/commercetools/commercetools-api-reference
in the parent directory
- Run `make generate`