-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathorder.go
125 lines (98 loc) · 3.31 KB
/
order.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
package biteship
import (
"bytes"
"encoding/json"
"fmt"
"github.com/go-playground/validator/v10"
"io"
"log"
"net/http"
"reflect"
)
func (bite *BiteshipImpl) CreateOrder(request *CreateOrderRequestParam) (*ResponseCreateOrder, *Error) {
resp := &ResponseCreateOrder{}
var url = fmt.Sprintf("%s/v1/orders", bite.Config.BiteshipUrl)
var errMarshal error
jsonRequest := []byte("")
validate = validator.New()
errValidate := validate.Struct(request)
if errValidate != nil {
return resp, ErrorRequestParam(errValidate)
}
isParamsNil := reflect.ValueOf(request).Kind() == reflect.Ptr && reflect.ValueOf(request).IsNil()
if !isParamsNil {
jsonRequest, errMarshal = json.Marshal(request)
if errMarshal != nil {
log.Println(errMarshal)
return resp, ErrorGo(errMarshal)
}
}
errRequest := bite.HttpRequest.Call(http.MethodPost, url, bite.Config.SecretKey, bytes.NewBuffer(jsonRequest), resp)
if errRequest != nil {
return resp, errRequest
}
return resp, nil
}
func (bite *BiteshipImpl) RetrieveOrder(orderId string) (*ResponseRetrieveOrder, *Error) {
resp := &ResponseRetrieveOrder{}
var url = fmt.Sprintf("%s/v1/orders/%s", bite.Config.BiteshipUrl, orderId)
errRequest := bite.HttpRequest.Call(http.MethodGet, url, bite.Config.SecretKey, nil, resp)
if errRequest != nil {
return resp, errRequest
}
return resp, nil
}
func (bite *BiteshipImpl) UpdateOrder(orderId string, request interface{}) (*ResponseCreateOrder, *Error) {
resp := &ResponseCreateOrder{}
var url = fmt.Sprintf("%s/v1/orders/%s", bite.Config.BiteshipUrl, orderId)
var errMarshal error
jsonRequest := []byte("")
isParamsNil := reflect.ValueOf(request).Kind() == reflect.Ptr && reflect.ValueOf(request).IsNil()
if !isParamsNil {
jsonRequest, errMarshal = json.Marshal(request)
if errMarshal != nil {
log.Println(errMarshal)
return resp, ErrorGo(errMarshal)
}
}
errRequest := bite.HttpRequest.Call(http.MethodPost, url, bite.Config.SecretKey, bytes.NewBuffer(jsonRequest), resp)
if errRequest != nil {
return resp, errRequest
}
log.Println(bytes.NewBuffer(jsonRequest))
return resp, nil
}
func (bite *BiteshipImpl) ConfirmOrder(orderId string) (*ResponseCreateOrder, *Error) {
resp := &ResponseCreateOrder{}
var url = fmt.Sprintf("%s/v1/orders/%s/confirm", bite.Config.BiteshipUrl, orderId)
errRequest := bite.HttpRequest.Call(http.MethodPost, url, bite.Config.SecretKey, nil, resp)
if errRequest != nil {
return resp, errRequest
}
return resp, nil
}
func (bite *BiteshipImpl) CancelOrder(orderId string, reason string) (*ResponseCancelOrder, *Error) {
var body io.Reader
resp := &ResponseCancelOrder{}
var url = fmt.Sprintf("%s/v1/orders/%s", bite.Config.BiteshipUrl, orderId)
var errMarshal error
jsonRequest := []byte("")
isParamsNil := reason == "" || (reflect.ValueOf(reason).Kind() == reflect.Ptr && reflect.ValueOf(reason).IsNil())
if !isParamsNil {
cancelReason := ReasonRequest{reason}
jsonRequest, errMarshal = json.Marshal(cancelReason)
if errMarshal != nil {
log.Println(errMarshal)
return resp, ErrorGo(errMarshal)
}
body = bytes.NewBuffer(jsonRequest)
} else {
body = nil
}
errRequest := bite.HttpRequest.Call(http.MethodDelete, url, bite.Config.SecretKey, body, resp)
if errRequest != nil {
return resp, errRequest
}
//log.Println(bytes.NewBuffer(jsonRequest))
return resp, nil
}