/
common.go
53 lines (43 loc) · 1.31 KB
/
common.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
package services
import (
"fmt"
"github.com/dghubble/sling"
)
const (
baseURL = "https://api.mollie.nl"
apiVersion = "v1"
)
// MollieError represents a Mollie API error response
type MollieError struct {
Err struct {
Type string `json:"type"`
Message string `json:"message"`
Field string `json:"field"`
} `json:"error"`
}
// ListParams are the params for any list request
// https://www.mollie.com/nl/docs/reference/payments/list#parameters
type ListParams struct {
Offset int `url:"offset,omitempty"`
Count int `url:"count,omitempty"`
}
// ListLinks is a standard list links object for a resource list query
type ListLinks struct {
Previous string `json:"previous"`
Next string `json:"next"`
First string `json:"first"`
Last string `json:"last"`
}
// NewClient returns a new Mollie client
func NewClient(accessToken string) *sling.Sling {
// Create mollie api client
client := sling.New().Client(nil).Base(fmt.Sprintf("%s/%s/", baseURL, apiVersion))
// Add request headers
client.Set("authorization", fmt.Sprintf("Bearer %s", accessToken))
client.Set("user-agent", "Mollie/1.1.8 Go/1.4 OpenSSL/1.0.2d")
return client
}
// Error is a formatted Mollie error
func (e MollieError) Error() string {
return fmt.Sprintf("Mollie %v error: %v %v", e.Err.Type, e.Err.Message, e.Err.Field)
}