-
Notifications
You must be signed in to change notification settings - Fork 0
/
request.go
120 lines (101 loc) · 3.03 KB
/
request.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
package ippanel
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"path"
"runtime"
)
var (
// ErrUnexpectedResponse is used when there was an internal server error and nothing can be done at this point.
ErrUnexpectedResponse = errors.New("The Ippanel API is currently unavailable")
)
// ListParams ...
type ListParams struct {
Limit int64 `json:"limit"`
Page int64 `json:"page"`
}
// PaginationInfo ...
type PaginationInfo struct {
Total int64 `json:"total"`
Limit int64 `json:"limit"`
Page int64 `json:"page"`
Pages int64 `json:"pages"`
Prev *string `json:"prev"`
Next *string `json:"next"`
}
// BaseResponse base response model
type BaseResponse struct {
Status string `json:"status"`
Code ResponseCode `json:"code"`
Data json.RawMessage `json:"data"`
Meta *PaginationInfo `json:"meta"`
}
// request preform http request
func (sms Ippanel) request(method string, uri string, params map[string]string, data interface{}) (*BaseResponse, error) {
u := *sms.BaseURL
// join base url with extra path
u.Path = path.Join(sms.BaseURL.Path, uri)
// set query params
p := url.Values{}
for key, param := range params {
p.Add(key, param)
}
u.RawQuery = p.Encode()
marshaledBody, err := json.Marshal(data)
if err != nil {
return nil, err
}
requestBody := bytes.NewBuffer(marshaledBody)
req, err := http.NewRequest(method, u.String(), requestBody)
if err != nil {
return nil, err
}
//req.Header.Set("Accept", "application/json")
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", "AccessKey "+sms.AccessKey)
req.Header.Set("User-Agent", "Ippanel/ApiClient/"+ClientVersion+" Go/"+runtime.Version())
res, err := sms.Client.Do(req)
if err != nil || res == nil {
return nil, err
}
defer res.Body.Close()
responseBody, err := ioutil.ReadAll(res.Body)
if err != nil {
return nil, err
}
switch res.StatusCode {
case http.StatusOK, http.StatusCreated:
_res := &BaseResponse{}
if err := json.Unmarshal(responseBody, _res); err != nil {
return nil, fmt.Errorf("could not decode response JSON, %s: %v", string(responseBody), err)
}
return _res, nil
case http.StatusNoContent:
// Status code 204 is returned for successful DELETE requests. Don't try to
// unmarshal the body: that would return errors.
return nil, nil
case http.StatusInternalServerError:
// Status code 500 is a server error and means nothing can be done at this
// point.
return nil, ErrUnexpectedResponse
default:
_res := &BaseResponse{}
if err := json.Unmarshal(responseBody, _res); err != nil {
return nil, fmt.Errorf("could not decode response JSON, %s: %v", string(responseBody), err)
}
return _res, ParseErrors(_res)
}
}
// get do get request
func (sms Ippanel) get(uri string, params map[string]string) (*BaseResponse, error) {
return sms.request("GET", uri, params, nil)
}
// post do post request
func (sms Ippanel) post(uri string, contentType string, data interface{}) (*BaseResponse, error) {
return sms.request("POST", uri, nil, data)
}