-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequest.go
48 lines (38 loc) · 1.13 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
package shortfundly
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"strings"
)
// CRequest defines the request given by the client to Shortfundly
type CRequest struct {
Method string
Path string
}
// sendRequest makes the request to Shortfundly's API
func (s *Shortfundly) sendRequest(r CRequest, data interface{}) error {
req, err := http.NewRequest(r.Method, fmt.Sprintf("%s/%s", s.Host, r.Path), strings.NewReader(""))
if err != nil {
return Error{ErrMessage: fmt.Sprintf("Unable to create the request: %s", err)}
}
req.Header.Add("Content-Type", "application/json")
req.Header.Add("X-API-KEY", s.APIKey)
httpClient := &http.Client{Timeout: s.Timeout}
resp, err := httpClient.Do(req)
if err != nil {
return Error{ErrMessage: fmt.Sprintf("Failed to make request: %s", err)}
}
defer resp.Body.Close()
contents, err := ioutil.ReadAll(resp.Body)
if err != nil {
return Error{ErrMessage: fmt.Sprintf("Could not read response: %s", err)}
}
if resp.StatusCode == http.StatusOK {
return json.Unmarshal(contents, &data)
}
errg := Error{}
json.Unmarshal(contents, &errg)
return Error{ErrMessage: errg.ErrMessage}
}