Skip to content

Commit

Permalink
Add Collection endpoint (bold-commerce#115)
Browse files Browse the repository at this point in the history
Co-authored-by: Geoffrey Chen <[email protected]>
  • Loading branch information
gcfresh and Geoffrey Chen authored Sep 8, 2020
1 parent decc076 commit cd587cf
Show file tree
Hide file tree
Showing 3 changed files with 477 additions and 0 deletions.
82 changes: 82 additions & 0 deletions collection.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package goshopify

import (
"fmt"
"net/http"
"time"
)

const collectionsBasePath = "collections"

// CollectionService is an interface for interfacing with the collection endpoints
// of the Shopify API.
// See: https://help.shopify.com/api/reference/products/collection
type CollectionService interface {
Get(collectionID int64, options interface{}) (*Collection, error)
ListProducts(collectionID int64, options interface{}) ([]Product, error)
ListProductsWithPagination(collectionID int64,options interface{}) ([]Product, *Pagination, error)
}

// CollectionServiceOp handles communication with the collection related methods of
// the Shopify API.
type CollectionServiceOp struct {
client *Client
}

// Collection represents a Shopify collection
type Collection struct {
ID int64 `json:"id"`
Handle string `json:"handle"`
Title string `json:"title"`
UpdatedAt *time.Time `json:"updated_at"`
BodyHTML string `json:"body_html"`
SortOrder string `json:"sort_order"`
TemplateSuffix string `json:"template_suffix"`
Image Image `json:"image"`
PublishedAt *time.Time `json:"published_at"`
PublishedScope string `json:"published_scope"`
}

// Represents the result from the collections/X.json endpoint
type CollectionResource struct {
Collection *Collection `json:"collection"`
}

// Get individual collection
func (s *CollectionServiceOp) Get(collectionID int64, options interface{}) (*Collection, error) {
path := fmt.Sprintf("%s/%d.json", collectionsBasePath, collectionID)
resource := new(CollectionResource)
err := s.client.Get(path, resource, options)
return resource.Collection, err
}

// List products for a collection
func (s *CollectionServiceOp) ListProducts(collectionID int64, options interface{}) ([]Product, error) {
products, _, err := s.ListProductsWithPagination(collectionID, options)
if err != nil {
return nil, err
}
return products, nil
}

// List products for a collection and return pagination to retrieve next/previous results.
func (s *CollectionServiceOp) ListProductsWithPagination(collectionID int64,options interface{}) ([]Product, *Pagination, error) {
path := fmt.Sprintf("%s/%d/products.json", collectionsBasePath, collectionID)
resource := new(ProductsResource)
headers := http.Header{}

headers, err := s.client.createAndDoGetHeaders("GET", path, nil, options, resource)
if err != nil {
return nil, nil, err
}

// Extract pagination info from header
linkHeader := headers.Get("Link")

pagination, err := extractPagination(linkHeader)
if err != nil {
return nil, nil, err
}

return resource.Products, pagination, nil
}
Loading

0 comments on commit cd587cf

Please sign in to comment.