Skip to content

Commit 640ae8c

Browse files
committed
api_client: create generate method
1 parent 9b16f94 commit 640ae8c

File tree

4 files changed

+156
-25
lines changed

4 files changed

+156
-25
lines changed

api_client.go

Lines changed: 74 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,98 @@
11
package flareio
22

3+
import (
4+
"bytes"
5+
"encoding/json"
6+
"fmt"
7+
"net/http"
8+
"net/url"
9+
)
10+
311
type ApiClient struct {
4-
tenantId int
5-
apiKey string
12+
tenantId int
13+
apiKey string
14+
httpClient *http.Client
15+
baseUrl string
616
}
717

818
type ApiClientOption func(*ApiClient)
919

20+
// WithTenantId allows configuring the tenant id.
1021
func WithTenantId(tenantId int) ApiClientOption {
1122
return func(client *ApiClient) {
1223
client.tenantId = tenantId
1324
}
1425
}
1526

27+
// withBaseUrl allows configuring the base url, for testing only.
28+
func withBaseUrl(baseUrl string) ApiClientOption {
29+
return func(client *ApiClient) {
30+
client.baseUrl = baseUrl
31+
}
32+
}
33+
1634
func NewClient(
1735
apiKey string,
1836
optionFns ...ApiClientOption,
1937
) *ApiClient {
2038
c := &ApiClient{
21-
apiKey: apiKey,
39+
apiKey: apiKey,
40+
baseUrl: "https://api.flare.io/",
41+
httpClient: &http.Client{},
2242
}
2343
for _, optionFn := range optionFns {
2444
optionFn(c)
2545
}
2646
return c
2747
}
48+
49+
func (client *ApiClient) GenerateToken() (string, error) {
50+
// Prepare payload
51+
type GeneratePayload struct {
52+
TenantId int `json:"tenant_id,omitempty"`
53+
}
54+
payload := &GeneratePayload{
55+
TenantId: client.tenantId,
56+
}
57+
payloadBytes, err := json.Marshal(payload)
58+
if err != nil {
59+
return "", fmt.Errorf("failed to marshal generate payload: %w", err)
60+
}
61+
62+
// Create dest URL
63+
destUrl, err := url.JoinPath(client.baseUrl, "/tokens/generate")
64+
if err != nil {
65+
return "", fmt.Errorf("failed to create test URL: %w", err)
66+
}
67+
68+
// Prepare the request
69+
request, err := http.NewRequest(
70+
"POST",
71+
destUrl,
72+
bytes.NewReader(payloadBytes),
73+
)
74+
if err != nil {
75+
return "", fmt.Errorf("failed to prepare request: %w", err)
76+
}
77+
request.Header.Set("Authorization", client.apiKey)
78+
79+
// Fire the request
80+
resp, err := client.httpClient.Do(request)
81+
if err != nil {
82+
return "", fmt.Errorf("failed to generate API token: %w", err)
83+
}
84+
if resp.StatusCode != 200 {
85+
return "", fmt.Errorf("unexpected response code: %d", resp.StatusCode)
86+
}
87+
88+
// Parse response
89+
type TokenResponse struct {
90+
Token string `json:"token"`
91+
}
92+
var tokenResponse TokenResponse
93+
if err := json.NewDecoder(resp.Body).Decode(&tokenResponse); err != nil {
94+
return "", err
95+
}
96+
97+
return tokenResponse.Token, nil
98+
}

api_client_http_test.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package flareio
2+
3+
import (
4+
"net/http"
5+
"net/http/httptest"
6+
"testing"
7+
8+
"github.com/stretchr/testify/assert"
9+
)
10+
11+
type ClientTest struct {
12+
apiClient *ApiClient
13+
httpServer *httptest.Server
14+
}
15+
16+
func newClientTest(
17+
handler http.HandlerFunc,
18+
) *ClientTest {
19+
httpServer := httptest.NewServer(
20+
handler,
21+
)
22+
apiClient := NewClient(
23+
"test-api-key",
24+
withBaseUrl(httpServer.URL),
25+
)
26+
clientTest := &ClientTest{
27+
httpServer: httpServer,
28+
apiClient: apiClient,
29+
}
30+
return clientTest
31+
}
32+
33+
func (clientTest *ClientTest) Close() {
34+
defer clientTest.httpServer.Close()
35+
}
36+
37+
func TestGenerateToken(t *testing.T) {
38+
clientTest := newClientTest(
39+
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
40+
assert.Equal(t, "/tokens/generate", r.URL.Path)
41+
assert.Equal(t, []string{"test-api-key"}, r.Header["Authorization"])
42+
w.WriteHeader(http.StatusOK)
43+
w.Write([]byte(`{"token":"test-token-hello"}`))
44+
}),
45+
)
46+
defer clientTest.Close()
47+
48+
token, err := clientTest.apiClient.GenerateToken()
49+
assert.NoError(t, err, "Generating a token")
50+
assert.Equal(t, "test-token-hello", token)
51+
}

api_client_initialize_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package flareio
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
func TestCreateClientWithApiKey(t *testing.T) {
10+
c := NewClient("test-api-key")
11+
assert.Equal(t, "test-api-key", c.apiKey)
12+
assert.Equal(t, 0, c.tenantId)
13+
assert.Equal(t, "https://api.flare.io/", c.baseUrl)
14+
}
15+
16+
func TestCreateClientWithTenantId(t *testing.T) {
17+
c := NewClient(
18+
"test-api-key",
19+
WithTenantId(42),
20+
)
21+
assert.Equal(t, "test-api-key", c.apiKey)
22+
assert.Equal(t, 42, c.tenantId)
23+
}
24+
25+
func TestCreateClientWithBaseUrl(t *testing.T) {
26+
c := NewClient(
27+
"test-api-key",
28+
withBaseUrl("https://test.com/"),
29+
)
30+
assert.Equal(t, "https://test.com/", c.baseUrl)
31+
}

api_client_test.go

Lines changed: 0 additions & 22 deletions
This file was deleted.

0 commit comments

Comments
 (0)