Skip to content
This repository has been archived by the owner on Jun 18, 2021. It is now read-only.

Allow retrieving localized assets #62

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 28 additions & 4 deletions asset.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,19 @@ func (asset *Asset) UnmarshalJSON(data []byte) error {
}

asset.Fields = &FileFields{
Title: title.(string),
Description: description.(string),
File: &File{},
Title: title.(string),
File: &File{},
}
if description != nil {
asset.Fields.Description = description.(string)
}

file := payload["fields"].(map[string]interface{})["file"].(map[string]interface{})[asset.locale]
if err := json.Unmarshal([]byte(file.(string)), asset.Fields.File); err != nil {
marshal, err := json.Marshal(file.(map[string]interface{}))
if err != nil {
return err
}
if err := json.Unmarshal(marshal, asset.Fields.File); err != nil {
return err
}
} else {
Expand Down Expand Up @@ -171,6 +177,24 @@ func (service *AssetsService) Get(spaceID, assetID string) (*Asset, error) {
return &asset, nil
}

// GetLocalized returns a single asset entity. You need to specify the target locale you wish to retrieve.
func (service *AssetsService) GetLocalized(spaceID, assetID string, locale string) (*Asset, error) {
path := fmt.Sprintf("/spaces/%s/assets/%s", spaceID, assetID)
method := "GET"

req, err := service.c.newRequest(method, path, nil, nil)
if err != nil {
return nil, err
}

asset := Asset{locale: locale}
if err := service.c.do(req, &asset); err != nil {
return nil, err
}

return &asset, nil
}

// Upsert updates or creates a new asset entity
func (service *AssetsService) Upsert(spaceID string, asset *Asset) error {
bytesArray, err := json.Marshal(asset)
Expand Down