Skip to content

numero33/go-ghost

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

go-ghost

Simple ghost api client for golang

Heavily inspired by philips/go-ghost

Installation

go get -u github.com/numero33/go-ghost

Usage

package main

import (
	"github.com/numero33/go-ghost
)

func main() {
	client := ghost.NewClient("<URL>", "<API_KEY>")

	resp, err := client.Request(http.MethodGet, client.Endpoint("admin", "posts"), nil)
	if err != nil {
		log.Fatalf("get: %v", err)
	}
	defer resp.Body.Close()

	pr := &ghost.PostRequest{}
	if err = json.NewDecoder(resp.Body).Decode(pr); err != nil {
		log.Fatalf("decode: %v", err)
	}
	for _, post := range pr.Posts {
		fmt.Printf("title: %v Updated At: %v\n", *post.Title, post.UpdatedAt.String())
	}
}

Admin API

Get Posts
package main

import (
	"github.com/numero33/go-ghost
)

func main() {
	client := ghost.NewClient("<URL>", "<API_KEY>")

	resp, err := client.Request(http.MethodGet, client.Endpoint("admin", "posts"), nil)
	if err != nil {
		log.Fatalf("get: %v", err)
	}
	defer resp.Body.Close()

	pr := &ghost.PostRequest{}
	if err = json.NewDecoder(resp.Body).Decode(pr); err != nil {
		log.Fatalf("decode: %v", err)
	}
	for _, post := range pr.Posts {
		fmt.Printf("title: %v Updated At: %v\n", *post.Title, post.UpdatedAt.String())
	}
}
Get Posts with Parameters
package main

import (
	"github.com/numero33/go-ghost
)

func main() {
	client := ghost.NewClient("<URL>", "<API_KEY>")

	req, err := client.NewRequest(http.MethodGet, client.Endpoint("admin", "posts"), nil)
	if err != nil {
		log.Fatalf("get: %v", err)
	}

	// https://ghost.org/docs/content-api/#parameters
	q := req.URL.Query()
    
	// https://ghost.org/docs/content-api/#formats
	q.Add("formats", "plaintext,html")

	req.URL.RawQuery = q.Encode()

	resp, err := client.Do(req)
	if err != nil {
		log.Fatalf("get: %v", err)
	}
	defer resp.Body.Close()

	pr := &ghost.PostRequest{}
	if err = json.NewDecoder(resp.Body).Decode(pr); err != nil {
		log.Fatalf("decode: %v", err)
	}
	for _, post := range pr.Posts {
		fmt.Printf("title: %v Updated At: %v\n", *post.Title, post.UpdatedAt.String())
	}
}
Create Post
package main

import (
	"github.com/numero33/go-ghost
)

func main() {
	client := ghost.NewClient("<URL>", "<API_KEY>")

	md := `This is a **test post** made with the [go-ghost](https://github.com/numero33/go-ghost) library.`

	mobiledoc := `{ "version": "0.3.1", "atoms": [], "cards": [["markdown", { "markdown": "` + md + `" }]], "markups": [], "sections": [[10, 0]] }`

	presp := ghost.PostRequest{
		Posts: []ghost.Post{
			ghost.Post{
				Title:     ghost.String("Test Post"),
				Mobiledoc: ghost.String(mobiledoc),
			}},
	}

	resp, err := client.Request(http.MethodPost, client.Endpoint("admin", "posts"), presp)
	if err != nil {
		log.Fatalf("post: %v", err)
	}

	defer resp.Body.Close()

	pr := &ghost.PostRequest{}
	if err = json.NewDecoder(resp.Body).Decode(pr); err != nil {
		log.Fatalf("decode: %v", err)
	}
	for _, post := range pr.Posts {
		fmt.Printf("title: %v Updated At: %v\n", *post.Title, post.UpdatedAt.String())
	}
}

About

Go library for the Ghost API

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Sponsor this project

 

Packages

No packages published

Languages