forked from bold-commerce/go-shopify
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Collection endpoint (bold-commerce#115)
Co-authored-by: Geoffrey Chen <[email protected]>
- Loading branch information
Showing
3 changed files
with
477 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Oops, something went wrong.