Skip to Main Content

Jul 29, 2019 | 2 minute read

A new Go SDK for Elastic Path

written by Jamie Barton

Today we are excited to share with you the early release of a new Go SDK for Elastic Path. Along with JavaScript and Swift, "gomo" is our third official SDK. We ❤️Go!

The Go SDK exposes a simple API along with some helpful functions when dealing with pagination, included resources, sorting and debugging. We carefully designed the SDK to make working with extended schemas (Flows) easy and intuitive.

Whether you're building a service that sits between your site and Elastic Path or you're using Elastic Path webhooks, you now have the option to use a native Go SDK for Elastic Path.

Working with a dynamic API like Elastic Path's in a statically typed language like Go does have its challenges. The idiomatic method for working with JSON is to unmarshall the API response onto a struct, but in order to do this, the types of each field must be known at compile time.

There are ways aroudn this using Go's empty interface (interface{}) and reflection, but they are quite cumbersome.

Gomo makes use of struct embedding and functional options to try and minimise these issues. API requests are made by constructing a suitable target struct for the response by embedding the supplied core types along with any flow fields and then selecting which parts of the response should be written to those structs.

Here is an example of a JSON response receive from the Elastic Path API:

{
"data":{
"id":"521e6029-0e0e-4704-b9a5-9777047ada04",
"type":"category",
"status":"live",
"name":"Bright",
"slug":"bright",
"description":"Bright Category",
"meta":{
"timestamps":{
"created_at":"2018-02-05T11:19:08+00:00",
"updated_at":"2018-03-29T10:34:13+00:00"
}
},
"relationships":{
"products":{
"data":[
{
"type":"product",
"id":"41c84c63-4d5e-4135-979f-e4b681b83dcc"
}
]
}
},
"background_colour":"#ded7cb",
"background_image":"https://s3-eu-west-1.amazonaws.com/bkt-svc-files-cmty-api-moltin-com/e8c53cb0-120d-4ea5-8941-ce74dec06038/61118f21-14a2-466c-a84b-c30b1f900cf9.png"
},
"included":{
"products":[
{
"type":"product",
"id":"41c84c63-4d5e-4135-979f-e4b681b83dcc",
"name":"Orb",
"slug":"orb",
"sku":"ORLP100WHI",
"manage_stock":false,
"description":"Abstract, sculptural, refined and edgy with a modern twist. Its symmetrical, spoked structure generates a clever geometric presence, which works well in a contemporary environment.",
"price":[
{
"amount":45000,
"currency":"USD",
"includes_tax":true
}
],
"status":"live",
"commodity_type":"physical",
"meta":{
"timestamps":{
"created_at":"2017-06-19T13:26:03+00:00",
"updated_at":"2018-03-13T13:43:57+00:00"
},
"display_price":{
"with_tax":{
"amount":45000,
"currency":"USD",
"formatted":"$450.00"
},
"without_tax":{
"amount":45000,
"currency":"USD",
"formatted":"$450.00"
}
},
"stock":{
"level":500,
"availability":"in-stock"
}
},
"relationships":{
"files":{
"data":[
{
"type":"file",
"id":"61118f21-14a2-466c-a84b-c30b1f900cf9"
}
]
},
"categories":{
"data":[
{
"type":"category",
"id":"521e6029-0e0e-4704-b9a5-9777047ada04"
}
]
},
"main_image":{
"data":{
"type":"main_image",
"id":"61118f21-14a2-466c-a84b-c30b1f900cf9"
}
}
}
}
]
}
}

As you can see, this is quite complex. With included products and flow fields in the category object. The response above is broken down into sections, in this case data and included. To deal with this response using the new Moltin Go SDK "Gomo", you would use:

var category struct {
core.Category
BackgroundColour string `json:"background_colour"`
BackgroundImage string `json:"background_image"`
}
var included struct {
Products []core.Product `json:"products"`
}
err := client.Get(
"categories/521e6029-0e0e-4704-b9a5-9777047ada04",
Include("products")
Data(&category),
Included(&included),
)

The structs category and included are the targets for the response. The

Get()

function takes the endpoint and a list of functional options that configure the request and specify how to deal with the response. In this case Include() configures the inclusion of products in the response, Data() specifies the target for the returned category, and Included() specifies the target for the included products.