/
api.clj
377 lines (287 loc) · 9.3 KB
/
api.clj
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
(ns dynamodb.api
(:import
#_:clj-kondo/ignore java.util.List
#_:clj-kondo/ignore java.util.Map
java.net.URI)
(:require
[dynamodb.sql :as sql]
[dynamodb.client :as client]
[dynamodb.constant :as const]
[dynamodb.mask :as mask]
[dynamodb.params :refer [pre-process]]
[dynamodb.response :refer [post-process]]))
(defn sql [string]
(sql/sql string))
(defn make-client
([^String access-key
^String secret-key
^String endpoint
^String region]
(make-client access-key secret-key endpoint region nil))
([^String access-key
^String secret-key
^String endpoint
^String region
{:keys [throw?
version
http-opt]
:or {throw? true
version const/version-20120810}}]
(let [uri
(new URI endpoint)
host
(.getHost uri)
path
(.getPath uri)
http-opt
(-> client/defaults
(merge http-opt)
(assoc :method :post
:url endpoint
:as :stream))]
(client/map->Client
{:access-key (mask/mask access-key)
:secret-key (mask/mask secret-key)
:endpoint endpoint
:content-type "application/x-amz-json-1.0"
:host host
:path path
:service "dynamodb"
:version version
:region region
:throw? throw?
:http-opt http-opt}))))
;; https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_CreateTable.html
;; https://cloud.yandex.com/en-ru/docs/ydb/docapi/api-ref/actions/createTable
(defn create-table
{:arglists
'([client]
[client
{:keys [^Map tags
^String table-class
^String billing-mode
^List provisioned-throughput
^Map global-indexes
^Map local-indexes
^Map stream-spec
^Map sse-spec]}])}
([client table attr-defs key-schema]
(create-table client table attr-defs key-schema nil))
([client table attr-defs key-schema options]
(-> options
(assoc :table table
:attr-defs attr-defs
:key-schema key-schema)
(pre-process)
(->> (client/make-request client "CreateTable"))
(post-process))))
;; https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_CreateBackup.html
(defn create-backup
([client table backup]
(create-backup client table backup nil))
([client table backup options]
(-> options
(assoc :table table :backup backup)
(pre-process)
(->> (client/make-request client "CreateBackup"))
(post-process))))
;; https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_DescribeBackup.html
(defn describe-backup
([client backup-arn]
(describe-backup client backup-arn nil))
([client backup-arn options]
(-> options
(assoc :backup-arn backup-arn)
(pre-process)
(->> (client/make-request client "DescribeBackup"))
(post-process))))
;; https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_DeleteTable.html
(defn delete-table
([client table]
(delete-table client table nil))
([client table options]
(-> options
(assoc :table table)
(pre-process)
(->> (client/make-request client "DeleteTable"))
(post-process))))
;; https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_ListTables.html
(defn list-tables
{:arglists
'([client]
[client
{:keys [^Long limit
^String start-table]}])}
([client]
(list-tables client nil))
([client options]
(-> options
(pre-process)
(->> (client/make-request client "ListTables"))
(post-process))))
;; https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_DescribeTable.html
(defn describe-table
([client table]
(describe-table client table nil))
([client table options]
(-> options
(assoc :table table)
(pre-process)
(->> (client/make-request client "DescribeTable"))
(post-process))))
;; https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_PutItem.html
(defn put-item
{:arglists
'([client table item]
[client table item
{:keys [^String sql-condition
^Map attr-names
^Map attr-values
^String return-consumed-capacity
^String return-item-collection-metrics
^String return-values]}])}
([client table item]
(put-item client table item nil))
([client table item options]
(-> options
(assoc :table table :item item)
(pre-process)
(->> (client/make-request client "PutItem"))
(post-process))))
;; https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_GetItem.html
(defn get-item
{:arglists
'([client table key]
[client table key {:keys [^List attrs-get
^Map attr-names
^Boolean consistent-read?
^String return-consumed-capacity]}])}
([client table key]
(get-item client table key nil))
([client table key options]
(-> options
(assoc :table table :key key)
(pre-process)
(->> (client/make-request client "GetItem"))
(post-process))))
;; https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_BatchGetItem.html
(defn batch-get-item
{:arglists
'([client table->options]
[client table->options
{:keys [^String return-consumed-capacity]}])}
([client table->options]
(batch-get-item client table->options nil))
([client table->options options]
(-> options
(assoc :request-items table->options)
(pre-process)
(->> (client/make-request client "BatchGetItem"))
(post-process))))
;; https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_DeleteItem.html
(defn delete-item
{:arglists
'([client table key]
[client table key
{:keys [^String sql-condition
^Map attr-names
^Map attr-values
^String return-consumed-capacity
^String return-item-collection-metrics
^String return-values]}])}
([client table key]
(delete-item client table key nil))
([client table key options]
(-> options
(assoc :table table :key key)
(pre-process)
(->> (client/make-request client "DeleteItem"))
(post-process))))
;; https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_UpdateItem.html
;; https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.UpdateExpressions.html
(defn update-item
{:arglists
'([client table key]
[client table key
{:keys [^String sql-condition
^Map attr-names
^Map attr-values
^Map set
^Map add
^List remove
^Map delete
^String return-consumed-capacity
^String return-item-collection-metrics
^String return-values]}])}
([client table key]
(update-item client table key nil))
([client table key options]
(-> options
(assoc :table table :key key)
(pre-process)
(->> (client/make-request client "UpdateItem"))
(post-process))))
;; https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Query.html
;; https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_Query.html
(defn query
{:arglists
'([client table]
[client table
{:keys [^Boolean consistent-read?
^String sql-filter
^String index
^Long limit
^Boolean scan-forward?
^Map start-key
^String select
^Boolean return-consumed-capacity
^String sql-key
^List attrs-get
^Map attr-names
^Map attr-values]}])}
([client table]
(query client table nil))
([client table options]
(-> options
(assoc :table table)
(pre-process)
(->> (client/make-request client "Query"))
(post-process))))
;; https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_Scan.html
(defn scan
{:arglists
'([client table]
[client table
{:keys [^Map attr-names
^Map attr-values
^List attrs-get
^Boolean consistent-read?
^String sql-filter
^Boolean index
^Long limit
^String return-consumed-capacity
^Long segment
^String select
^Map start-key
^Long total-segments]}])}
([client table]
(scan client table nil))
([client table options]
(-> options
(assoc :table table)
(pre-process)
(->> (client/make-request client "Scan"))
(post-process))))
;; https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_TagResource.html
(defn tag-resource
([client resource-arn tags]
(tag-resource client resource-arn tags nil))
([client resource-arn tags options]
(-> options
(assoc :tags tags :resource-arn resource-arn)
(pre-process)
(->> (client/make-request client "TagResource"))
(post-process))))
(defn api-call
[client target payload]
(client/make-request client target payload))