|
|
|
|
@@ -16,11 +16,13 @@ const (
|
|
|
|
|
defaultLimit = 50
|
|
|
|
|
apiUri = "https://app.snipcart.com"
|
|
|
|
|
ordersPath = "/api/orders"
|
|
|
|
|
productsPath = "/api/products"
|
|
|
|
|
validationPath = "/api/requestvalidation/"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var (
|
|
|
|
|
orderUri = apiUri + ordersPath
|
|
|
|
|
productsUri = apiUri + productsPath
|
|
|
|
|
validationUri = apiUri + validationPath
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
@@ -119,6 +121,50 @@ type SnipcartNotificationResponse struct {
|
|
|
|
|
SentOn time.Time `json:"sentOn"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type SnipcartProductVariant struct {
|
|
|
|
|
Stock int `json:"stock"`
|
|
|
|
|
Variation []any `json:"variation"`
|
|
|
|
|
AllowBackorder bool `json:"allowOutOfStockPurchases"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type SnipcartProductCustomField struct {
|
|
|
|
|
Name string `json:"name"`
|
|
|
|
|
Placeholder string `json:"placeholder"`
|
|
|
|
|
DisplayValue string `json:"displayValue"`
|
|
|
|
|
Type string `json:"type"`
|
|
|
|
|
Options string `json:"options"`
|
|
|
|
|
Required bool `json:"required"`
|
|
|
|
|
Value string `json:"value"`
|
|
|
|
|
Operation float64 `json:"operation"`
|
|
|
|
|
OptionsArray []string `json:"optionsArray"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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"`
|
|
|
|
|
CustomFields []SnipcartProductCustomField `json:"customFields"`
|
|
|
|
|
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 {
|
|
|
|
|
return &Client{
|
|
|
|
|
SnipcartKey: snipcartApiKey,
|
|
|
|
|
@@ -243,3 +289,47 @@ func (s *Client) ValidateWebhook(token string) error {
|
|
|
|
|
|
|
|
|
|
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 products SnipcartProductsResponse
|
|
|
|
|
err = json.NewDecoder(response.Body).Decode(&products)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if len(products.Items) < 1 {
|
|
|
|
|
return nil, fmt.Errorf("no products with id '%s'", id)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return &products.Items[0], nil
|
|
|
|
|
}
|
|
|
|
|
|