Skip to content

Commit ba09e7b

Browse files
committed
removed warnings, changed some names
1 parent 8eead99 commit ba09e7b

File tree

2 files changed

+162
-151
lines changed

2 files changed

+162
-151
lines changed

shopify/shopify.go

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,35 +10,38 @@ import (
1010
)
1111

1212
const (
13-
baseUrlString = ".myshopify.com/"
13+
baseURLString = ".myshopify.com/"
1414
)
1515

16+
// Shopify models the shopify client parameters
1617
type Shopify struct {
1718
shopifyDomain string
1819
shopifySecretToken string
1920

20-
Products []ShopifyProduct
21+
Products []Product
2122
}
2223

24+
// NewClient inits shopify client
2325
func NewClient(domain string, secrettoken string) Shopify {
2426
shop := Shopify{shopifyDomain: domain, shopifySecretToken: secrettoken}
2527
return shop
2628
}
2729

30+
// LoadProducts returns first block of 250
2831
func (shopifyClient *Shopify) LoadProducts() {
29-
//load first block of 250
32+
3033
urlStr := "admin/products.json?limit=250&page=1"
31-
var page int = 1
34+
var page = 1
3235

33-
var shopifyResponse = new(ShopifyResponse)
34-
shopifyClient.MakeRequest("GET", urlStr, shopifyResponse)
36+
var shopifyResponse = new(productResponse)
37+
shopifyClient.makeRequest("GET", urlStr, shopifyResponse)
3538

3639
shopifyClient.Products = shopifyResponse.Products[:]
3740

3841
//if response was == 250 products
3942
if len(shopifyResponse.Products) == 250 {
4043
//load every successive block
41-
var done bool = false
44+
var done = false
4245
//var lastId int = shopifyResponse.Products[249].Id
4346

4447
for done == false {
@@ -47,8 +50,8 @@ func (shopifyClient *Shopify) LoadProducts() {
4750
//get this thread to wait 0.5 seconds
4851
time.Sleep(time.Second / 2)
4952
urlStr = fmt.Sprintf("admin/products.json?limit=250&page=%v", page)
50-
shopifyResponse = new(ShopifyResponse)
51-
shopifyClient.MakeRequest("GET", urlStr, shopifyResponse)
53+
shopifyResponse = new(productResponse)
54+
shopifyClient.makeRequest("GET", urlStr, shopifyResponse)
5255
if len(shopifyResponse.Products) > 0 {
5356
shopifyClient.Products = append(shopifyClient.Products, shopifyResponse.Products[:]...)
5457
//lastId = shopifyResponse.Products[len(shopifyResponse.Products)-1].Id
@@ -63,19 +66,21 @@ func (shopifyClient *Shopify) LoadProducts() {
6366

6467
}
6568

66-
func (shopifyClient *Shopify) GetLiveProduct(shopifyId string) ShopifyProduct {
67-
urlStr := "admin/products/" + shopifyId + ".json"
68-
var shopifyResponse = new(ShopifyResponse)
69+
// GetLiveProduct gets product by ID
70+
func (shopifyClient *Shopify) GetLiveProduct(shopifyID string) Product {
71+
urlStr := "admin/products/" + shopifyID + ".json"
72+
var shopifyResponse = new(productResponse)
6973

70-
shopifyClient.MakeRequest("GET", urlStr, shopifyResponse)
74+
shopifyClient.makeRequest("GET", urlStr, shopifyResponse)
7175

72-
fmt.Printf("%v\n", shopifyResponse.Product.Id)
76+
fmt.Printf("%v\n", shopifyResponse.SingleProduct.ID)
7377

74-
return shopifyResponse.Product
78+
return shopifyResponse.SingleProduct
7579
}
7680

77-
func (shopifyClient *Shopify) MakeRequest(method string, urlStr string, body interface{}) {
78-
url := fmt.Sprintf("https://%s%s%s", shopifyClient.shopifyDomain, baseUrlString, urlStr)
81+
func (shopifyClient *Shopify) makeRequest(method string, urlStr string, body interface{}) {
82+
url := fmt.Sprintf("https://%s%s%s", shopifyClient.shopifyDomain, baseURLString, urlStr)
83+
log.Printf("Request URL: %s", url)
7984
client := &http.Client{}
8085
buf := new(bytes.Buffer)
8186
r, err := http.NewRequest(method, url, buf)
@@ -92,6 +97,7 @@ func (shopifyClient *Shopify) MakeRequest(method string, urlStr string, body int
9297
}
9398

9499
err = json.NewDecoder(resp.Body).Decode(body)
100+
95101
if err != nil {
96102
fmt.Print(err)
97103
fmt.Print(resp.Body)

shopify/shopifyproduct.go

Lines changed: 139 additions & 134 deletions
Original file line numberDiff line numberDiff line change
@@ -1,134 +1,139 @@
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-
}
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+
// Product models the shopify product model
73+
type Product struct {
74+
BodyHTML string `json:"body_html,omitempty"`
75+
CreatedAt string `json:"created_at,omitempty"`
76+
Handle string `json:"handle,omitempty"`
77+
ID int `json:"id,omitempty"`
78+
ProductType string `json:"product_type,omitempty"`
79+
PublishedAt string `json:"published_at,omitempty"`
80+
PublishedScope string `json:"published_scope,omitempty"`
81+
TemplateSuffix string `json:"template_suffix,omitempty"`
82+
Title string `json:"title,omitempty"`
83+
UpdatedAt string `json:"updated_at,omitempty"`
84+
Vendor string `json:"vendor,omitempty"`
85+
Tags string `json:"tags,omitempty"`
86+
Variants []ProductVariant `json:"variants,omitempty"`
87+
Options []ProductOption `json:"options,omitempty"`
88+
Images []ProductImage `json:"images,omitempty"`
89+
Image ProductImage `json:"image,omitempty"`
90+
}
91+
92+
// ProductVariant models the shopify product variant
93+
type ProductVariant struct {
94+
Barcode string `json:"barcode,omitempty"`
95+
CompareAtPrice string `json:"compare_at_price,omitempty"`
96+
CreatedAt string `json:"created_at,omitempty"`
97+
FulfillmentService string `json:"fulfillment_service,omitempty"`
98+
Grams int `json:"grams,omitempty"`
99+
ID int `json:"id,omitempty"`
100+
InventoryManagement string `json:"inventory_management,omitempty"`
101+
InventoryPolicy string `json:"inventory_policy,omitempty"`
102+
Option1 string `json:"option1,omitempty"`
103+
Option2 string `json:"option2,omitempty"`
104+
Option3 string `json:"option3,omitempty"`
105+
Position int `json:"position,omitempty"`
106+
Price string `json:"price,omitempty"`
107+
ProductID int `json:"product_id,omitempty"`
108+
RequiresShipping bool `json:"requires_shopping,omitempty"`
109+
Sku string `json:"sku,omitempty"`
110+
Taxable bool `json:"taxable,omitempty"`
111+
Title string `json:"title,omitempty"`
112+
UpdatedAt string `json:"updated_at,omitempty"`
113+
InventoryQuantity float64 `json:"inventory_quantity,omitempty"`
114+
OldInventoryQuantity int `json:"old_inventory_quantity,omitempty"`
115+
}
116+
117+
// ProductOption models the shopify product option
118+
type ProductOption struct {
119+
ID int `json:"id,omitempty"`
120+
Name string `json:"name,omitempty"`
121+
Position int `json:"position,omitempty"`
122+
ProductID int `json:"product_id,omitempty"`
123+
}
124+
125+
// ProductImage models the shopify product image object
126+
type ProductImage struct {
127+
CreatedAt string `json:"created_at,omitempty"`
128+
ID int `json:"id,omitempty"`
129+
Position int `json:"position,omitempty"`
130+
ProductID int `json:"product_id,omitempty"`
131+
UpdatedAt string `json:"updated_at,omitempty"`
132+
Src string `json:"src,omitempty"`
133+
}
134+
135+
// Response models the shopify API response
136+
type productResponse struct {
137+
Products []Product `json:"products,omitempty"`
138+
SingleProduct Product `json:"product,omitempty"`
139+
}

0 commit comments

Comments
 (0)