diff --git a/.env.example b/.env.example index 66adf91..4f8710d 100644 --- a/.env.example +++ b/.env.example @@ -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= diff --git a/cmd/check_health/main.go b/cmd/check_health/main.go index baa063e..79a2124 100644 --- a/cmd/check_health/main.go +++ b/cmd/check_health/main.go @@ -2,7 +2,6 @@ package check_health import ( "fmt" - "net/http" "github.com/Dobefu/csb/cmd/cs_sdk" "github.com/Dobefu/csb/cmd/database" @@ -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 diff --git a/cmd/cs_sdk/api/get-entry.go b/cmd/cs_sdk/api/get-entry.go index 8e91648..466d9ec 100644 --- a/cmd/cs_sdk/api/get-entry.go +++ b/cmd/cs_sdk/api/get-entry.go @@ -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 diff --git a/cmd/cs_sdk/get-url.go b/cmd/cs_sdk/get-url.go index bf5e5b1..519e71d 100644 --- a/cmd/cs_sdk/get-url.go +++ b/cmd/cs_sdk/get-url.go @@ -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) @@ -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) } diff --git a/cmd/cs_sdk/main.go b/cmd/cs_sdk/main.go index d308f28..256d5c2 100644 --- a/cmd/cs_sdk/main.go +++ b/cmd/cs_sdk/main.go @@ -1,6 +1,7 @@ package cs_sdk import ( + "bytes" "encoding/json" "fmt" "io" @@ -8,20 +9,35 @@ import ( "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) @@ -33,14 +49,18 @@ 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 @@ -48,7 +68,7 @@ func Request(path string, method string) (map[string]interface{}, error) { var data map[string]interface{} - err = json.Unmarshal(body, &data) + err = json.Unmarshal(respBody, &data) if err != nil { return nil, err diff --git a/cmd/cs_sdk/sync.go b/cmd/cs_sdk/sync.go index 54386cf..fa534b4 100644 --- a/cmd/cs_sdk/sync.go +++ b/cmd/cs_sdk/sync.go @@ -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 {