-
Notifications
You must be signed in to change notification settings - Fork 11
/
clerk.go
185 lines (152 loc) · 4.31 KB
/
clerk.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package clerk
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"strings"
)
const (
ProdUrl = "https://api.clerk.dev/v1/"
ClientsUrl = "clients"
ClientsVerifyUrl = ClientsUrl + "/verify"
EmailsUrl = "emails"
SessionsUrl = "sessions"
SMSUrl = "sms_messages"
UsersUrl = "users"
)
type Client interface {
NewRequest(method string, url string, body ...interface{}) (*http.Request, error)
Do(req *http.Request, v interface{}) (*http.Response, error)
Clients() *ClientsService
Emails() *EmailService
Sessions() *SessionsService
SMS() *SMSService
Users() *UsersService
Verification() *VerificationService
}
type service struct {
client Client
}
type client struct {
client *http.Client
baseURL *url.URL
token string
clients *ClientsService
emails *EmailService
sessions *SessionsService
sms *SMSService
users *UsersService
verification *VerificationService
}
// NewClient creates a new Clerk client.
// Because the token supplied will be used for all authenticated requests,
// the created client should not be used across different users
func NewClient(token string) (Client, error) {
return NewClientWithBaseUrl(token, ProdUrl)
}
func NewClientWithBaseUrl(token string, baseUrl string) (Client, error) {
httpClient := http.Client{}
return NewClientWithCustomHTTP(token, baseUrl, &httpClient)
}
func NewClientWithCustomHTTP(token string, urlStr string, httpClient *http.Client) (Client, error) {
baseURL := toURLWithEndingSlash(urlStr)
client := &client{client: httpClient, baseURL: baseURL, token: token}
commonService := &service{client: client}
client.clients = (*ClientsService)(commonService)
client.emails = (*EmailService)(commonService)
client.sessions = (*SessionsService)(commonService)
client.sms = (*SMSService)(commonService)
client.users = (*UsersService)(commonService)
client.verification = (*VerificationService)(commonService)
return client, nil
}
func toURLWithEndingSlash(u string) *url.URL {
baseURL, _ := url.Parse(u)
if !strings.HasSuffix(baseURL.Path, "/") {
baseURL.Path += "/"
}
return baseURL
}
// NewRequestWithBody creates an API request.
// A relative URL `url` can be specified which is resolved relative to the baseURL of the client.
// Relative URLs should be specified without a preceding slash.
// The `body` parameter can be used to pass a body to the request. If no body is required, the parameter can be omitted.
func (c *client) NewRequest(method string, url string, body ...interface{}) (*http.Request, error) {
fullUrl, err := c.baseURL.Parse(url)
if err != nil {
return nil, err
}
var buf io.ReadWriter
if len(body) > 0 && body[0] != nil {
buf = &bytes.Buffer{}
enc := json.NewEncoder(buf)
enc.SetEscapeHTML(false)
err := enc.Encode(body[0])
if err != nil {
return nil, err
}
}
req, err := http.NewRequest(method, fullUrl.String(), buf)
if err != nil {
return nil, err
}
return req, nil
}
// Do will send the given request using the client `c` on which it is called.
// If the response contains a body, it will be unmarshalled in `v`.
func (c *client) Do(req *http.Request, v interface{}) (*http.Response, error) {
req.Header.Set("Authorization", "Bearer "+c.token)
resp, err := c.client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
err = checkForErrors(resp)
if err != nil {
return resp, err
}
if resp.Body != nil && v != nil {
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return resp, err
}
err = json.Unmarshal(body, &v)
if err != nil {
return resp, err
}
}
return resp, nil
}
func checkForErrors(resp *http.Response) error {
if c := resp.StatusCode; c >= 200 && c < 400 {
return nil
}
data, _ := ioutil.ReadAll(resp.Body)
if data != nil && len(data) > 0 {
return errors.New(string(data))
}
return errors.New(fmt.Sprintf("Server returned unexpected error with status code %d", resp.StatusCode))
}
func (c *client) Clients() *ClientsService {
return c.clients
}
func (c *client) Emails() *EmailService {
return c.emails
}
func (c *client) Sessions() *SessionsService {
return c.sessions
}
func (c *client) SMS() *SMSService {
return c.sms
}
func (c *client) Users() *UsersService {
return c.users
}
func (c *client) Verification() *VerificationService {
return c.verification
}