Categorygithub.com/ZTX-Foundation/contentful-go
modulepackage
1.0.13
Repository: https://github.com/ztx-foundation/contentful-go.git
Documentation: pkg.go.dev

# README

Godoc License: MIT Build Status

❗ Disclaimer

This project is not actively maintained or monitored. Feel free to fork and work on it in your account. If you want to maintain but also collaborate with fellow developers, feel free to reach out to Contentful's Developer Relations team to move the project into our community GitHub organisation contentful-userland.

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

# Functions

NewCDA returns a CDA client.
NewCMA returns a CMA client.
NewCollection initilazies a new collection.
NewCPA returns a CPA client.
NewQuery initilazies a new query.
NewResource returns a client for the resource/uploads endpoints.
ParseValidations converts json representation to go struct.

# Constants

FieldTypeArray content type field type for array data.
FieldTypeBoolean content type field type for boolean data.
FieldTypeDate content type field type for date data.
FieldTypeInteger content type field type for integer data.
FieldTypeLink content type field type for link data.
FieldTypeLocation content type field type for location data.
FieldTypeObject content type field type for object data.
FieldTypeSymbol content type field type for text data.
FieldTypeText content type field type for text data.
FieldValidationRegexPattern12HourTime 12-hour time validation.
FieldValidationRegexPattern24HourTime 24-hour time validation.
FieldValidationRegexPatternEmail email validation.
FieldValidationRegexPatternEuorpeanDate euorpean date validation.
FieldValidationRegexPatternURL url validation.
FieldValidationRegexPatternUSDate us date validation.
FieldValidationRegexPatternUSPhoneNumber us phone number validation.
FieldValidationRegexPatternUSZipCode us zip code validation.
MimeTypeArchive mime type validation for content type field.
MimeTypeAttachment mime type validation for content type field.
MimeTypeAudio mime type validation for content type field.
MimeTypeCode mime type validation for content type field.
MimeTypeImage mime type validation for content type field.
MimeTypeMarkup mime type validation for content type field.
MimeTypePDF mime type validation for content type field.
MimeTypePlainText mime type validation for content type field.
MimeTypePresentation mime type validation for content type field.
MimeTypeRichText mime type validation for content type field.
MimeTypeSpreadSheet mime type validation for content type field.
MimeTypeVideo mime type validation for content type field.

# Variables

Version for SDK Version.

# Structs

AccessDeniedError error model for access denied responses.
AccessTokenInvalidError for 401 errors.
APIError model.
APIKey model.
APIKeyPolicy model.
Asset model.
BadRequestError error model for bad request responses.
Collection model.
CollectionOptions holds init options.
Contentful model.
ContentType model.
DateMinMax model.
Entry
Entry model.
EntryField model.
ErrorDetail model.
ErrorDetails model.
ErrorResponse model.
Field model.
FieldTypeArrayItem model.
FieldValidationDate model.
FieldValidationDimension model.
FieldValidationFileSize model.
FieldValidationLink model.
FieldValidationMimeType model.
FieldValidationPredefinedValues model.
FieldValidationRange model.
FieldValidationRegex model.
FieldValidationSize model.
FieldValidationUnique model.
File model.
FileDetail model.
FileFields model.
FileImage model.
InvalidQueryError error model for invalid query responses.
Locale model.
MinMax model.
NotFoundError for 404 errors.
PreviewAPIKey model.
Query model.
RateLimitExceededError for rate limit errors.
Regex model.
Resource model.
ServerError error model for server error responses.
Space model.
Sys model.
UploadFrom model.
ValidationFailedError model.
VersionMismatchError for 409 errors.
Webhook model.
WebhookHeader model.

# Interfaces

FieldValidation interface.

# Type aliases

APIKeyService service.
AssetsService service.
ContentTypesService service.
EntriesService service.
LocalesService service.
ResourcesService service.
SpacesService model.
WebhooksService service.