corrected SnipcartProductsResponse, added GetProductById
This commit is contained in:
@@ -127,7 +127,7 @@ type SnipcartProductVariant struct {
|
|||||||
AllowBackorder bool `json:"allowOutOfStockPurchases"`
|
AllowBackorder bool `json:"allowOutOfStockPurchases"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type SnipcartProductsResponse struct {
|
type SnipcartProduct struct {
|
||||||
Token string `json:"id"`
|
Token string `json:"id"`
|
||||||
Id string `json:"userDefinedId"`
|
Id string `json:"userDefinedId"`
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
@@ -137,6 +137,21 @@ type SnipcartProductsResponse struct {
|
|||||||
Variants []SnipcartProductVariant `json:"variants"`
|
Variants []SnipcartProductVariant `json:"variants"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type SnipcartProductsResponse struct {
|
||||||
|
Keywords string `json:"keywords"`
|
||||||
|
UserDefinedId string `json:"userDefinedId"`
|
||||||
|
Archived bool `json:"archived"`
|
||||||
|
From time.Time `json:"from"`
|
||||||
|
To time.Time `json:"to"`
|
||||||
|
OrderBy string `json:"orderBy"`
|
||||||
|
Paginated bool `json:"hasMoreResults"`
|
||||||
|
TotalItems int `json:"totalItems"`
|
||||||
|
Offset int `json:"offset"`
|
||||||
|
Limit int `json:"limit"`
|
||||||
|
Sort []any `json:"sort"`
|
||||||
|
Items []SnipcartProduct `json:"items"`
|
||||||
|
}
|
||||||
|
|
||||||
func NewClient(snipcartApiKey string) *Client {
|
func NewClient(snipcartApiKey string) *Client {
|
||||||
return &Client{
|
return &Client{
|
||||||
SnipcartKey: snipcartApiKey,
|
SnipcartKey: snipcartApiKey,
|
||||||
@@ -281,3 +296,23 @@ func (s *Client) GetProducts(queries map[string]string) (*SnipcartProductsRespon
|
|||||||
|
|
||||||
return &products, nil
|
return &products, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *Client) GetProductById(id string) (*SnipcartProduct, error) {
|
||||||
|
response, err := helper.Get(productsUri, "Basic", s.AuthBase64, map[string]string{"userDefinedId": id})
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if response.StatusCode < 200 && response.StatusCode >= 300 {
|
||||||
|
return nil, fmt.Errorf("unexpected response received: %s", response.Status)
|
||||||
|
}
|
||||||
|
|
||||||
|
defer response.Body.Close()
|
||||||
|
|
||||||
|
var product SnipcartProduct
|
||||||
|
err = json.NewDecoder(response.Body).Decode(&product)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return &product, nil
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user