GoLang SDK for Contentful's Content Delivery, Preview and Management API's.
Contentful provides a content infrastructure for digital teams to power content in websites, apps, and devices. Unlike a CMS, Contentful was built to integrate with the modern software stack. It offers a central hub for structured content, powerful management and delivery APIs, and a customizable web app that enable developers and content creators to ship digital products faster.
Go is an open source programming language that makes it easy to build simple, reliable, and efficient software.
go get github.com/plutov/contentful-go
Import into your Go project or library
import (
contentful "github.com/plutov/contentful-go"
)
Create a API client in order to interact with the Contentful's API endpoints.
token := "your-cma-token" // observe your CMA token from Contentful's web page
cma := contentful.NewCMA(token)
If your Contentful account is part of an organization, you can setup your API client as so. When you set your organization id for the SDK client, every api request will have X-Contentful-Organization: <your-organization-id>
header automatically.
cma.SetOrganization("your-organization-id")
When debug mode is activated, sdk client starts to work in verbose mode and try to print as much informatin as possible. In debug mode, all outgoing http requests are printed nicely in the form of curl
command so that you can easly drop into your command line to debug specific request.
cma.Debug = true
contentful-go
stores its dependencies under vendor
folder and uses dep
to manage dependency resolutions. Dependencies in vendor
folder will be loaded automatically by Go 1.6+. To install the dependencies, run dep ensure
, for more options and documentation please visit dep
.
Currently SDK exposes the following resource services:
- Spaces
- APIKeys
- Assets
- ContentTypes
- Entries
- Locales
- Webhooks
Every resource service has at least the following interface:
List() *Collection
Get(spaceID, resourceID string) <Resource>, error
Upsert(spaceID string, resourceID *Resource) error
Delete(spaceID string, resourceID *Resource) error
space, err := cma.Spaces.Get("space-id")
if err != nil {
log.Fatal(err)
}
collection := cma.ContentTypes.List(space.Sys.ID)
collection, err = collection.Next()
if err != nil {
log.Fatal(err)
}
for _, contentType := range collection.ToContentType() {
fmt.Println(contentType.Name, contentType.Description)
}
All the endpoints which return an array of objects are wrapped around Collection
struct. The main features of Collection
are pagination and type assertion.
WIP
Collection
struct exposes the necessary converters (type assertion) such as ToSpace()
. The following example gets all spaces for the given account:
collection := cma.Spaces.List() // returns a collection
collection, err := collection.Next() // makes the actual api call
if err != nil {
log.Fatal(err)
}
spaces := collection.ToSpace() // make the type assertion
for _, space := range spaces {
fmt.Println(space.Name)
fmt.Println(space.Sys.ID)
}
// In order to access collection metadata
fmt.Println(col.Total)
fmt.Println(col.Skip)
fmt.Println(col.Limit)
$> go test
To enable higher verbose mode
$> go test -v -race
Content Delivery API Content Management API Content Preview API
This is a project created for demo purposes and not officially supported, so if you find issues or have questions you can let us know via the issue page but don't expect a quick and prompt response.
[WIP]
MIT