· 5 years ago · Jan 25, 2021, 09:22 PM
1package hanshow
2
3import (
4 "bytes"
5 "encoding/json"
6 "errors"
7 "fmt"
8 "io/ioutil"
9 "log"
10 "net/http"
11
12 "../config"
13)
14
15//Integration - :)
16type Integration struct {
17 CustomerStoreCode string `json:"customerStoreCode"`
18 StoreCode string `json:"storeCode"`
19 BatchNo string `json:"batchNo"`
20 CallbackURL string `json:"callbackUrl"`
21 Items []Item `json:"items"`
22}
23
24//Item - :-)
25type Item struct {
26 Sku string `json:"sku"`
27 Ean string `json:"ean"`
28 ItemName string `json:"itemName"`
29 IISCOMMAND string `json:"IIS_COMMAND"`
30 Price1 string `json:"price1"`
31 Price2 string `json:"price2"`
32 CustomerStoreCode string `json:"customerStoreCode"`
33 StoreCode string `json:"storeCode"`
34}
35
36//GetToken - Get authentication token from prismart.
37func GetToken() (string, error) {
38 type Payload struct {
39 Username string `json:"username"`
40 Password string `json:"password"`
41 }
42
43 type HSR struct {
44 ResultCode int `json:"ResultCode"`
45 Result string `json:"result"`
46 Message string `json:"message"`
47 Data struct {
48 Jsessionid string `json:"jsessionid"`
49 Admin int `json:"admin"`
50 StoreName string `json:"storeName"`
51 StoreCode string `json:"storeCode"`
52 CustomerStoreCode string `json:"customerStoreCode"`
53 Key string `json:"key"`
54 } `json:"data"`
55 }
56
57 data := Payload{
58 config.Cfg.Username,
59 config.Cfg.Password,
60 }
61
62 payloadBytes, err := json.Marshal(data)
63 if err != nil {
64 log.Printf("AlsoAPI: error with json.")
65 }
66 body := bytes.NewReader(payloadBytes)
67
68 req, err := http.NewRequest("POST", config.Cfg.Prismart+"login", body)
69 if err != nil {
70 log.Printf("Hanshow API: error with newrequest.")
71 }
72 req.Header.Set("Accept", "application/json")
73 req.Header.Set("Content-Type", "application/json")
74
75 resp, err := http.DefaultClient.Do(req)
76 if err != nil {
77 log.Printf("HanshowAPI: Error with response.")
78 }
79 defer resp.Body.Close()
80
81 r, err := ioutil.ReadAll(resp.Body)
82 if err != nil {
83 log.Fatal(err)
84 }
85 if resp.StatusCode != 200 {
86 return "", errors.New("HanshowAPI: " + resp.Status)
87 }
88 // var ret (string) = string(r)
89
90 var hsr HSR
91
92 err = json.Unmarshal(r, &hsr)
93 if err != nil {
94 log.Fatal(err)
95 }
96 return hsr.Data.Jsessionid, nil
97}
98
99//UpdateGoods - Update goods in the storedb
100func UpdateGoods(goods Integration) (string, error) {
101
102 type HSR struct {
103 ResultCode int `json:"resultCode"`
104 Result string `json:"result"`
105 Message string `json:"message"`
106 Data string `json:"data"`
107 }
108
109 payloadBytes, err := json.Marshal(goods)
110
111 if err != nil {
112 log.Printf("AlsoAPI: error with json.")
113 }
114
115 body := bytes.NewReader(payloadBytes)
116
117 req, err := http.NewRequest("POST", config.Cfg.Prismart+"integration", body)
118
119 if err != nil {
120 log.Printf("Hanshow API: error with newrequest.")
121 }
122
123 req.Header.Set("Accept", "application/json")
124 req.Header.Set("Content-Type", "application/json")
125
126 resp, err := http.DefaultClient.Do(req)
127
128 if err != nil {
129 log.Printf("HanshowAPI: Error with response.")
130 }
131 defer resp.Body.Close()
132
133 r, err := ioutil.ReadAll(resp.Body)
134
135 if err != nil {
136 log.Fatal(err)
137 }
138
139 if resp.StatusCode != 200 {
140 return "", errors.New("HanshowAPI: " + resp.Status)
141 }
142
143 var hsr HSR
144
145 err = json.Unmarshal(r, &hsr)
146
147 if err != nil {
148 return "", err
149 }
150
151 fmt.Printf("%+v", hsr)
152 return hsr.Message, nil
153}
154