-
Notifications
You must be signed in to change notification settings - Fork 0
/
kopeechka.go
136 lines (105 loc) · 3 KB
/
kopeechka.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
package kopeechka
import (
"encoding/json"
"fmt"
"github.com/google/go-querystring/query"
"net/http"
)
const (
apiUrl = "https://api.kopeechka.store"
apiVersion = "2.0"
)
type Api struct {
// Your API key from kopeechka.store.
token string
// The response type you want to receive from the API.
// Defaults to Json.
responseType ResponseType
client http.Client
}
func New(token string) *Api {
return &Api{client: http.Client{}, token: token, responseType: Json}
}
func (api *Api) SetResponseType(responseType ResponseType) {
api.responseType = responseType
}
func (api *Api) SetToken(token string) {
api.token = token
}
func (api *Api) getBaseUrl(endpoint string, data interface{}) (string, error) {
v, err := query.Values(data)
if err != nil {
return "", err
}
v.Set("api", apiVersion)
v.Set("type", string(api.responseType))
v.Set("token", api.token)
return fmt.Sprintf("%s/%s?%s", apiUrl, endpoint, v.Encode()), nil
}
func (api *Api) GetBalance() (BalanceResponse, error) {
url, err := api.getBaseUrl("user-balance", nil)
if err != nil {
return BalanceResponse{}, err
}
res, err := api.client.Get(url)
if err != nil {
return BalanceResponse{}, err
}
defer res.Body.Close()
var balanceResponse BalanceResponse
if err = json.NewDecoder(res.Body).Decode(&balanceResponse); err != nil {
return BalanceResponse{}, err
}
return balanceResponse, nil
}
func (api *Api) OrderMail(request OrderMailRequest) (OrderMailResponse, error) {
if request.Site == "" {
return OrderMailResponse{}, MissingRequiredParameterError
}
url, err := api.getBaseUrl("mailbox-get-email", request)
res, err := api.client.Get(url)
if err != nil {
return OrderMailResponse{}, err
}
defer res.Body.Close()
var response OrderMailResponse
if err = json.NewDecoder(res.Body).Decode(&response); err != nil {
return OrderMailResponse{}, err
}
return response, nil
}
func (api *Api) GetMessage(request MessageRequest) (MessageResponse, error) {
if request.OrderId == "" {
return MessageResponse{}, MissingRequiredParameterError
}
url, err := api.getBaseUrl("mailbox-get-message", request)
res, err := api.client.Get(url)
if err != nil {
return MessageResponse{}, err
}
defer res.Body.Close()
var response MessageResponse
if err = json.NewDecoder(res.Body).Decode(&response); err != nil {
return MessageResponse{}, err
}
return response, nil
}
func (api *Api) CancelMail(request CancelMailRequest) (CancelMailResponse, error) {
if request.OrderId == "" {
return CancelMailResponse{}, MissingRequiredParameterError
}
url, err := api.getBaseUrl("mailbox-cancel", request)
res, err := api.client.Get(url)
if err != nil {
return CancelMailResponse{}, err
}
defer res.Body.Close()
var response CancelMailResponse
if err = json.NewDecoder(res.Body).Decode(&response); err != nil {
return CancelMailResponse{}, err
}
return response, nil
}
// TODO: Re-order mail (/Reorder mail)
// TODO: Find orderId from mail (/Find ID activation of mail)
// Docs: https://faq.kopeechka.store/api_page/