Skip to content

Commit 82aaa1c

Browse files
committed
implemented CancelOrder
1 parent 06c0df0 commit 82aaa1c

File tree

1 file changed

+22
-9
lines changed

1 file changed

+22
-9
lines changed

shopify/shopify.go

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"fmt"
77
"log"
88
"net/http"
9+
"strconv"
910
"time"
1011
)
1112

@@ -45,7 +46,7 @@ func (shopifyClient *Shopify) LoadProducts() {
4546
//var lastId int = shopifyResponse.Products[249].Id
4647

4748
for done == false {
48-
log.Printf("Shopify: Loaded page %v, that's %v products!\n", page, len(shopifyClient.Products))
49+
log.Printf("[LoadProducts] - Shopify: Loaded page %v, that's %v products!\n", page, len(shopifyClient.Products))
4950
page++
5051
//get this thread to wait 0.5 seconds
5152
time.Sleep(time.Second / 2)
@@ -73,7 +74,7 @@ func (shopifyClient *Shopify) GetLiveProduct(shopifyID string) Product {
7374

7475
shopifyClient.makeRequest("GET", urlStr, shopifyResponse)
7576

76-
fmt.Printf("%v\n", shopifyResponse.SingleProduct.ID)
77+
fmt.Printf("[GetLiveProduct] - Product ID: %s\n", strconv.Itoa(shopifyResponse.SingleProduct.ID))
7778

7879
return shopifyResponse.SingleProduct
7980
}
@@ -85,27 +86,39 @@ func (shopifyClient *Shopify) GetOrder(shopifyID string) Order {
8586

8687
shopifyClient.makeRequest("GET", urlStr, shopifyResponse)
8788

88-
fmt.Printf("%v\n", shopifyResponse.SingleOrder.ID)
89+
fmt.Printf("[GetOrder] - Order id: %s\n", strconv.Itoa(shopifyResponse.SingleOrder.ID))
90+
91+
return shopifyResponse.SingleOrder
92+
}
93+
94+
// CancelOrder deletes order by ID
95+
func (shopifyClient *Shopify) CancelOrder(shopifyID string) Order {
96+
urlStr := "admin/orders/" + shopifyID + "/cancel.json"
97+
var shopifyResponse = new(orderResponse)
98+
99+
shopifyClient.makeRequest("POST", urlStr, shopifyResponse)
100+
101+
//fmt.Printf("[CancelOrder] - Order: %v\n", shopifyResponse)
89102

90103
return shopifyResponse.SingleOrder
91104
}
92105

93106
func (shopifyClient *Shopify) makeRequest(method string, urlStr string, body interface{}) {
94107
url := fmt.Sprintf("https://%s%s%s", shopifyClient.shopifyDomain, baseURLString, urlStr)
95-
log.Printf("Request URL: %s", url)
108+
log.Printf("[makeRequest] - Request URL: %s", url)
96109
client := &http.Client{}
97110
buf := new(bytes.Buffer)
98111
r, err := http.NewRequest(method, url, buf)
99112
r.Header.Add("X-Shopify-Access-Token", shopifyClient.shopifySecretToken)
100113
resp, err := client.Do(r)
101114
defer resp.Body.Close()
102115
if resp.StatusCode == http.StatusNotFound {
103-
fmt.Printf("404 on executing request: %s", url)
116+
fmt.Printf("[makeRequest] - 404 on executing request: %s", url)
104117
} else if resp.StatusCode == 429 {
105-
fmt.Printf("Rate limited!")
118+
fmt.Printf("[makeRequest] - Rate limited!")
106119
}
107120
if err != nil {
108-
fmt.Printf("Error executing request : %s", err)
121+
fmt.Printf("[makeRequest] - Error executing request : %s", err)
109122
}
110123

111124
// bodyResp, _ := ioutil.ReadAll(resp.Body)
@@ -114,7 +127,7 @@ func (shopifyClient *Shopify) makeRequest(method string, urlStr string, body int
114127
err = json.NewDecoder(resp.Body).Decode(body)
115128

116129
if err != nil {
117-
fmt.Print(err)
118-
fmt.Print(resp.Body)
130+
fmt.Printf("\n[makeRequest] - Decoding error: %#v", err)
131+
fmt.Printf("\n[makeRequest] - Response: %#v", resp.Body)
119132
}
120133
}

0 commit comments

Comments
 (0)