Skip to content

Commit

Permalink
Merge branch 'master' of github.com:ONSdigital/dp-api-clients-go
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidSubiros committed Aug 11, 2020
2 parents 7bd1b88 + 7130a55 commit 9911631
Show file tree
Hide file tree
Showing 8 changed files with 209 additions and 200 deletions.
50 changes: 37 additions & 13 deletions image/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,28 +40,46 @@ type Image struct {
//- deleted
//- failed_import
//- failed_publish
Filename string `json:"filename,omitempty"`
License License `json:"license,omitempty"`
Upload ImageUpload `json:"upload,omitempty"`
Type string `json:"type,omitempty"`
Downloads map[string]ImageDownload `json:"downloads,omitempty"`
Filename string `json:"filename,omitempty"`
License License `json:"license,omitempty"`
Upload ImageUpload `json:"upload,omitempty"`
Type string `json:"type,omitempty"`
Links *ImageLinks `json:"links,omitempty"`
}

// ImageUpload represents the fields for an Image Upload
type ImageUpload struct {
Path string `json:"path,omitempty"`
}

// ImageLinks represents the fields for the image HATEOAS links
type ImageLinks struct {
Self string `json:"self"`
Downloads string `json:"downloads"`
}

// Images represents the fields for a group of image download variants as returned by Image API
type ImageDownloads struct {
Count int `json:"count"`
Items []ImageDownload `json:"items"`
Limit int `json:"limit"`
Offset int `json:"offset"`
TotalCount int `json:"total_count"`
}

// ImageDownload represents the fields for an Image Download
type ImageDownload struct {
Size int `json:"size,omitempty"`
Type string `json:"type,omitempty"`
Width *int `json:"width,omitempty"`
Height *int `json:"height,omitempty"`
Public bool `json:"public,omitempty"`
Href string `json:"href,omitempty"`
Private string `json:"private,omitempty"`
State string `json:"state,omitempty"`
Id string `json:"id,omitempty"`
Size int `json:"size,omitempty"`
Palette string `json:"palette,omitempty"`
Type string `json:"type,omitempty"`
Width *int `json:"width,omitempty"`
Height *int `json:"height,omitempty"`
Public bool `json:"public,omitempty"`
Href string `json:"href,omitempty"`
Links *ImageDownloadLinks `json:"links,omitempty"`
Private string `json:"private,omitempty"`
State string `json:"state,omitempty"`
//enum:
//- pending
//- importing
Expand All @@ -75,3 +93,9 @@ type ImageDownload struct {
PublishStarted *time.Time `json:"publish_started,omitempty"`
PublishCompleted *time.Time `json:"publish_completed,omitempty"`
}

// ImageDownloadLinks represents the fields for the image download HATEOAS links
type ImageDownloadLinks struct {
Self string `json:"self"`
Image string `json:"image"`
}
64 changes: 30 additions & 34 deletions image/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,18 +213,13 @@ func (c *Client) PutImage(ctx context.Context, userAuthToken, serviceAuthToken,
return
}

// PostImageUpload specifies download variants and triggers image import
func (c *Client) PostImageUpload(ctx context.Context, userAuthToken, serviceAuthToken, collectionID, imageID string, data ImageUpload) (err error) {
payload, err := json.Marshal(data)
if err != nil {
return
}

uri := fmt.Sprintf("%s/images/%s/upload", c.hcCli.URL, imageID)
// GetDownloadVariants returns the list of download variants for an image
func (c *Client) GetDownloadVariants(ctx context.Context, userAuthToken, serviceAuthToken, collectionID, imageID string) (m ImageDownloads, err error) {
uri := fmt.Sprintf("%s/images/%s/downloads", c.hcCli.URL, imageID)

clientlog.Do(ctx, "publishing image", service, uri)
clientlog.Do(ctx, "retrieving download variants", service, uri)

resp, err := c.doPostWithAuthHeaders(ctx, userAuthToken, serviceAuthToken, collectionID, uri, payload)
resp, err := c.doGetWithAuthHeaders(ctx, userAuthToken, serviceAuthToken, collectionID, uri, nil)
if err != nil {
return
}
Expand All @@ -234,21 +229,26 @@ func (c *Client) PostImageUpload(ctx context.Context, userAuthToken, serviceAuth
err = NewImageAPIResponse(resp, uri)
return
}
return
}

// PutDownloadVariant updates the specified download variant for the specified image
func (c *Client) PutDownloadVariant(ctx context.Context, userAuthToken, serviceAuthToken, collectionID, imageID, variant string, data ImageDownload) (m ImageDownload, err error) {
payload, err := json.Marshal(data)
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
return
}

if err = json.Unmarshal(b, &m); err != nil {
return
}

return
}

// GetDownloadVariant returns a requested download variant for an image
func (c *Client) GetDownloadVariant(ctx context.Context, userAuthToken, serviceAuthToken, collectionID, imageID, variant string) (m ImageDownload, err error) {
uri := fmt.Sprintf("%s/images/%s/downloads/%s", c.hcCli.URL, imageID, variant)

clientlog.Do(ctx, "updating image download variant", service, uri)
clientlog.Do(ctx, "retrieving download variant", service, uri)

resp, err := c.doPutWithAuthHeaders(ctx, userAuthToken, serviceAuthToken, collectionID, uri, payload)
resp, err := c.doGetWithAuthHeaders(ctx, userAuthToken, serviceAuthToken, collectionID, uri, nil)
if err != nil {
return
}
Expand All @@ -271,13 +271,18 @@ func (c *Client) PutDownloadVariant(ctx context.Context, userAuthToken, serviceA
return
}

//ImportDownloadVariant triggers a download variant import
func (c *Client) ImportDownloadVariant(ctx context.Context, userAuthToken, serviceAuthToken, collectionID, imageID, variant string) (err error) {
uri := fmt.Sprintf("%s/images/%s/downloads/%s/import", c.hcCli.URL, imageID, variant)
// PutDownloadVariant updates the specified download variant for the specified image
func (c *Client) PutDownloadVariant(ctx context.Context, userAuthToken, serviceAuthToken, collectionID, imageID, variant string, data ImageDownload) (m ImageDownload, err error) {
payload, err := json.Marshal(data)
if err != nil {
return
}

uri := fmt.Sprintf("%s/images/%s/downloads/%s", c.hcCli.URL, imageID, variant)

clientlog.Do(ctx, "importing image download variant", service, uri)
clientlog.Do(ctx, "updating image download variant", service, uri)

resp, err := c.doPostWithAuthHeaders(ctx, userAuthToken, serviceAuthToken, collectionID, uri, []byte{})
resp, err := c.doPutWithAuthHeaders(ctx, userAuthToken, serviceAuthToken, collectionID, uri, payload)
if err != nil {
return
}
Expand All @@ -287,25 +292,16 @@ func (c *Client) ImportDownloadVariant(ctx context.Context, userAuthToken, servi
err = NewImageAPIResponse(resp, uri)
return
}
return
}

//CompleteDownloadVariant triggers a download variant import
func (c *Client) CompleteDownloadVariant(ctx context.Context, userAuthToken, serviceAuthToken, collectionID, imageID, variant string) (err error) {
uri := fmt.Sprintf("%s/images/%s/downloads/%s/complete", c.hcCli.URL, imageID, variant)

clientlog.Do(ctx, "completing image download variant", service, uri)

resp, err := c.doPostWithAuthHeaders(ctx, userAuthToken, serviceAuthToken, collectionID, uri, []byte{})
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
return
}
defer closeResponseBody(ctx, resp)

if resp.StatusCode != http.StatusOK {
err = NewImageAPIResponse(resp, uri)
if err = json.Unmarshal(b, &m); err != nil {
return
}

return
}

Expand Down
Loading

0 comments on commit 9911631

Please sign in to comment.