-
Notifications
You must be signed in to change notification settings - Fork 0
/
habitify.go
123 lines (98 loc) · 2.78 KB
/
habitify.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
package habitify
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"time"
)
const (
// DefaultEndpoint is the default endpoint of Habitify API.
DefaultEndpoint = "https://api.habitify.me"
dateFormat = "2006-01-02T15:04:05-07:00"
urlHabits = "/habits"
urlJournal = "/journal"
urlNotes = "/notes"
urlStatus = "/status"
)
type apiResponse struct {
Message string `json:"message"`
Version string `json:"version"`
Status bool `json:"status"`
Data json.RawMessage `json:"data"`
}
// Client is a client for Habitify API.
type Client struct {
httpClient *http.Client
apiKey string
endpoint string
}
// Option is an option for Client.
type Option func(*Client)
// New returns a new client associated with given api key.
func New(apiKey string, opts ...Option) *Client {
c := &Client{
httpClient: http.DefaultClient,
endpoint: DefaultEndpoint,
apiKey: apiKey,
}
for _, opt := range opts {
opt(c)
}
return c
}
// WithHTTPClient allows you to pass your http client.
func WithHTTPClient(httpClient *http.Client) Option {
return func(c *Client) {
c.httpClient = httpClient
}
}
// WithEndpoint allows you to set an endpoint.
func WithEndpoint(endpoint string) Option {
return func(c *Client) {
c.endpoint = endpoint
}
}
type httpResponse struct {
*http.Response
}
func (c *Client) do(ctx context.Context, method, path string, body io.Reader, data interface{}) error {
req, err := http.NewRequestWithContext(ctx, method, c.endpoint+path, body)
if err != nil {
return &ErrInternal{msg: fmt.Sprintf("failed to create a request: %s", err.Error())}
}
req.Header.Add("Authorization", c.apiKey)
req.Header.Add("Content-Type", "application/json; charset=utf8")
resp, err := c.httpClient.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
var apiResp apiResponse
if err := json.NewDecoder(resp.Body).Decode(&apiResp); err != nil {
return &ErrInternal{msg: fmt.Sprintf("failed to decode response body: %s", err.Error())}
}
if !apiResp.Status {
return newError(resp.StatusCode, apiResp.Message)
}
if err := json.Unmarshal(apiResp.Data, data); err != nil {
return &ErrInternal{msg: fmt.Sprintf("failed to decode response data: %s", err.Error())}
}
return nil
}
func (c *Client) get(ctx context.Context, path string, data interface{}) error {
return c.do(ctx, http.MethodGet, path, nil, data)
}
func (c *Client) post(ctx context.Context, path string, body interface{}) error {
var buf bytes.Buffer
if err := json.NewEncoder(&buf).Encode(body); err != nil {
return &ErrInternal{msg: fmt.Sprintf("failed to encode request body: %s", err.Error())}
}
return c.do(ctx, http.MethodPost, path, &buf, nil)
}
func formatTime(t time.Time) string {
return url.QueryEscape(t.Format(dateFormat))
}