Skip to content

Commit a3b468e

Browse files
committed
First commit.
0 parents  commit a3b468e

File tree

9 files changed

+257
-0
lines changed

9 files changed

+257
-0
lines changed

.DS_Store

6 KB
Binary file not shown.

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
go-shopify
2+
==========
3+
4+
Golang tool for connecting to Shopify's API

example/.DS_Store

6 KB
Binary file not shown.

example/example

4.76 MB
Binary file not shown.

example/example.exe

6.7 MB
Binary file not shown.

example/example.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"github.com/hammond-bones/go-shopify/shopify"
6+
"strconv"
7+
)
8+
9+
func main() {
10+
fmt.Printf("Hello!\n")
11+
shop := shopify.NewClient("mcglynn-quitzon-and-windler8990", "98bfc43d6aa771567326d76e6395173d")
12+
13+
shop.LoadProducts()
14+
15+
fmt.Printf("%v\n", shop.Products[0].Id)
16+
17+
prod := shop.GetLiveProduct(strconv.Itoa(shop.Products[0].Id))
18+
19+
fmt.Printf("%v\n", prod.Handle)
20+
}

shopify/.DS_Store

6 KB
Binary file not shown.

shopify/shopify.go

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
package shopify
2+
3+
import (
4+
"bytes"
5+
"encoding/json"
6+
"fmt"
7+
"log"
8+
"net/http"
9+
"time"
10+
)
11+
12+
const (
13+
baseUrlString = ".myshopify.com/"
14+
)
15+
16+
type Shopify struct {
17+
shopifyDomain string
18+
shopifySecretToken string
19+
20+
Products []ShopifyProduct
21+
}
22+
23+
func NewClient(domain string, secrettoken string) Shopify {
24+
shop := Shopify{shopifyDomain: domain, shopifySecretToken: secrettoken}
25+
return shop
26+
}
27+
28+
func (shopifyClient *Shopify) LoadProducts() {
29+
//load first block of 250
30+
urlStr := "admin/products.json?limit=250&page=1"
31+
var page int = 1
32+
33+
var shopifyResponse = new(ShopifyResponse)
34+
shopifyClient.MakeRequest("GET", urlStr, shopifyResponse)
35+
36+
shopifyClient.Products = shopifyResponse.Products[:]
37+
38+
//if response was == 250 products
39+
if len(shopifyResponse.Products) == 250 {
40+
//load every successive block
41+
var done bool = false
42+
//var lastId int = shopifyResponse.Products[249].Id
43+
44+
for done == false {
45+
log.Printf("Shopify: Loaded page %v, that's %v products!\n", page, len(shopifyClient.Products))
46+
page++
47+
//get this thread to wait 0.5 seconds
48+
time.Sleep(time.Second / 2)
49+
urlStr = fmt.Sprintf("admin/products.json?limit=250&page=%v", page)
50+
shopifyResponse = new(ShopifyResponse)
51+
shopifyClient.MakeRequest("GET", urlStr, shopifyResponse)
52+
if len(shopifyResponse.Products) > 0 {
53+
shopifyClient.Products = append(shopifyClient.Products, shopifyResponse.Products[:]...)
54+
//lastId = shopifyResponse.Products[len(shopifyResponse.Products)-1].Id
55+
} else {
56+
done = true
57+
}
58+
59+
}
60+
61+
}
62+
return
63+
64+
}
65+
66+
func (shopifyClient *Shopify) GetLiveProduct(shopifyId string) ShopifyProduct {
67+
urlStr := "admin/products/" + shopifyId + ".json"
68+
var shopifyResponse = new(ShopifyResponse)
69+
70+
shopifyClient.MakeRequest("GET", urlStr, shopifyResponse)
71+
72+
fmt.Printf("%v\n", shopifyResponse.Product.Id)
73+
74+
return shopifyResponse.Product
75+
}
76+
77+
func (shopifyClient *Shopify) MakeRequest(method string, urlStr string, body interface{}) {
78+
url := fmt.Sprintf("https://%s%s%s", shopifyClient.shopifyDomain, baseUrlString, urlStr)
79+
client := &http.Client{}
80+
buf := new(bytes.Buffer)
81+
r, err := http.NewRequest(method, url, buf)
82+
r.Header.Add("X-Shopify-Access-Token", shopifyClient.shopifySecretToken)
83+
resp, err := client.Do(r)
84+
defer resp.Body.Close()
85+
if resp.StatusCode == http.StatusNotFound {
86+
fmt.Printf("404 on executing request: %s", url)
87+
} else if resp.StatusCode == 429 {
88+
fmt.Printf("Rate limited!")
89+
}
90+
if err != nil {
91+
fmt.Printf("Error executing request : %s", err)
92+
}
93+
94+
err = json.NewDecoder(resp.Body).Decode(body)
95+
if err != nil {
96+
fmt.Print(err)
97+
fmt.Print(resp.Body)
98+
}
99+
}

shopify/shopifyproduct.go

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
/*
2+
3+
{
4+
"body_html": null,
5+
"created_at": "2013-12-10T03:40:05-05:00",
6+
"handle": "cheese",
7+
"id": 191302485,
8+
"product_type": "General",
9+
"published_at": "2013-12-10T21:29:21-05:00",
10+
"published_scope": "global",
11+
"template_suffix": null,
12+
"title": "Cheese",
13+
"updated_at": "2014-02-11T19:37:59-05:00",
14+
"vendor": "Not specified",
15+
"tags": "",
16+
"variants": [
17+
{
18+
"barcode": null,
19+
"compare_at_price": null,
20+
"created_at": "2013-12-10T03:40:05-05:00",
21+
"fulfillment_service": "manual",
22+
"grams": 0,
23+
"id": 437938881,
24+
"inventory_management": "shopify",
25+
"inventory_policy": "deny",
26+
"option1": "Default",
27+
"option2": null,
28+
"option3": null,
29+
"position": 1,
30+
"price": "23.00",
31+
"product_id": 191302485,
32+
"requires_shipping": true,
33+
"sku": "10061",
34+
"taxable": true,
35+
"title": "Default",
36+
"updated_at": "2013-12-10T03:40:05-05:00",
37+
"inventory_quantity": 20,
38+
"old_inventory_quantity": 20
39+
}
40+
],
41+
"options": [
42+
{
43+
"id": 228536785,
44+
"name": "Title",
45+
"position": 1,
46+
"product_id": 191302485
47+
}
48+
],
49+
"images": [
50+
{
51+
"created_at": "2013-12-10T03:40:12-05:00",
52+
"id": 391973353,
53+
"position": 1,
54+
"product_id": 191302505,
55+
"updated_at": "2013-12-10T03:40:12-05:00",
56+
"src": "http://cdn.shopify.com/s/files/1/0199/1250/products/df4331ec645329971ea45cdfb77315fead191d32.jpeg?v=1386664812"
57+
}
58+
],
59+
"image": {
60+
"created_at": "2013-12-10T03:40:12-05:00",
61+
"id": 391973353,
62+
"position": 1,
63+
"product_id": 191302505,
64+
"updated_at": "2013-12-10T03:40:12-05:00",
65+
"src": "http://cdn.shopify.com/s/files/1/0199/1250/products/df4331ec645329971ea45cdfb77315fead191d32.jpeg?v=1386664812"
66+
}
67+
},
68+
*/
69+
70+
package shopify
71+
72+
type ShopifyProduct struct {
73+
BodyHtml string `json:"body_html,omitempty"`
74+
CreatedAt string `json:"created_at,omitempty"`
75+
Handle string `json:"handle,omitempty"`
76+
Id int `json:"id,omitempty"`
77+
ProductType string `json:"product_type,omitempty"`
78+
PublishedAt string `json:"published_at,omitempty"`
79+
PublishedScope string `json:"published_scope,omitempty"`
80+
TemplateSuffix string `json:"template_suffix,omitempty"`
81+
Title string `json:"title,omitempty"`
82+
UpdatedAt string `json:"updated_at,omitempty"`
83+
Vendor string `json:"vendor,omitempty"`
84+
Tags string `json:"tags,omitempty"`
85+
Variants []ShopifyProductVariant `json:"variants,omitempty"`
86+
Options []ShopifyProductOption `json:"options,omitempty"`
87+
Images []ShopifyProductImage `json:"images,omitempty"`
88+
Image ShopifyProductImage `json:"image,omitempty"`
89+
}
90+
91+
type ShopifyProductVariant struct {
92+
Barcode string `json:"barcode,omitempty"`
93+
CompareAtPrice string `json:"compare_at_price,omitempty"`
94+
CreatedAt string `json:"created_at,omitempty"`
95+
FulfillmentService string `json:"fulfillment_service,omitempty"`
96+
Grams int `json:"grams,omitempty"`
97+
Id int `json:"id,omitempty"`
98+
InventoryManagement string `json:"inventory_management,omitempty"`
99+
InventoryPolicy string `json:"inventory_policy,omitempty"`
100+
Option1 string `json:"option1,omitempty"`
101+
Option2 string `json:"option2,omitempty"`
102+
Option3 string `json:"option3,omitempty"`
103+
Position int `json:"position,omitempty"`
104+
Price string `json:"price,omitempty"`
105+
ProductId int `json:"product_id,omitempty"`
106+
RequiresShipping bool `json:"requires_shopping,omitempty"`
107+
Sku string `json:"sku,omitempty"`
108+
Taxable bool `json:"taxable,omitempty"`
109+
Title string `json:"title,omitempty"`
110+
UpdatedAt string `json:"updated_at,omitempty"`
111+
InventoryQuantity float64 `json:"inventory_quantity,omitempty"`
112+
OldInventoryQuantity int `json:"old_inventory_quantity,omitempty"`
113+
}
114+
115+
type ShopifyProductOption struct {
116+
Id int `json:"id,omitempty"`
117+
Name string `json:"name,omitempty"`
118+
Position int `json:"position,omitempty"`
119+
ProductId int `json:"product_id,omitempty"`
120+
}
121+
122+
type ShopifyProductImage struct {
123+
CreatedAt string `json:"created_at,omitempty"`
124+
Id int `json:"id,omitempty"`
125+
Position int `json:"position,omitempty"`
126+
ProductId int `json:"product_id,omitempty"`
127+
UpdatedAt string `json:"updated_at",omitempty"`
128+
Src string `json:"src,omitempty"`
129+
}
130+
131+
type ShopifyResponse struct {
132+
Products []ShopifyProduct `json:"products,omitempty"`
133+
Product ShopifyProduct `json:"product,omitempty"`
134+
}

0 commit comments

Comments
 (0)