Compare commits

...

2 Commits

Author SHA1 Message Date
Bastian de Byl
9e631a91b1 corrected SnipcartProductsResponse, added GetProductById 2023-05-12 00:36:50 -04:00
Bastian de Byl
e575ff03a8 added GetProducts, SnipcartProductsResponse, SnipcartProductVariant 2023-05-12 00:01:55 -04:00

View File

@@ -16,11 +16,13 @@ const (
defaultLimit = 50 defaultLimit = 50
apiUri = "https://app.snipcart.com" apiUri = "https://app.snipcart.com"
ordersPath = "/api/orders" ordersPath = "/api/orders"
productsPath = "/api/products"
validationPath = "/api/requestvalidation/" validationPath = "/api/requestvalidation/"
) )
var ( var (
orderUri = apiUri + ordersPath orderUri = apiUri + ordersPath
productsUri = apiUri + productsPath
validationUri = apiUri + validationPath validationUri = apiUri + validationPath
) )
@@ -119,6 +121,37 @@ type SnipcartNotificationResponse struct {
SentOn time.Time `json:"sentOn"` SentOn time.Time `json:"sentOn"`
} }
type SnipcartProductVariant struct {
Stock int `json:"stock"`
Variation []any `json:"variation"`
AllowBackorder bool `json:"allowOutOfStockPurchases"`
}
type SnipcartProduct struct {
Token string `json:"id"`
Id string `json:"userDefinedId"`
Name string `json:"name"`
Stock int `json:"stock"`
TotalStock int `json:"totalStock"`
AllowBackorder bool `json:"allowOutOfStockPurchases"`
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,
@@ -243,3 +276,43 @@ func (s *Client) ValidateWebhook(token string) error {
return nil return nil
} }
func (s *Client) GetProducts(queries map[string]string) (*SnipcartProductsResponse, error) {
response, err := helper.Get(productsUri, "Basic", s.AuthBase64, queries)
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 products SnipcartProductsResponse
err = json.NewDecoder(response.Body).Decode(&products)
if err != nil {
return nil, err
}
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
}