Skip to content

Commit

Permalink
environment support
Browse files Browse the repository at this point in the history
  • Loading branch information
Cristian Vidmar committed Aug 19, 2021
1 parent b632c4e commit 4b8749e
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 41 deletions.
10 changes: 5 additions & 5 deletions api_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func (apiKey *APIKey) GetVersion() int {

// List returns all api keys collection
func (service *APIKeyService) List(spaceID string) *Collection {
path := fmt.Sprintf("/spaces/%s/api_keys", spaceID)
path := fmt.Sprintf("/spaces/%s%s/api_keys", spaceID, getEnvPath(service.c))
method := "GET"

req, err := service.c.newRequest(method, path, nil, nil)
Expand All @@ -71,7 +71,7 @@ func (service *APIKeyService) List(spaceID string) *Collection {

// Get returns a single api key entity
func (service *APIKeyService) Get(spaceID, apiKeyID string) (*APIKey, error) {
path := fmt.Sprintf("/spaces/%s/api_keys/%s", spaceID, apiKeyID)
path := fmt.Sprintf("/spaces/%s%s/api_keys/%s", spaceID, getEnvPath(service.c), apiKeyID)
method := "GET"

req, err := service.c.newRequest(method, path, nil, nil)
Expand All @@ -98,10 +98,10 @@ func (service *APIKeyService) Upsert(spaceID string, apiKey *APIKey) error {
var method string

if apiKey.Sys != nil && apiKey.Sys.CreatedAt != "" {
path = fmt.Sprintf("/spaces/%s/api_keys/%s", spaceID, apiKey.Sys.ID)
path = fmt.Sprintf("/spaces/%s%s/api_keys/%s", spaceID, getEnvPath(service.c), apiKey.Sys.ID)
method = "PUT"
} else {
path = fmt.Sprintf("/spaces/%s/api_keys", spaceID)
path = fmt.Sprintf("/spaces/%s%s/api_keys", spaceID, getEnvPath(service.c))
method = "POST"
}

Expand All @@ -117,7 +117,7 @@ func (service *APIKeyService) Upsert(spaceID string, apiKey *APIKey) error {

// Delete deletes a sinlge api key entity
func (service *APIKeyService) Delete(spaceID string, apiKey *APIKey) error {
path := fmt.Sprintf("/spaces/%s/api_keys/%s", spaceID, apiKey.Sys.ID)
path := fmt.Sprintf("/spaces/%s%s/api_keys/%s", spaceID, getEnvPath(service.c), apiKey.Sys.ID)
method := "DELETE"

req, err := service.c.newRequest(method, path, nil, nil)
Expand Down
16 changes: 8 additions & 8 deletions asset.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func (asset *Asset) GetVersion() int {

// List returns asset collection
func (service *AssetsService) List(spaceID string) *Collection {
path := fmt.Sprintf("/spaces/%s/assets", spaceID)
path := fmt.Sprintf("/spaces/%s%s/assets", spaceID, getEnvPath(service.c))
method := "GET"

req, err := service.c.newRequest(method, path, nil, nil)
Expand All @@ -87,7 +87,7 @@ func (service *AssetsService) List(spaceID string) *Collection {

// Get returns a single asset entity
func (service *AssetsService) Get(spaceID, assetID string, locale ...string) (*Asset, error) {
path := fmt.Sprintf("/spaces/%s/assets/%s", spaceID, assetID)
path := fmt.Sprintf("/spaces/%s%s/assets/%s", spaceID, getEnvPath(service.c), assetID)
query := url.Values{}
if service.c.api == "CDA" && len(locale) > 0 {
query["locale"] = locale
Expand Down Expand Up @@ -159,10 +159,10 @@ func (service *AssetsService) Upsert(spaceID string, asset *Asset) error {
var method string

if asset.Sys.ID != "" {
path = fmt.Sprintf("/spaces/%s/assets/%s", spaceID, asset.Sys.ID)
path = fmt.Sprintf("/spaces/%s%s/assets/%s", spaceID, getEnvPath(service.c), asset.Sys.ID)
method = "PUT"
} else {
path = fmt.Sprintf("/spaces/%s/assets", spaceID)
path = fmt.Sprintf("/spaces/%s%s/assets", spaceID, getEnvPath(service.c))
method = "POST"
}

Expand All @@ -178,7 +178,7 @@ func (service *AssetsService) Upsert(spaceID string, asset *Asset) error {

// Delete sends delete request
func (service *AssetsService) Delete(spaceID string, asset *Asset) error {
path := fmt.Sprintf("/spaces/%s/assets/%s", spaceID, asset.Sys.ID)
path := fmt.Sprintf("/spaces/%s%s/assets/%s", spaceID, getEnvPath(service.c), asset.Sys.ID)
method := "DELETE"

req, err := service.c.newRequest(method, path, nil, nil)
Expand All @@ -200,7 +200,7 @@ func (service *AssetsService) Process(spaceID string, asset *Asset) error {
break
}

path := fmt.Sprintf("/spaces/%s/assets/%s/files/%s/process", spaceID, asset.Sys.ID, locale)
path := fmt.Sprintf("/spaces/%s%s/assets/%s/files/%s/process", spaceID, getEnvPath(service.c), asset.Sys.ID, locale)
method := "PUT"

req, err := service.c.newRequest(method, path, nil, nil)
Expand All @@ -216,7 +216,7 @@ func (service *AssetsService) Process(spaceID string, asset *Asset) error {

// Publish publishes the asset
func (service *AssetsService) Publish(spaceID string, asset *Asset) error {
path := fmt.Sprintf("/spaces/%s/assets/%s/published", spaceID, asset.Sys.ID)
path := fmt.Sprintf("/spaces/%s%s/assets/%s/published", spaceID, getEnvPath(service.c), asset.Sys.ID)
method := "PUT"

req, err := service.c.newRequest(method, path, nil, nil)
Expand All @@ -232,7 +232,7 @@ func (service *AssetsService) Publish(spaceID string, asset *Asset) error {

// Unpublish unpublishes the asset
func (service *AssetsService) Unpublish(spaceID string, asset *Asset) error {
path := fmt.Sprintf("/spaces/%s/assets/%s/published", spaceID, asset.Sys.ID)
path := fmt.Sprintf("/spaces/%s%s/assets/%s/published", spaceID, getEnvPath(service.c), asset.Sys.ID)
method := "DELETE"

req, err := service.c.newRequest(method, path, nil, nil)
Expand Down
18 changes: 9 additions & 9 deletions content_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ type ContentType struct {
}

const (
// FieldTypeSymbol content type field type for short textual data
FieldTypeSymbol = "Symbol"
// FieldTypeSymbol content type field type for short textual data
FieldTypeSymbol = "Symbol"

// FieldTypeText content type field type for text data
FieldTypeText = "Text"
Expand Down Expand Up @@ -290,7 +290,7 @@ func (ct *ContentType) GetVersion() int {

// List return a content type collection
func (service *ContentTypesService) List(spaceID string) *Collection {
path := fmt.Sprintf("/spaces/%s/content_types", spaceID)
path := fmt.Sprintf("/spaces/%s%s/content_types", spaceID, getEnvPath(service.c))
method := "GET"

req, err := service.c.newRequest(method, path, nil, nil)
Expand All @@ -307,7 +307,7 @@ func (service *ContentTypesService) List(spaceID string) *Collection {

// Get fetched a content type specified by `contentTypeID`
func (service *ContentTypesService) Get(spaceID, contentTypeID string) (*ContentType, error) {
path := fmt.Sprintf("/spaces/%s/content_types/%s", spaceID, contentTypeID)
path := fmt.Sprintf("/spaces/%s%s/content_types/%s", spaceID, getEnvPath(service.c), contentTypeID)
method := "GET"

req, err := service.c.newRequest(method, path, nil, nil)
Expand All @@ -334,10 +334,10 @@ func (service *ContentTypesService) Upsert(spaceID string, ct *ContentType) erro
var method string

if ct.Sys != nil && ct.Sys.ID != "" {
path = fmt.Sprintf("/spaces/%s/content_types/%s", spaceID, ct.Sys.ID)
path = fmt.Sprintf("/spaces/%s%s/content_types/%s", spaceID, getEnvPath(service.c), ct.Sys.ID)
method = "PUT"
} else {
path = fmt.Sprintf("/spaces/%s/content_types", spaceID)
path = fmt.Sprintf("/spaces/%s%s/content_types", spaceID, getEnvPath(service.c))
method = "POST"
}

Expand All @@ -353,7 +353,7 @@ func (service *ContentTypesService) Upsert(spaceID string, ct *ContentType) erro

// Delete the content_type
func (service *ContentTypesService) Delete(spaceID string, ct *ContentType) error {
path := fmt.Sprintf("/spaces/%s/content_types/%s", spaceID, ct.Sys.ID)
path := fmt.Sprintf("/spaces/%s%s/content_types/%s", spaceID, getEnvPath(service.c), ct.Sys.ID)
method := "DELETE"

req, err := service.c.newRequest(method, path, nil, nil)
Expand All @@ -369,7 +369,7 @@ func (service *ContentTypesService) Delete(spaceID string, ct *ContentType) erro

// Activate the contenttype, a.k.a publish
func (service *ContentTypesService) Activate(spaceID string, ct *ContentType) error {
path := fmt.Sprintf("/spaces/%s/content_types/%s/published", spaceID, ct.Sys.ID)
path := fmt.Sprintf("/spaces/%s%s/content_types/%s/published", spaceID, getEnvPath(service.c), ct.Sys.ID)
method := "PUT"

req, err := service.c.newRequest(method, path, nil, nil)
Expand All @@ -385,7 +385,7 @@ func (service *ContentTypesService) Activate(spaceID string, ct *ContentType) er

// Deactivate the contenttype, a.k.a unpublish
func (service *ContentTypesService) Deactivate(spaceID string, ct *ContentType) error {
path := fmt.Sprintf("/spaces/%s/content_types/%s/published", spaceID, ct.Sys.ID)
path := fmt.Sprintf("/spaces/%s%s/content_types/%s/published", spaceID, getEnvPath(service.c), ct.Sys.ID)
method := "DELETE"

req, err := service.c.newRequest(method, path, nil, nil)
Expand Down
9 changes: 9 additions & 0 deletions contentful.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type Contentful struct {
QueryParams map[string]string
Headers map[string]string
BaseURL string
Environment string

Spaces *SpacesService
APIKeys *APIKeyService
Expand All @@ -37,6 +38,14 @@ type service struct {
c *Contentful
}

func getEnvPath(c *Contentful) (envPath string) {
if c.Environment != "" {
envPath = fmt.Sprintf("/environments/%s", c.Environment)
return
}
return
}

// NewCMA returns a CMA client
func NewCMA(token string) *Contentful {
c := &Contentful{
Expand Down
16 changes: 8 additions & 8 deletions entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func (service *EntriesService) GetEntryKey(entry *Entry, key string) (*EntryFiel

// List returns entries collection
func (service *EntriesService) List(spaceID string) *Collection {
path := fmt.Sprintf("/spaces/%s/entries", spaceID)
path := fmt.Sprintf("/spaces/%s%s/entries", spaceID, getEnvPath(service.c))
method := "GET"

req, err := service.c.newRequest(method, path, nil, nil)
Expand All @@ -75,7 +75,7 @@ func (service *EntriesService) List(spaceID string) *Collection {

// Sync returns entries collection
func (service *EntriesService) Sync(spaceID string, initial bool, syncToken ...string) *Collection {
path := fmt.Sprintf("/spaces/%s/sync", spaceID)
path := fmt.Sprintf("/spaces/%s%s/sync", spaceID, getEnvPath(service.c))
method := "GET"

req, err := service.c.newRequest(method, path, nil, nil)
Expand All @@ -98,7 +98,7 @@ func (service *EntriesService) Sync(spaceID string, initial bool, syncToken ...s

// Get returns a single entry
func (service *EntriesService) Get(spaceID, entryID string, locale ...string) (*Entry, error) {
path := fmt.Sprintf("/spaces/%s/entries/%s", spaceID, entryID)
path := fmt.Sprintf("/spaces/%s%s/entries/%s", spaceID, getEnvPath(service.c), entryID)
query := url.Values{}
if len(locale) > 0 {
query["locale"] = locale
Expand All @@ -120,7 +120,7 @@ func (service *EntriesService) Get(spaceID, entryID string, locale ...string) (*

// Delete the entry
func (service *EntriesService) Delete(spaceID string, entryID string) error {
path := fmt.Sprintf("/spaces/%s/entries/%s", spaceID, entryID)
path := fmt.Sprintf("/spaces/%s%s/entries/%s", spaceID, getEnvPath(service.c), entryID)
method := "DELETE"

req, err := service.c.newRequest(method, path, nil, nil)
Expand Down Expand Up @@ -151,10 +151,10 @@ func (service *EntriesService) Upsert(spaceID string, entry *Entry) error {
var method string

if entry.Sys != nil && entry.Sys.ID != "" {
path = fmt.Sprintf("/spaces/%s/entries/%s", spaceID, entry.Sys.ID)
path = fmt.Sprintf("/spaces/%s%s/entries/%s", spaceID, getEnvPath(service.c), entry.Sys.ID)
method = "PUT"
} else {
path = fmt.Sprintf("/spaces/%s/entries", spaceID)
path = fmt.Sprintf("/spaces/%s%s/entries", spaceID, getEnvPath(service.c))
method = "POST"
}

Expand All @@ -171,7 +171,7 @@ func (service *EntriesService) Upsert(spaceID string, entry *Entry) error {

// Publish the entry
func (service *EntriesService) Publish(spaceID string, entry *Entry) error {
path := fmt.Sprintf("/spaces/%s/entries/%s/published", spaceID, entry.Sys.ID)
path := fmt.Sprintf("/spaces/%s%s/entries/%s/published", spaceID, getEnvPath(service.c), entry.Sys.ID)
method := "PUT"

req, err := service.c.newRequest(method, path, nil, nil)
Expand All @@ -187,7 +187,7 @@ func (service *EntriesService) Publish(spaceID string, entry *Entry) error {

// Unpublish the entry
func (service *EntriesService) Unpublish(spaceID string, entry *Entry) error {
path := fmt.Sprintf("/spaces/%s/entries/%s/published", spaceID, entry.Sys.ID)
path := fmt.Sprintf("/spaces/%s%s/entries/%s/published", spaceID, getEnvPath(service.c), entry.Sys.ID)
method := "DELETE"

req, err := service.c.newRequest(method, path, nil, nil)
Expand Down
10 changes: 5 additions & 5 deletions locale.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func (locale *Locale) GetVersion() int {

// List returns a locales collection
func (service *LocalesService) List(spaceID string) *Collection {
path := fmt.Sprintf("/spaces/%s/locales", spaceID)
path := fmt.Sprintf("/spaces/%s%s/locales", spaceID, getEnvPath(service.c))
method := "GET"

req, err := service.c.newRequest(method, path, nil, nil)
Expand All @@ -65,7 +65,7 @@ func (service *LocalesService) List(spaceID string) *Collection {

// Get returns a single locale entity
func (service *LocalesService) Get(spaceID, localeID string) (*Locale, error) {
path := fmt.Sprintf("/spaces/%s/locales/%s", spaceID, localeID)
path := fmt.Sprintf("/spaces/%s%s/locales/%s", spaceID, getEnvPath(service.c), localeID)
method := "GET"

req, err := service.c.newRequest(method, path, nil, nil)
Expand All @@ -83,7 +83,7 @@ func (service *LocalesService) Get(spaceID, localeID string) (*Locale, error) {

// Delete the locale
func (service *LocalesService) Delete(spaceID string, locale *Locale) error {
path := fmt.Sprintf("/spaces/%s/locales/%s", spaceID, locale.Sys.ID)
path := fmt.Sprintf("/spaces/%s%s/locales/%s", spaceID, getEnvPath(service.c), locale.Sys.ID)
method := "DELETE"

req, err := service.c.newRequest(method, path, nil, nil)
Expand All @@ -108,10 +108,10 @@ func (service *LocalesService) Upsert(spaceID string, locale *Locale) error {
var method string

if locale.Sys != nil && locale.Sys.CreatedAt != "" {
path = fmt.Sprintf("/spaces/%s/locales/%s", spaceID, locale.Sys.ID)
path = fmt.Sprintf("/spaces/%s%s/locales/%s", spaceID, getEnvPath(service.c), locale.Sys.ID)
method = "PUT"
} else {
path = fmt.Sprintf("/spaces/%s/locales", spaceID)
path = fmt.Sprintf("/spaces/%s%s/locales", spaceID, getEnvPath(service.c))
method = "POST"
}

Expand Down
2 changes: 1 addition & 1 deletion space.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func (service *SpacesService) Upsert(space *Space) error {
var method string

if space.Sys != nil && space.Sys.CreatedAt != "" {
path = fmt.Sprintf("/spaces/%s", space.Sys.ID)
path = fmt.Sprintf("/spaces/%s%s", space.Sys.ID, getEnvPath(service.c))
method = "PUT"
} else {
path = "/spaces"
Expand Down
10 changes: 5 additions & 5 deletions webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func (webhook *Webhook) GetVersion() int {

// List returns webhooks collection
func (service *WebhooksService) List(spaceID string) *Collection {
path := fmt.Sprintf("/spaces/%s/webhook_definitions", spaceID)
path := fmt.Sprintf("/spaces/%s%s/webhook_definitions", spaceID, getEnvPath(service.c))
method := "GET"

req, err := service.c.newRequest(method, path, nil, nil)
Expand All @@ -56,7 +56,7 @@ func (service *WebhooksService) List(spaceID string) *Collection {

// Get returns a single webhook entity
func (service *WebhooksService) Get(spaceID, webhookID string) (*Webhook, error) {
path := fmt.Sprintf("/spaces/%s/webhook_definitions/%s", spaceID, webhookID)
path := fmt.Sprintf("/spaces/%s%s/webhook_definitions/%s", spaceID, getEnvPath(service.c), webhookID)
method := "GET"

req, err := service.c.newRequest(method, path, nil, nil)
Expand All @@ -83,10 +83,10 @@ func (service *WebhooksService) Upsert(spaceID string, webhook *Webhook) error {
var method string

if webhook.Sys != nil && webhook.Sys.CreatedAt != "" {
path = fmt.Sprintf("/spaces/%s/webhook_definitions/%s", spaceID, webhook.Sys.ID)
path = fmt.Sprintf("/spaces/%s%s/webhook_definitions/%s", spaceID, getEnvPath(service.c), webhook.Sys.ID)
method = "PUT"
} else {
path = fmt.Sprintf("/spaces/%s/webhook_definitions", spaceID)
path = fmt.Sprintf("/spaces/%s%s/webhook_definitions", spaceID, getEnvPath(service.c))
method = "POST"
}

Expand All @@ -102,7 +102,7 @@ func (service *WebhooksService) Upsert(spaceID string, webhook *Webhook) error {

// Delete the webhook
func (service *WebhooksService) Delete(spaceID string, webhook *Webhook) error {
path := fmt.Sprintf("/spaces/%s/webhook_definitions/%s", spaceID, webhook.Sys.ID)
path := fmt.Sprintf("/spaces/%s%s/webhook_definitions/%s", spaceID, getEnvPath(service.c), webhook.Sys.ID)
method := "DELETE"

req, err := service.c.newRequest(method, path, nil, nil)
Expand Down

0 comments on commit 4b8749e

Please sign in to comment.