Skip to content

foomo/contentful

 
 

Repository files navigation

codecov Godoc License: MIT Build Status

contentful-go

GoLang SDK for Contentful's Content Delivery, Preview and Management API's.

About

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.

Install

go get github.com/contentful-labs/contentful-go

Getting started

Import into your Go project or library

import (
	contentful "github.com/contentful-labs/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)

Organization

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")

Debug mode

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

Dependencies

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.

Using the SDK

Working with resource services

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

Example

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)
}

Working with collections

All the endpoints which return an array of objects are wrapped around Collection struct. The main features of Collection are pagination and type assertion.

Pagination

WIP

Type assertion

Collection struct exposes the necessary converters (type assertion) such as ToSpace(). The following example gets all spaces for the given account:

Example

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)

Testing

$> go test

To enable higher verbose mode

$> go test -v -race

Documentation/References

Contentful

Content Delivery API Content Management API Content Preview API

GoLang

Effective Go

Support

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.

Contributing

[WIP]

License

MIT