Skip to content

Commit

Permalink
Merge pull request #6 from ButterCMS/feat/add-preview-param
Browse files Browse the repository at this point in the history
feat: preview mode for ButterCMS
  • Loading branch information
jakelumetta authored Sep 14, 2022
2 parents a2cc61b + 56bf102 commit 38c5993
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,16 @@ ButterCMS.SearchPosts("query", params)

To include this package in your Go code, simply use `import "ButterCMS"` at the top of your file

### Preview mode

Preview mode can be used to setup a staging website for previewing content fields or for testing content during localdevelopment. To fetch content from preview mode call `ButterCMS.SetPreviewMode` with `true` input:

```go
package main
...
ButterCMS.SetPreviewMode(true)
```

## Example Usage

```go
Expand Down
15 changes: 13 additions & 2 deletions buttercms.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
package ButterCMS

import (
"strings"
"encoding/json"
"errors"
"io/ioutil"
"net/http"
"strings"
)

var authToken string

var preview bool

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

Expand All @@ -30,6 +32,11 @@ func getRequest(path string, params map[string]string) ([]byte, error) {
for k := range params {
q.Add(k, params[k])
}

if preview {
q.Add("preview", "1")
}

req.URL.RawQuery = q.Encode()

resp, err := client.Do(req)
Expand All @@ -56,6 +63,10 @@ func SetAuthToken(token string) {
authToken = token
}

func SetPreviewMode(inPreview bool) {
preview = inPreview
}

///////////
// Feed
///////////
Expand Down

0 comments on commit 38c5993

Please sign in to comment.