-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
executable file
·78 lines (72 loc) · 1.43 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
package turso
import (
"bytes"
"fmt"
"io"
"net/http"
"net/url"
)
type Client struct {
client client
Tokens
Organizations
Locations
AuditLogs
}
type client struct {
baseURL string
apiToken string
api *http.Client
}
const tursoBaseURL = "https://api.turso.tech"
const tursoBaseURLRegion = "https://region.turso.io"
func NewClient(baseURL, apiToken string) (*Client, error) {
if baseURL == "" {
baseURL = tursoBaseURL
}
if apiToken == "" {
return nil, fmt.Errorf("apiToken is required")
}
connection := &client{
baseURL: baseURL,
apiToken: apiToken,
api: &http.Client{},
}
client := &Client{
client: *connection,
}
client.Tokens = Tokens{
client: connection,
}
client.Organizations = Organizations{
client: connection,
}
client.Locations = Locations{
client: connection,
}
client.AuditLogs = AuditLogs{
client: connection,
}
return client, nil
}
func (client *client) tursoAPIrequest(endpoint string, method string, body interface{}) (*http.Response, error) {
endpointURL, err := url.Parse(endpoint)
if err != nil {
return nil, err
}
headers := http.Header{}
headers.Set("Authorization", fmt.Sprintf("Bearer %s", client.apiToken))
req := &http.Request{
Method: method,
URL: endpointURL,
Header: headers,
}
if body != nil {
req.Body = io.NopCloser(bytes.NewBuffer([]byte(body.(string))))
}
resp, err := client.api.Do(req)
if err != nil {
return nil, err
}
return resp, nil
}