-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathclient.go
executable file
·135 lines (120 loc) · 3.23 KB
/
client.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// Code generated by `gogenitor`. DO NOT EDIT.
package sumup
import (
"context"
_ "embed"
"fmt"
"io"
"net/http"
"strings"
)
//go:embed .version
var version string
const (
// APIUrl is the URL of our API. Currently, SumUp doesn't provide any
// other environment or APIs thus APIUrl is used as the default URL
// for the client.
APIUrl = "https://api.sumup.com"
)
type service struct {
client *Client
}
type Client struct {
// service is the shared service struct re-used for all services.
svc service
// client is the HTTP client used to communicate with the API.
client *http.Client
// url is the url of the API the requests will be sent to.
url string
// userAgent is the user-agent header that will be sent with
// every request.
userAgent string
// key is the API key or access token used for authorization.
key string
ApiKeys *ApiKeysService
Authorization *AuthorizationService
Checkouts *CheckoutsService
Customers *CustomersService
Members *MembersService
Merchant *MerchantService
Payouts *PayoutsService
Readers *ReadersService
Receipts *ReceiptsService
Shared *SharedService
Subaccounts *SubaccountsService
Transactions *TransactionsService
}
// NewClient creates new SumUp API client.
// To use APIs that require authentication use [Client.WithAuth].
func NewClient() *Client {
c := &Client{
client: http.DefaultClient,
userAgent: fmt.Sprintf("sumup-go/%s", version),
url: APIUrl,
}
c.populate()
return c
}
// WithAuth returns a copy of the client configured with the provided Authorization key.
func (c *Client) WithAuth(key string) *Client {
clone := Client{
client: c.client,
url: APIUrl,
userAgent: c.userAgent,
key: key,
}
clone.populate()
return &clone
}
// WithClient returns a copy of the client configured with the provided http client.
func (c *Client) WithHTTPClient(client *http.Client) *Client {
clone := Client{
client: client,
url: APIUrl,
userAgent: c.userAgent,
key: c.key,
}
clone.populate()
return &clone
}
func (c *Client) populate() {
c.svc.client = c
c.ApiKeys = (*ApiKeysService)(&c.svc)
c.Authorization = (*AuthorizationService)(&c.svc)
c.Checkouts = (*CheckoutsService)(&c.svc)
c.Customers = (*CustomersService)(&c.svc)
c.Members = (*MembersService)(&c.svc)
c.Merchant = (*MerchantService)(&c.svc)
c.Payouts = (*PayoutsService)(&c.svc)
c.Readers = (*ReadersService)(&c.svc)
c.Receipts = (*ReceiptsService)(&c.svc)
c.Shared = (*SharedService)(&c.svc)
c.Subaccounts = (*SubaccountsService)(&c.svc)
c.Transactions = (*TransactionsService)(&c.svc)
}
func (c *Client) NewRequest(
ctx context.Context,
method, path string,
body io.Reader,
) (*http.Request, error) {
if !strings.HasPrefix(path, "/") {
path = "/" + path
}
req, err := http.NewRequestWithContext(
ctx,
method,
c.url+path,
body,
)
if err != nil {
return nil, fmt.Errorf("build request: %s", err.Error())
}
req.Header.Add("Authorization", "Bearer "+c.key)
req.Header.Add("Content-Type", "application/json")
req.Header.Add("SumUp-Version", version)
req.Header.Add("User-Agent", c.userAgent)
return req, nil
}
func (c *Client) Do(req *http.Request) (*http.Response, error) {
return c.client.Do(req)
}