-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.go
67 lines (59 loc) · 1.54 KB
/
utils.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
package fluidpay
import (
"bytes"
"context"
"io/ioutil"
"net/http"
)
// TestAPIKey is the api key used in the sandbox enviroment.
const TestAPIKey = "api_0wUsHIlrkK1I6ADno5MfT10UjhR"
const urlProduction = "https://app.fluidpay.com/api"
const urlSandbox = "https://sandbox.fluidpay.com/api"
const urlLocalDev = "http://localhost:8001/api"
// Fluidpay contains the api key, client and context.
type Fluidpay struct {
APIKey string
Client *http.Client
Ctx context.Context
Sandbox bool
LocalDev bool
}
func urlBuilder(params []string) string {
path := ""
for _, param := range params {
path += "/" + param
}
return path
}
// DoRequest returns a JSON from the API
// given the correct method, endpoint, JSON body (nil is accepted) and Fluidpay.
func DoRequest(fluidpay Fluidpay, method string, params []string, body []byte) ([]byte, error) {
var res []byte
var req *http.Request
var err error
var url string
if fluidpay.LocalDev {
url = urlLocalDev + urlBuilder(params)
} else if fluidpay.Sandbox {
url = urlSandbox + urlBuilder(params)
} else {
url = urlProduction + urlBuilder(params)
}
req, err = http.NewRequest(method, url, bytes.NewBuffer(body))
if err != nil {
return res, err
}
req.WithContext(fluidpay.Ctx)
req.Header.Set("Authorization", fluidpay.APIKey)
req.Header.Set("Content-Type", "application/json")
response, err := fluidpay.Client.Do(req)
if err != nil {
return res, err
}
defer response.Body.Close()
res, err = ioutil.ReadAll(response.Body)
if err != nil {
return res, err
}
return res, err
}