forked from saasbuilders/itembase
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
185 lines (150 loc) · 4.35 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
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 itembase gives a thin wrapper around the itembase REST API.
package itembase
import (
"encoding/json"
)
// Error is a Go representation of the error message sent back by itembase when a
// request results in an error.
type Error struct {
Message string `json:"message"`
Code int `json:"code"`
}
func (f *Error) Error() string {
return f.Message
}
// This is the default implementation
type client struct {
// root is the client's base URL used for all calls.
root string
me string
activation string
// url is the current url to call
url string
// auth is authentication token used when making calls.
// The token is optional and can also be overwritten on an individual
// call basis via params.
auth string
// user is the current shop we're calling for
user string
// production environment vs sandbox
production bool
// api is the underlying client used to make calls.
api API
params map[string]string
options Config
}
// New creates a new instance of the default itembase Client implementation.
//
// The options must be non-nil and must provide all OAuth2 credentials and
// configuration for an application registered with the itembase API.
//
// TODO: always use the default API impl, NewClient allows dependency injection
// needed for testing.
func New(options Config, api API) Client {
if api == nil {
api = new(itembaseAPI)
}
return &client{options: options, production: options.Production, api: api}
}
// NewClient is an alternative Client constructor intended for testing or
// advanced usage, where a custom API implementation can be injected.
func NewClient(root, auth string, api API) Client {
if api == nil {
api = new(itembaseAPI)
}
return &client{url: root, root: root, auth: auth, api: api}
}
func (c *client) URL() string {
return c.url
}
func (c *client) Sandbox() Client {
c.production = false
return c
}
func (c *client) User(user string) Client {
c.auth = c.getUserToken(user).AccessToken
c.user = user
c.url = c.root + "/users/" + user
return c
}
func (c *client) GetInto(destination interface{}) error {
err := c.api.Call("GET", c.url, c.auth, nil, c.params, &destination)
if err != nil {
return err
}
return nil
}
func (c *client) Get() (destination interface{}, err error) {
err = c.api.Call("GET", c.url, c.auth, nil, c.params, &destination)
return
}
func (c *client) Me() (destination User, err error) {
err = c.api.Call("GET", c.me, c.auth, nil, c.params, &destination)
return
}
func (c *client) Activate() (destination interface{}, err error) {
err = c.api.Call("GET", c.activation+"/activate", c.auth, nil, c.params, &destination)
return
}
func (c *client) Child(path string) Client {
c.url = c.url + "/" + path
return c
}
func (c *client) Transactions() Client {
c.url = c.root + "/users/" + c.user + "/transactions"
return c
}
func (c *client) Products() Client {
c.url = c.root + "/users/" + c.user + "/products"
return c
}
func (c *client) Buyers() Client {
c.url = c.root + "/users/" + c.user + "/buyers"
return c
}
func (c *client) Profiles() Client {
c.url = c.root + "/users/" + c.user + "/profiles"
return c
}
// These are some shenanigans, golang. Shenanigans I say.
func (c *client) newParamMap(key string, value interface{}) map[string]string {
ret := make(map[string]string, len(c.params)+1)
for key, value := range c.params {
ret[key] = value
}
switch value.(type) {
case string:
ret[key] = value.(string)
default:
jsonVal, _ := json.Marshal(value)
ret[key] = string(jsonVal)
}
return ret
}
func (c *client) clientWithNewParam(key string, value interface{}) *client {
c.params = c.newParamMap(key, value)
return c
}
// Query functions.
func (c *client) Select(prop string) Client {
c.url = c.url + "/" + prop
return c
}
func (c *client) CreatedAtFrom(value string) Client {
return c.clientWithNewParam("created_at_from", value)
}
func (c *client) CreatedAtTo(value string) Client {
return c.clientWithNewParam("created_at_to", value)
}
func (c *client) UpdatedAtFrom(value string) Client {
return c.clientWithNewParam("updated_at_from", value)
}
func (c *client) UpdatedAtTo(value string) Client {
return c.clientWithNewParam("updated_at_to", value)
}
func (c *client) Limit(limit uint) Client {
return c.clientWithNewParam("document_limit", limit)
}
func (c *client) Offset(offset uint) Client {
return c.clientWithNewParam("start_at_document", offset)
}