initial commit
This commit is contained in:
3
.gitignore
vendored
Normal file
3
.gitignore
vendored
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
*.json
|
||||||
|
*.sum
|
||||||
|
config/*
|
||||||
32
example.go
Normal file
32
example.go
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"flag"
|
||||||
|
"log"
|
||||||
|
|
||||||
|
"github.com/debyltech/go-snipcart/snipcart"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
configFile = "config.json"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
snipcartApiKey := flag.String("key", "", "Snipcart API Key")
|
||||||
|
flag.Parse()
|
||||||
|
|
||||||
|
if *snipcartApiKey == "" {
|
||||||
|
log.Fatal("missing -key flag")
|
||||||
|
}
|
||||||
|
|
||||||
|
snipcartProvider := snipcart.NewSnipcartProvider(*snipcartApiKey)
|
||||||
|
|
||||||
|
response, err := snipcartProvider.GetOrdersByStatus(snipcart.Processed)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
for k, v := range response.Items {
|
||||||
|
log.Printf("%v: %v\n", k, v)
|
||||||
|
}
|
||||||
|
}
|
||||||
39
snipcart/helper.go
Normal file
39
snipcart/helper.go
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
package snipcart
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
|
type URLQuery struct {
|
||||||
|
Key string
|
||||||
|
Value string
|
||||||
|
}
|
||||||
|
|
||||||
|
func JSONGet(uri string, authName string, authValue string, queries []URLQuery) (*http.Response, error) {
|
||||||
|
client := &http.Client{}
|
||||||
|
|
||||||
|
request, err := http.NewRequest("GET", uri, nil)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
request.Header.Set("Accept", "application/json")
|
||||||
|
request.Header.Set("Authorization", fmt.Sprintf("%s %s", authName, authValue))
|
||||||
|
|
||||||
|
if len(queries) > 0 {
|
||||||
|
q := request.URL.Query()
|
||||||
|
|
||||||
|
for _, query := range queries {
|
||||||
|
q.Add(query.Key, query.Value)
|
||||||
|
}
|
||||||
|
|
||||||
|
request.URL.RawQuery = q.Encode()
|
||||||
|
}
|
||||||
|
|
||||||
|
response, err := client.Do(request)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return response, nil
|
||||||
|
}
|
||||||
13
snipcart/orderstatus.go
Normal file
13
snipcart/orderstatus.go
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
package snipcart
|
||||||
|
|
||||||
|
type OrderStatus string
|
||||||
|
|
||||||
|
const (
|
||||||
|
Processed OrderStatus = "Processed"
|
||||||
|
Disputed = "Disputed"
|
||||||
|
Shipped = "Shipped"
|
||||||
|
Delivered = "Delivered"
|
||||||
|
Pending = "Pending"
|
||||||
|
Cancelled = "Cancelled"
|
||||||
|
Dispatched = "Dispatched"
|
||||||
|
)
|
||||||
99
snipcart/snipcart.go
Normal file
99
snipcart/snipcart.go
Normal file
@@ -0,0 +1,99 @@
|
|||||||
|
package snipcart
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/base64"
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"strconv"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
defaultLimit = 50
|
||||||
|
apiUri = "https://app.snipcart.com"
|
||||||
|
ordersPath = "/api/orders"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
orderUri = apiUri + ordersPath
|
||||||
|
)
|
||||||
|
|
||||||
|
type SnipcartProvider struct {
|
||||||
|
SnipcartKey string
|
||||||
|
AuthBase64 string
|
||||||
|
Limit int
|
||||||
|
}
|
||||||
|
|
||||||
|
type SnipcartItem struct {
|
||||||
|
UUID string `json:"uniqueId"`
|
||||||
|
ID string `json:"id"`
|
||||||
|
Name string `json:"name"`
|
||||||
|
Quantity float64 `json:"quantity"`
|
||||||
|
Weight string `json:"weight"`
|
||||||
|
TotalWeight float64 `json:"totalWeight"`
|
||||||
|
CustomFieldsJSON string `json:"customFieldsJson"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type SnipcartOrder struct {
|
||||||
|
Token string `json:"token"`
|
||||||
|
Invoice string `json:"invoiceNumber"`
|
||||||
|
Status string `json:"status"`
|
||||||
|
TotalWeight float64 `json:"totalWeight"`
|
||||||
|
Email string `json:"email"`
|
||||||
|
Name string `json:"shippingAddressName"`
|
||||||
|
Address1 string `json:"shippingAddressAddress1"`
|
||||||
|
Address2 string `json:"shippingAddressAddress2"`
|
||||||
|
City string `json:"shippingAddressCity"`
|
||||||
|
Province string `json:"shippingAddressProvince"`
|
||||||
|
Country string `json:"shippingAddressCountry"`
|
||||||
|
PostalCode string `json:"shippingAddressPostalCode"`
|
||||||
|
Phone string `json:"shippingAddressPhone"`
|
||||||
|
Items []SnipcartItem `json:"items"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type SnipcartOrders struct {
|
||||||
|
TotalItems int
|
||||||
|
Items []SnipcartOrder
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewSnipcartProvider(snipcartApiKey string) SnipcartProvider {
|
||||||
|
return SnipcartProvider{
|
||||||
|
SnipcartKey: snipcartApiKey,
|
||||||
|
AuthBase64: base64.StdEncoding.EncodeToString([]byte(snipcartApiKey + ":")),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *SnipcartProvider) GetOrders() (*SnipcartOrders, error) {
|
||||||
|
return s.GetOrdersByStatus("")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *SnipcartProvider) GetOrdersByStatus(status OrderStatus) (*SnipcartOrders, error) {
|
||||||
|
if s.Limit == 0 {
|
||||||
|
s.Limit = defaultLimit
|
||||||
|
}
|
||||||
|
|
||||||
|
queries := []URLQuery{
|
||||||
|
{Key: "limit", Value: strconv.Itoa(s.Limit)},
|
||||||
|
}
|
||||||
|
|
||||||
|
if status != "" {
|
||||||
|
queries = append(queries, URLQuery{Key: "status", Value: string(status)})
|
||||||
|
}
|
||||||
|
|
||||||
|
response, err := JSONGet(orderUri, "Basic", s.AuthBase64, queries)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if response.Status != "200 OK" {
|
||||||
|
return nil, fmt.Errorf("unexpected response received: %s", response.Status)
|
||||||
|
}
|
||||||
|
|
||||||
|
defer response.Body.Close()
|
||||||
|
|
||||||
|
var orders SnipcartOrders
|
||||||
|
err = json.NewDecoder(response.Body).Decode(&orders)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return &orders, nil
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user