Skip to content

Commit

Permalink
Add a new env variable for the Contentstack management token
Browse files Browse the repository at this point in the history
  • Loading branch information
Dobefu committed Dec 14, 2024
1 parent f83c29f commit 274aedc
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 21 deletions.
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
CS_API_KEY=
CS_DELIVERY_TOKEN=
CS_MANAGEMENT_TOKEN=
CS_BRANCH="main"
# Can be one of: "us", "eu", "azure-na" or "azure-eu".
CS_REGION=
Expand Down
25 changes: 20 additions & 5 deletions cmd/check_health/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package check_health

import (
"fmt"
"net/http"

"github.com/Dobefu/csb/cmd/cs_sdk"
"github.com/Dobefu/csb/cmd/database"
Expand Down Expand Up @@ -31,17 +30,33 @@ func checkDatabase() error {
}

func checkCsSdk() error {
var resp *http.Response
var resp map[string]interface{}
var err error

resp, err = cs_sdk.RequestRaw("content_types", "GET")
// Create a temporary label in Contentstack, to test the management token.
resp, err = cs_sdk.Request(
"labels",
"POST",
map[string]interface{}{
"label": map[string]interface{}{
"name": "__csb_healthcheck",
},
},
)

if err != nil {
return err
}

if resp.StatusCode != http.StatusOK {
return fmt.Errorf("Could not connect to Contentstack: %s", resp.Status)
// Delete the temporary label in Contentstack.
_, err = cs_sdk.Request(
fmt.Sprintf("labels/%s", resp["label"].(map[string]interface{})["uid"]),
"DELETE",
nil,
)

if err != nil {
return err
}

return err
Expand Down
2 changes: 1 addition & 1 deletion cmd/cs_sdk/api/get-entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func GetEntry(route structs.Route) (interface{}, error) {
route.Locale,
)

res, err := cs_sdk.Request(path, "GET")
res, err := cs_sdk.Request(path, "GET", nil)

if err != nil {
return nil, err
Expand Down
9 changes: 7 additions & 2 deletions cmd/cs_sdk/get-url.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ import (

var VERSION = "v3"

func GetUrl() string {
func GetUrl(method string) string {
region := os.Getenv("CS_REGION")
endpoint := "cdn"
extension := "com"

region = fmt.Sprintf("%s-", region)
Expand All @@ -18,5 +19,9 @@ func GetUrl() string {
extension = "io"
}

return fmt.Sprintf("https://%scdn.contentstack.%s", region, extension)
if method != "GET" {
endpoint = "api"
}

return fmt.Sprintf("https://%s%s.contentstack.%s", region, endpoint, extension)
}
40 changes: 30 additions & 10 deletions cmd/cs_sdk/main.go
Original file line number Diff line number Diff line change
@@ -1,27 +1,43 @@
package cs_sdk

import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"os"
)

func RequestRaw(path string, method string) (*http.Response, error) {
url := fmt.Sprintf("%s/%s/%s", GetUrl(), VERSION, path)
func RequestRaw(path string, method string, body map[string]interface{}) (*http.Response, error) {
url := fmt.Sprintf("%s/%s/%s", GetUrl(method), VERSION, path)

bodyJson, err := json.Marshal(body)

if err != nil {
return nil, err
}

client := http.Client{}
req, err := http.NewRequest(method, url, nil)
req, err := http.NewRequest(method, url, bytes.NewBuffer(bodyJson))

if err != nil {
return nil, err
}

req.Header = http.Header{
"api_key": {os.Getenv("CS_API_KEY")},
"access_token": {os.Getenv("CS_DELIVERY_TOKEN")},
"branch": {os.Getenv("CS_BRANCH")},
"api_key": {os.Getenv("CS_API_KEY")},
"branch": {os.Getenv("CS_BRANCH")},
}

if method == "GET" {
req.Header.Set("access_token", os.Getenv("CS_DELIVERY_TOKEN"))
} else {
req.Header.Set("authorization", os.Getenv("CS_MANAGEMENT_TOKEN"))
}

if body != nil {
req.Header.Set("Content-Type", "application/json")
}

res, err := client.Do(req)
Expand All @@ -33,22 +49,26 @@ func RequestRaw(path string, method string) (*http.Response, error) {
return res, nil
}

func Request(path string, method string) (map[string]interface{}, error) {
res, err := RequestRaw(path, method)
func Request(path string, method string, body map[string]interface{}) (map[string]interface{}, error) {
resp, err := RequestRaw(path, method, body)

if err != nil {
return nil, err
}

body, err := io.ReadAll(res.Body)
if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusCreated {
return nil, fmt.Errorf("Could not connect to Contentstack: %s", resp.Status)
}

respBody, err := io.ReadAll(resp.Body)

if err != nil {
return nil, err
}

var data map[string]interface{}

err = json.Unmarshal(body, &data)
err = json.Unmarshal(respBody, &data)

if err != nil {
return nil, err
Expand Down
6 changes: 3 additions & 3 deletions cmd/cs_sdk/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,15 +118,15 @@ func getSyncData(paginationToken string, reset bool, syncToken string) (map[stri
if paginationToken != "" {
logger.Info("Getting a new sync page")
path := fmt.Sprintf("stacks/sync?pagination_token=%s", paginationToken)
data, err = Request(path, "GET")
data, err = Request(path, "GET", nil)
} else if err != nil || reset {
logger.Info("Initialising a fresh sync")
path := "stacks/sync?init=true&type=entry_published,entry_unpublished,entry_deleted"
data, err = Request(path, "GET")
data, err = Request(path, "GET", nil)
} else {
logger.Info("Syncing data using an existing sync token")
path := fmt.Sprintf("stacks/sync?sync_token=%s", syncToken)
data, err = Request(path, "GET")
data, err = Request(path, "GET", nil)
}

if err != nil {
Expand Down

0 comments on commit 274aedc

Please sign in to comment.