This repository has been archived by the owner on Sep 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
core.go
87 lines (71 loc) · 1.8 KB
/
core.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
// Copyright 2017 The Go-Mollie Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package mollie
import (
"encoding/json"
"errors"
"io"
"io/ioutil"
"net/http"
"strings"
)
const endpoint string = "https://api.mollie.nl"
const apiVersion string = "v1"
type core struct {
apiKey string
}
type errorReply struct {
Error struct {
Message string
}
}
func getURI(action string) string {
return endpoint + "/" + apiVersion + "/" + action
}
func (c core) Get(action string, d interface{}) error {
return c.request(http.MethodGet, action, d, nil)
}
func (c core) Delete(action string, d interface{}) error {
return c.request(http.MethodDelete, action, d, nil)
}
func (c core) Post(action string, d interface{}, postData interface{}) error {
postStr, err := json.Marshal(postData)
if err != nil {
return err
}
reader := strings.NewReader(string(postStr))
return c.request(http.MethodPost, action, d, reader)
}
func (c core) request(method, action string, d interface{}, reader io.Reader) error {
req, err := http.NewRequest(method, getURI(action), reader)
if err != nil {
return err
}
req.Header.Set("Accept", "application/json")
req.Header.Set("User-Agent", "go-mollie-api/v1")
req.Header.Set("Authorization", "Bearer "+c.apiKey)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return err
}
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
if err := resp.Body.Close(); err != nil {
return err
}
if resp.StatusCode >= 400 {
if len(data) > 0 {
r := &errorReply{}
err := json.Unmarshal(data, r)
if err == nil && len(r.Error.Message) > 0 {
return errors.New(r.Error.Message)
}
}
return errors.New(resp.Status)
}
return json.Unmarshal(data, &d)
}