-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproducts.go
201 lines (165 loc) · 5.81 KB
/
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
//********************************************************************************************************************//
//
// Copyright (C) 2018 - 2021 J&J Ideenschmiede GmbH <[email protected]>
//
// This file is part of gohood.
// All code may be used. Feel free and maybe code something better.
//
// Author: Jonas Kwiedor (aka gowizzard)
//
//********************************************************************************************************************//
package gohood
import (
"crypto/md5"
"encoding/xml"
"fmt"
"github.com/jjideenschmiede/gohood/products"
)
// ProductsRequest is to structure the data
type ProductsRequest struct {
AccountName string `xml:"accountName"`
AccountPass string `xml:"accountPass"`
Items ProductsRequestItems `xml:"items"`
}
type ProductsRequestItems struct {
Item []ProductsRequestItem `xml:"item"`
}
type ProductsRequestItem struct {
ItemId int `xml:"itemID,omitempty"`
ItemMode string `xml:"itemMode"`
CategoryId int `xml:"categoryID"`
ItemName string `xml:"itemName"`
Quantity int `xml:"quantity"`
Condition string `xml:"condition"`
Description string `xml:"description"`
Shipmethods ProductsRequestShipmethods `xml:"shipmethods"`
Price string `xml:"price"`
SalesTax string `xml:"salesTax"`
PackagingSize string `xml:"packagingSize"`
PackagingUnit string `xml:"packagingUnit"`
ProdCatId string `xml:"prodCatID"`
ProdCatId2 string `xml:"prodCatID2"`
ProdCatId3 string `xml:"prodCatID3"`
ShortDesc string `xml:"shortDesc"`
IfIsSoldOut string `xml:"ifIsSoldOut"`
ProductProperties ProductsRequestProductProductProperties `xml:"productProperties"`
ProductOptions ProductsRequestProductOptions `xml:"productOptions"`
Ean string `xml:"ean"`
Isbn string `xml:"isbn"`
Mpn string `xml:"mpn"`
Manufacturer string `xml:"manufacturer"`
Weight string `xml:"weight"`
Images ProductsRequestImages `xml:"images"`
}
type ProductsRequestShipmethods struct {
Shipmethod []ProductsRequestShipmethod `xml:"shipmethod"`
}
type ProductsRequestShipmethod struct {
Name string `xml:"name,attr"`
Value string `xml:"value"`
}
type ProductsRequestProductProductProperties struct {
NameValueList []ProductsRequestProductNameValueList `xml:"nameValueList"`
}
type ProductsRequestProductNameValueList struct {
Name string `xml:"name"`
Value string `xml:"value"`
}
type ProductsRequestProductOptions struct {
ProductOption []ProductsRequestProductOption `xml:"productOption"`
}
type ProductsRequestProductOption struct {
OptionPrice string `xml:"optionPrice"`
OptionQuantity int `xml:"optionQuantity"`
OptionItemNumber int `xml:"optionItemNumber"`
Mpn string `xml:"mpn"`
Ean string `xml:"ean"`
PackagingSize string `xml:"PackagingSize"`
OptionsDetails ProductsRequestOptionDetails `xml:"optionDetails"`
}
type ProductsRequestOptionDetails struct {
NameValueList []ProductsRequestNameValueList `xml:"nameValueList"`
}
type ProductsRequestNameValueList struct {
Name string `xml:"name"`
Value string `xml:"value"`
}
type ProductsRequestImages struct {
ImageUrl []string `xml:"imageURL"`
}
// AddProduct are to set a new product
func AddProduct(request ProductsRequest) (products.Return, error) {
// Hash the password
hash := fmt.Sprintf("%x", md5.Sum([]byte(request.AccountPass)))
// Define body data
body := products.Api{
"public",
"2.0",
request.AccountName,
hash,
"itemInsert",
request.AccountName,
hash,
request.Items,
}
// Convert body
convert, err := xml.Marshal(body)
if err != nil {
return products.Return{}, err
}
// Config new request
c := Config{convert}
// Send new request
response, err := c.Send()
if err != nil {
return products.Return{}, err
}
// Close request body
defer response.Body.Close()
// Decode data
var decode products.Return
err = xml.NewDecoder(response.Body).Decode(&decode)
if err != nil {
return products.Return{}, err
}
// Return data
return decode, nil
}
// UpdateProduct is to update a product
func UpdateProduct(request ProductsRequest) (products.Return, error) {
// Hash the password
hash := fmt.Sprintf("%x", md5.Sum([]byte(request.AccountPass)))
// Define body data
body := products.Api{
"public",
"2.0",
request.AccountName,
hash,
"itemUpdate",
request.AccountName,
hash,
request.Items,
}
// Convert body
convert, err := xml.Marshal(body)
if err != nil {
return products.Return{}, err
}
// Config new request
c := Config{convert}
// Send new request
response, err := c.Send()
if err != nil {
return products.Return{}, err
}
// Close request body
defer response.Body.Close()
// Decode data
var decode products.Return
err = xml.NewDecoder(response.Body).Decode(&decode)
if err != nil {
return products.Return{}, err
}
// Return data
return decode, nil
}