microcms

package module
v1.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 9, 2023 License: Apache-2.0 Imports: 8 Imported by: 1

README

microCMS Go SDK

microCMS Go SDK.

Tutorial

See official tutorial.

Installation

$ go get github.com/microcmsio/microcms-go-sdk

Usage

Import
import "github.com/microcmsio/microcms-go-sdk"
Create client object
serviceDomain := "YOUR_DOMAIN" // YOUR_DOMAIN is the XXXX part of XXXX.microcms.io
apiKey := "YOUR_API_KEY"
client := microcms.New(serviceDomain, apiKey)
Example content definition
type YourContent struct {
	ID          string    `json:"id,omitempty"`
	Title       string    `json:"title,omitempty"`
	Body        string    `json:"body,omitempty"`
	CreatedAt   time.Time `json:"createdAt,omitempty"`
	UpdatedAt   time.Time `json:"updatedAt,omitempty"`
	PublishedAt time.Time `json:"publishedAt,omitempty"`
	RevisedAt   time.Time `json:"revisedAt,omitempty"`
}

type YourContentList struct {
	Contents   []Content
	TotalCount int
	Limit      int
	Offset     int
}
Get content list
var list YourContentList
err := client.List(
	microcms.ListParams{
		Endpoint: "endpoint",
	},
	&list,
)
println(list.Contents[0].Title)
Get content list with parameters
var list YourContentList
err := client.List(
	microcms.ListParams{
		Endpoint: "endpoint",
		DraftKey: "abcd",
		Limit:    100,
		Offset:   1,
		Orders:   []string{"createdAt"},
		Q:        "Hello",
		Fields:   []string{"id", "title"},
		IDs:      []string{"foo"},
		Filters:  "publishedAt[greater_than]2021-01-01",
		Depth:    1,
	},
	&list,
)
println(list.Contents[0].Title)
Get single content
var content YourContent
err := client.Get(
	microcms.GetParams{
		Endpoint:  "endpoint",
		ContentID: "my-content-id",
	},
	&content,
)
println(content.Title)
Get single content with parameters
var content YourContent
err := client.Get(
	microcms.GetParams{
		Endpoint:  "endpoint",
		ContentID: "my-content-id",
		DraftKey:  "abcd",
		Fields:    []string{"id", "title"},
		Depth:     1,
	},
	&content,
)
println(content.Title)
Get object form content
var content YourContent
err := client.Get(
	microcms.GetParams{
		Endpoint: "endpoint",
	},
	&content,
)
println(content.Title)
Create content
createResult, err := client.Create(microcms.CreateParams{
	Endpoint: "endpoint",
	Content:  YourContent{
		Title: "content",
		Body:  "Hello, content!",
	},
})
println(createResult.ID)
Create content with specified ID
createResult, err := client.Create(microcms.CreateParams{
	Endpoint:  "endpoint",
	ContentID: "my-content-id",
	Content:   YourContent{
		Title: "my content",
		Body:  "Hello, my content!",
	},
})
println(createResult.ID)
Create draft content
createResult, err := client.Create(microcms.CreateParams{
	Endpoint: "endpoint",
	Status:   microcms.StatusDraft,
	Content:  YourContent{
		Title: "draft content",
		Body:  "Hello, draft content!",
	},
})
println(createResult.ID)
Update content
updateResult, err := client.Update(microcms.UpdateParams{
	Endpoint:  "endpoint",
	ContentID: "my-content-id",
	Content:   YourContent{
		Body: "Hello, new content!",
	},
})
println(updateResult.ID)
Update object form content
updateResult, err := client.Update(microcms.UpdateParams{
	Endpoint:  "endpoint",
	ContentID: "my-content-id",
	Content:   YourContent{
		Body: "Hello, new content!",
	},
})
println(updateResult.ID)
Delete content
err := client.Delete(microcms.DeleteParams{
	Endpoint:  "endpoint",
	ContentID: "my-content-id",
})
Error Handling
data, err := client.Get(ctx, "endpoint", nil)
if err != nil {
    if httpErr, ok := err.(*sdk.HttpResponseError); ok {
        fmt.Printf("HTTP Status Code: %d\n", httpErr.Response.StatusCode)
        fmt.Printf("Error Message: %s\n", httpErr.ErrorMessage)
    }
    return
}

Documentation

Index

Constants

View Source
const (
	BaseDomain  = "microcms.io"
	APIVersion  = "v1"
	StatusDraft = "draft"
)

Base API endpoint

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client struct {
	// contains filtered or unexported fields
}

func New

func New(serviceDomain, apiKey string) *Client

func (*Client) Create

func (c *Client) Create(p CreateParams) (*CreateResponse, error)

func (*Client) Delete

func (c *Client) Delete(p DeleteParams) error

func (*Client) Get

func (c *Client) Get(p GetParams, data interface{}) error

func (*Client) List

func (c *Client) List(p ListParams, data interface{}) error

func (*Client) SetHTTPClient

func (c *Client) SetHTTPClient(client httpClient)

func (*Client) Update

func (c *Client) Update(p UpdateParams) (*UpdateResponse, error)

type CreateParams

type CreateParams struct {
	Endpoint  string
	ContentID string
	Status    string
	Content   interface{}
}

type CreateResponse

type CreateResponse struct {
	ID string
}

type DeleteParams

type DeleteParams struct {
	Endpoint  string
	ContentID string
}

type GetParams

type GetParams struct {
	Endpoint  string
	ContentID string
	DraftKey  string
	Fields    []string
	Depth     int
}

type HttpResponseError added in v1.1.0

type HttpResponseError struct {
	Response     *http.Response
	ErrorMessage string
}

func (*HttpResponseError) Error added in v1.1.0

func (r *HttpResponseError) Error() string

type ListParams

type ListParams struct {
	Endpoint string
	DraftKey string
	Limit    int
	Offset   int
	Orders   []string
	Q        string
	Fields   []string
	IDs      []string
	Filters  string
	Depth    int
}

type UpdateParams

type UpdateParams struct {
	Endpoint  string
	ContentID string
	Content   interface{}
}

type UpdateResponse

type UpdateResponse struct {
	ID string
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL