forked from chargebee/chargebee-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequest.go
39 lines (37 loc) · 905 Bytes
/
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
package chargebee
import (
"bytes"
"io"
"net/url"
"strings"
)
func (request RequestObj) Request() (*Result, error) {
result, err := request.RequestWithEnv(DefaultConfig())
return result, err
}
func (request RequestObj) RequestWithEnv(env Environment) (*Result, error) {
path, body := getBody(request.Method, request.Path, request.Params)
req, err := newRequest(env, request.Method, path, body, request.Header)
if err != nil {
panic(err)
}
res, err1 := Do(req)
result := &Result{}
if err1 != nil {
return result, err1
}
err1 = UnmarshalJSON([]byte(res), result)
return result, err1
}
func getBody(method string, path string, form *url.Values) (string, io.Reader) {
var body io.Reader
if form != nil && len(*form) > 0 {
data := form.Encode()
if strings.ToUpper(method) == "GET" {
path += "?" + data
} else {
body = bytes.NewBufferString(data)
}
}
return path, body
}