Compare commits

..

6 Commits

Author SHA1 Message Date
Bastian de Byl
5a74c2bae8 main corrected Operation in ProductCustomField to float64 from int 2023-05-12 15:23:14 -04:00
Bastian de Byl
d7f1571e09 main added CustomFields to snipcart product 2023-05-12 15:20:36 -04:00
Bastian de Byl
f18e21f181 main return only one product for GetProductById 2023-05-12 10:46:54 -04:00
Bastian de Byl
d56cf589da main correct the output of GetProductById 2023-05-12 03:14:07 -04:00
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
2 changed files with 90 additions and 32 deletions

View File

@@ -1,32 +0,0 @@
package main
import (
"flag"
"log"
"github.com/debyltech/go-snipcart/snipcart"
)
func main() {
snipcartApiKey := flag.String("key", "", "Snipcart API Key")
flag.Parse()
if *snipcartApiKey == "" {
log.Fatal("missing -key flag")
}
Client := snipcart.NewClient(*snipcartApiKey)
updateOrder := snipcart.SnipcartOrderUpdate{
Status: snipcart.Delivered,
}
response, err := Client.UpdateOrder("b35990df-c0ca-4014-94de-1caa7bd7bb51", &updateOrder)
if err != nil {
log.Fatal(err)
}
for k, v := range response.Items {
log.Printf("%v: %v\n", k, v)
}
}

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,50 @@ 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 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 { func NewClient(snipcartApiKey string) *Client {
return &Client{ return &Client{
SnipcartKey: snipcartApiKey, SnipcartKey: snipcartApiKey,
@@ -243,3 +289,47 @@ 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 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
}