Skip to content

Commit

Permalink
Merge pull request #1 from ButterCMS/pages
Browse files Browse the repository at this point in the history
Pages
  • Loading branch information
jakelumetta authored Nov 6, 2017
2 parents 5eff5b2 + 6da76fd commit 3f682fc
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 18 deletions.
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,28 @@ func main() {
}

fmt.Printf("%+v\n", postResp)

pagesResp, err := ButterCMS.GetPages("news", nil)
if err != nil {
panic(err.Error())
}

fmt.Printf("%d Results\n", pagesResp.MetaData.Count)
for index, page := range pagesResp.PageList {
fmt.Printf("Result %d: %s\n", index, page.Slug)

if (len(page.Fields) > 0) {
fmt.Println("Custom fields:")
for fieldName, fieldValue := range page.Fields {
switch value := fieldValue.(type) {
case string:
fmt.Printf("Field '%1s' has value '%2s'\n", fieldName, value)
default:
fmt.Println("Other type\n")
}
}
}
}
}
```

Expand Down
28 changes: 22 additions & 6 deletions api.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package ButterCMS

// Posts
type PostsMeta struct {
type PagingMeta struct {
Count int `json:"count"`
NextPage int `json:"next_page"`
PreviousPage int `json:"previous_page"`
}

// Posts
type PostsAPIResponse struct {
MetaData PostsMeta `json:"meta"`
PostList []Post `json:"data"`
MetaData PagingMeta `json:"meta"`
PostList []Post `json:"data"`
}

type PostMeta struct {
Expand All @@ -18,8 +18,24 @@ type PostMeta struct {
}

type PostAPIResponse struct {
MetaData PostMeta `json:"meta"`
Post Post `json:"data"`
MetaData PostMeta `json:"meta"`
Post Post `json:"data"`
}

// Pages
type PagesMeta struct {
Count int `json:"count"`
NextPage int `json:"next_page"`
PreviousPage int `json:"previous_page"`
}

type PagesAPIResponse struct {
MetaData PagingMeta `json:"meta"`
PageList []Page `json:"data"`
}

type PageAPIResponse struct {
Page Page `json:"data"`
}

// Categories
Expand Down
82 changes: 70 additions & 12 deletions buttercms.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
var authToken string

const (
VERSION = "2.3.0"
API_ROOT_URL = "https://api.buttercms.com/v2/"
)

Expand All @@ -22,6 +23,7 @@ func getRequest(path string, params map[string]string) ([]byte, error) {
client := &http.Client{}
req, _ := http.NewRequest("GET", API_ROOT_URL+path, nil)
req.Header.Set("Content-Type", "application/json")
req.Header.Set("X-Butter-Client", "Go/"+VERSION)

q := req.URL.Query()
q.Add("auth_token", authToken)
Expand Down Expand Up @@ -54,8 +56,12 @@ func SetAuthToken(token string) {
authToken = token
}

///////////
// Feed
///////////

func GetFeed(feedType string) (*FeedAPIResponse, error) {
body, err := getRequest("feeds/"+feedType, nil)
body, err := getRequest("feeds/"+feedType+"/", nil)
if err != nil {
return nil, err
}
Expand All @@ -68,9 +74,45 @@ func GetFeed(feedType string) (*FeedAPIResponse, error) {
return resp, err
}

///////////
// Pages
///////////

func GetPages(pageType string, params map[string]string) (*PagesAPIResponse, error) {
body, err := getRequest("pages/"+pageType+"/", params)
if err != nil {
return nil, err
}

var resp = new(PagesAPIResponse)
err = json.Unmarshal(body, &resp)
if err != nil {
return nil, err
}
return resp, err
}

func GetPage(pageType string, slug string, params map[string]string) (*PageAPIResponse, error) {
body, err := getRequest("pages/"+pageType+"/"+slug+"/", params)
if err != nil {
return nil, err
}

var resp = new(PageAPIResponse)
err = json.Unmarshal(body, &resp)
if err != nil {
return nil, err
}
return resp, err
}

///////////
// Posts
///////////

func SearchPosts(query string, params map[string]string) (*PostsAPIResponse, error) {
params["query"] = query
body, err := getRequest("search", params)
body, err := getRequest("search/", params)
if err != nil {
return nil, err
}
Expand All @@ -84,7 +126,7 @@ func SearchPosts(query string, params map[string]string) (*PostsAPIResponse, err
}

func GetPosts(params map[string]string) (*PostsAPIResponse, error) {
body, err := getRequest("posts", params)
body, err := getRequest("posts/", params)
if err != nil {
return nil, err
}
Expand All @@ -98,7 +140,7 @@ func GetPosts(params map[string]string) (*PostsAPIResponse, error) {
}

func GetPost(slug string) (*PostAPIResponse, error) {
body, err := getRequest("posts/"+slug, nil)
body, err := getRequest("posts/"+slug+"/", nil)
if err != nil {
return nil, err
}
Expand All @@ -111,8 +153,12 @@ func GetPost(slug string) (*PostAPIResponse, error) {
return resp, err
}

///////////
// Authors
///////////

func GetAuthors(params map[string]string) (*AuthorsAPIResponse, error) {
body, err := getRequest("authors", params)
body, err := getRequest("authors/", params)
if err != nil {
return nil, err
}
Expand All @@ -126,7 +172,7 @@ func GetAuthors(params map[string]string) (*AuthorsAPIResponse, error) {
}

func GetAuthor(slug string, params map[string]string) (*AuthorAPIResponse, error) {
body, err := getRequest("authors/"+slug, params)
body, err := getRequest("authors/"+slug+"/", params)
if err != nil {
return nil, err
}
Expand All @@ -139,8 +185,12 @@ func GetAuthor(slug string, params map[string]string) (*AuthorAPIResponse, error
return resp, err
}

///////////
// Categories
///////////

func GetCategories(params map[string]string) (*CategoriesAPIResponse, error) {
body, err := getRequest("categories", params)
body, err := getRequest("categories/", params)
if err != nil {
return nil, err
}
Expand All @@ -167,8 +217,12 @@ func GetCategory(slug string, params map[string]string) (*CategoryAPIResponse, e
return resp, err
}

///////////
// Tags
///////////

func GetTags(params map[string]string) (*TagsAPIResponse, error) {
body, err := getRequest("tags", params)
body, err := getRequest("tags/", params)
if err != nil {
return nil, err
}
Expand All @@ -182,7 +236,7 @@ func GetTags(params map[string]string) (*TagsAPIResponse, error) {
}

func GetTag(slug string, params map[string]string) (*TagAPIResponse, error) {
body, err := getRequest("tags/"+slug, params)
body, err := getRequest("tags/"+slug+"/", params)
if err != nil {
return nil, err
}
Expand All @@ -195,9 +249,13 @@ func GetTag(slug string, params map[string]string) (*TagAPIResponse, error) {
return resp, err
}

func GetContentFields(keys []string) (*ContentFieldsAPIResponse, error) {
params := map[string]string{"keys": strings.Join(keys, ",")}
body, err := getRequest("content", params)
///////////
// Content Fields
///////////

func GetContentFields(keys []string, params map[string]string) (*ContentFieldsAPIResponse, error) {
params["keys"] = strings.Join(keys, ",")
body, err := getRequest("content/", params)
if err != nil {
return nil, err
}
Expand Down
6 changes: 6 additions & 0 deletions page.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package ButterCMS

type Page struct {
Slug string `json:"slug"`
Fields map[string]interface{} `json:"fields"`
}

0 comments on commit 3f682fc

Please sign in to comment.