-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshipstation_products.go
144 lines (124 loc) · 4.9 KB
/
shipstation_products.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package shipstation
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
)
// ProductsResponse represents the response structure for retrieving all products.
type ProductsResponse struct {
Products []Product `json:"products"`
Total int `json:"total"`
Page int `json:"page"`
Pages int `json:"pages"`
}
// Product represents a single product.
type Product struct {
Aliases interface{} `json:"aliases"`
ProductID int `json:"productId"`
SKU string `json:"sku"`
Name string `json:"name"`
Price float64 `json:"price"`
DefaultCost float64 `json:"defaultCost"`
Length int `json:"length"`
Width int `json:"width"`
Height int `json:"height"`
WeightOz int `json:"weightOz"`
InternalNotes interface{} `json:"internalNotes"`
FulfillmentSKU string `json:"fulfillmentSku"`
CreateDate string `json:"createDate"`
ModifyDate string `json:"modifyDate"`
Active bool `json:"active"`
ProductCategory ProductCategory `json:"productCategory"`
ProductType interface{} `json:"productType"`
WarehouseLocation string `json:"warehouseLocation"`
DefaultCarrierCode string `json:"defaultCarrierCode"`
DefaultServiceCode string `json:"defaultServiceCode"`
DefaultPackageCode string `json:"defaultPackageCode"`
DefaultIntlCarrierCode string `json:"defaultIntlCarrierCode"`
DefaultIntlServiceCode string `json:"defaultIntlServiceCode"`
DefaultIntlPackageCode string `json:"defaultIntlPackageCode"`
DefaultConfirmation string `json:"defaultConfirmation"`
DefaultIntlConfirmation string `json:"defaultIntlConfirmation"`
CustomsDescription interface{} `json:"customsDescription"`
CustomsValue interface{} `json:"customsValue"`
CustomsTariffNo interface{} `json:"customsTariffNo"`
CustomsCountryCode interface{} `json:"customsCountryCode"`
NoCustoms interface{} `json:"noCustoms"`
Tags []Tag `json:"tags"`
}
// ProductCategory represents the category of a product.
type ProductCategory struct {
CategoryID int `json:"categoryId"`
Name string `json:"name"`
}
// Tag represents a product tag.
type Tag struct {
TagID int `json:"tagId"`
Name string `json:"name"`
}
// UpdateProductResponse represents the response structure for updating a product.
type UpdateProductResponse struct {
Success bool `json:"success"`
Message string `json:"message"`
}
func (s *ShipStation) GetProducts() (*ProductsResponse, error) {
url := fmt.Sprintf("%s/products", s.baseURL)
resp, err := s.sendRequest("GET", url, "retrieve products", nil)
if err != nil {
return nil, err
}
defer resp.Body.Close()
// Check if the response status code is OK
if resp.StatusCode != http.StatusOK {
body, _ := ioutil.ReadAll(resp.Body)
return nil, fmt.Errorf("failed to retrieve products. Status code: %d and error message: %s", resp.StatusCode, string(body))
}
// Parse the response
var productsResponse ProductsResponse
err = json.NewDecoder(resp.Body).Decode(&productsResponse)
if err != nil {
return nil, err
}
return &productsResponse, nil
}
func (s *ShipStation) GetProduct(productID int) (*Product, error) {
url := fmt.Sprintf("%s/products/%d", s.baseURL, productID)
resp, err := s.sendRequest("GET", url, "retrieve product", nil)
if err != nil {
return nil, err
}
defer resp.Body.Close()
// Check if the response status code is OK
if resp.StatusCode != http.StatusOK {
body, _ := ioutil.ReadAll(resp.Body)
return nil, fmt.Errorf("failed to retrieve product. Status code: %d and error message: %s", resp.StatusCode, string(body))
}
// Parse the response
var product Product
err = json.NewDecoder(resp.Body).Decode(&product)
if err != nil {
return nil, err
}
return &product, nil
}
func (s *ShipStation) UpdateProduct(productID int, updateData []byte) (*UpdateProductResponse, error) {
url := fmt.Sprintf("%s/products/%d", s.baseURL, productID)
resp, err := s.sendRequest("PUT", url, "update product", updateData)
if err != nil {
return nil, err
}
defer resp.Body.Close()
// Check if the response status code is OK
if resp.StatusCode != http.StatusOK {
body, _ := ioutil.ReadAll(resp.Body)
return nil, fmt.Errorf("failed to update product. Status code: %d and error message: %s", resp.StatusCode, string(body))
}
// Parse the response
var updateProductResponse UpdateProductResponse
err = json.NewDecoder(resp.Body).Decode(&updateProductResponse)
if err != nil {
return nil, err
}
return &updateProductResponse, nil
}