-
Notifications
You must be signed in to change notification settings - Fork 431
/
Copy pathbase.py
1378 lines (1261 loc) · 57.4 KB
/
base.py
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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
Lowest level connection
"""
import asyncio
import json
import logging
import random
import sys
import time
import uuid
from base64 import b64decode
from threading import local
from typing import Any, Dict, List, Mapping, Optional, Sequence, cast
import botocore.client
import botocore.exceptions
from botocore.awsrequest import AWSPreparedRequest, create_request_object
from botocore.client import ClientError
from botocore.hooks import first_non_none_response
from botocore.exceptions import BotoCoreError
import aiobotocore.session
from aiobotocore.config import AioConfig
from aiobotocore.client import AioBaseClient
from aiobotocore.session import get_session as get_async_session
from pynamodb.async_util import wrap_secretly_sync_async_fn
from pynamodb.constants import (
RETURN_CONSUMED_CAPACITY_VALUES, RETURN_ITEM_COLL_METRICS_VALUES,
RETURN_ITEM_COLL_METRICS, RETURN_CONSUMED_CAPACITY, RETURN_VALUES_VALUES,
EXCLUSIVE_START_KEY, SCAN_INDEX_FORWARD, ATTR_DEFINITIONS,
BATCH_WRITE_ITEM, CONSISTENT_READ, DESCRIBE_TABLE, KEY_CONDITION_EXPRESSION,
BATCH_GET_ITEM, DELETE_REQUEST, SELECT_VALUES, RETURN_VALUES, REQUEST_ITEMS,
PROJECTION_EXPRESSION, SERVICE_NAME, DELETE_ITEM, PUT_REQUEST, UPDATE_ITEM, TABLE_NAME,
INDEX_NAME, KEY_SCHEMA, ATTR_NAME, ATTR_TYPE, TABLE_KEY, KEY_TYPE, GET_ITEM, UPDATE,
PUT_ITEM, SELECT, LIMIT, QUERY, SCAN, ITEM, LOCAL_SECONDARY_INDEXES,
KEYS, KEY, SEGMENT, TOTAL_SEGMENTS, CREATE_TABLE, PROVISIONED_THROUGHPUT, READ_CAPACITY_UNITS,
WRITE_CAPACITY_UNITS, GLOBAL_SECONDARY_INDEXES, PROJECTION, EXCLUSIVE_START_TABLE_NAME, TOTAL,
DELETE_TABLE, UPDATE_TABLE, LIST_TABLES, GLOBAL_SECONDARY_INDEX_UPDATES, ATTRIBUTES,
CONSUMED_CAPACITY, CAPACITY_UNITS, ATTRIBUTE_TYPES,
ITEMS, DEFAULT_ENCODING, BINARY, BINARY_SET, LAST_EVALUATED_KEY, RESPONSES, UNPROCESSED_KEYS,
UNPROCESSED_ITEMS, STREAM_SPECIFICATION, STREAM_VIEW_TYPE, STREAM_ENABLED,
EXPRESSION_ATTRIBUTE_NAMES, EXPRESSION_ATTRIBUTE_VALUES,
CONDITION_EXPRESSION, FILTER_EXPRESSION,
TRANSACT_WRITE_ITEMS, TRANSACT_GET_ITEMS, CLIENT_REQUEST_TOKEN, TRANSACT_ITEMS, TRANSACT_CONDITION_CHECK,
TRANSACT_GET, TRANSACT_PUT, TRANSACT_DELETE, TRANSACT_UPDATE, UPDATE_EXPRESSION,
RETURN_VALUES_ON_CONDITION_FAILURE_VALUES, RETURN_VALUES_ON_CONDITION_FAILURE,
AVAILABLE_BILLING_MODES, DEFAULT_BILLING_MODE, BILLING_MODE, PAY_PER_REQUEST_BILLING_MODE,
PROVISIONED_BILLING_MODE,
TIME_TO_LIVE_SPECIFICATION, ENABLED, UPDATE_TIME_TO_LIVE, TAGS, VALUE
)
from pynamodb.exceptions import (
TableError, QueryError, PutError, DeleteError, UpdateError, GetError, ScanError, TableDoesNotExist,
VerboseClientError,
TransactGetError, TransactWriteError)
from pynamodb.expressions.condition import Condition
from pynamodb.expressions.operand import Path
from pynamodb.expressions.projection import create_projection_expression
from pynamodb.expressions.update import Action, Update
from pynamodb.settings import get_settings_value, OperationSettings
from pynamodb.signals import pre_dynamodb_send, post_dynamodb_send
from pynamodb.types import HASH, RANGE
BOTOCORE_EXCEPTIONS = (BotoCoreError, ClientError)
RATE_LIMITING_ERROR_CODES = ['ProvisionedThroughputExceededException', 'ThrottlingException']
log = logging.getLogger(__name__)
log.addHandler(logging.NullHandler())
class MetaTable(object):
"""
A pythonic wrapper around table metadata
"""
def __init__(self, data: Optional[Dict]) -> None:
self.data = data or {}
self._range_keyname = None
self._hash_keyname = None
def __repr__(self) -> str:
if self.data:
return "MetaTable<{}>".format(self.data.get(TABLE_NAME))
return ""
@property
def range_keyname(self) -> Optional[str]:
"""
Returns the name of this table's range key
"""
if self._range_keyname is None:
for attr in self.data.get(KEY_SCHEMA, []):
if attr.get(KEY_TYPE) == RANGE:
self._range_keyname = attr.get(ATTR_NAME)
return self._range_keyname
@property
def hash_keyname(self) -> str:
"""
Returns the name of this table's hash key
"""
if self._hash_keyname is None:
for attr in self.data.get(KEY_SCHEMA, []):
if attr.get(KEY_TYPE) == HASH:
self._hash_keyname = attr.get(ATTR_NAME)
break
if self._hash_keyname is None:
raise ValueError("No hash_key found in key schema")
return self._hash_keyname
def get_key_names(self, index_name=None):
"""
Returns the names of the primary key attributes and index key attributes (if index_name is specified)
"""
key_names = [self.hash_keyname]
if self.range_keyname:
key_names.append(self.range_keyname)
if index_name is not None:
index_hash_keyname = self.get_index_hash_keyname(index_name)
if index_hash_keyname not in key_names:
key_names.append(index_hash_keyname)
index_range_keyname = self.get_index_range_keyname(index_name)
if index_range_keyname is not None and index_range_keyname not in key_names:
key_names.append(index_range_keyname)
return key_names
def has_index_name(self, index_name):
"""
Returns True if the base table has a global or local secondary index with index_name
"""
global_indexes = self.data.get(GLOBAL_SECONDARY_INDEXES)
local_indexes = self.data.get(LOCAL_SECONDARY_INDEXES)
indexes = (global_indexes or []) + (local_indexes or [])
return any(index.get(INDEX_NAME) == index_name for index in indexes)
def get_index_hash_keyname(self, index_name: str) -> str:
"""
Returns the name of the hash key for a given index
"""
global_indexes = self.data.get(GLOBAL_SECONDARY_INDEXES)
local_indexes = self.data.get(LOCAL_SECONDARY_INDEXES)
indexes = []
if local_indexes:
indexes += local_indexes
if global_indexes:
indexes += global_indexes
for index in indexes:
if index.get(INDEX_NAME) == index_name:
for schema_key in index.get(KEY_SCHEMA):
if schema_key.get(KEY_TYPE) == HASH:
return schema_key.get(ATTR_NAME)
raise ValueError("No hash key attribute for index: {}".format(index_name))
def get_index_range_keyname(self, index_name):
"""
Returns the name of the hash key for a given index
"""
global_indexes = self.data.get(GLOBAL_SECONDARY_INDEXES)
local_indexes = self.data.get(LOCAL_SECONDARY_INDEXES)
indexes = []
if local_indexes:
indexes += local_indexes
if global_indexes:
indexes += global_indexes
for index in indexes:
if index.get(INDEX_NAME) == index_name:
for schema_key in index.get(KEY_SCHEMA):
if schema_key.get(KEY_TYPE) == RANGE:
return schema_key.get(ATTR_NAME)
return None
def get_item_attribute_map(self, attributes: Dict, item_key=ITEM, pythonic_key: bool = True):
"""
Builds up a dynamodb compatible AttributeValue map
"""
if pythonic_key:
item_key = item_key
attr_map: Dict[str, Dict] = {
item_key: {}
}
for key, value in attributes.items():
# In this case, the user provided a mapping
# {'key': {'S': 'value'}}
if isinstance(value, dict):
attr_map[item_key][key] = value
else:
attr_map[item_key][key] = {
self.get_attribute_type(key): value
}
return attr_map
def get_attribute_type(self, attribute_name: str, value: Optional[Any] = None) -> str:
"""
Returns the proper attribute type for a given attribute name
"""
for attr in self.data.get(ATTR_DEFINITIONS, []):
if attr.get(ATTR_NAME) == attribute_name:
return attr.get(ATTR_TYPE)
if value is not None and isinstance(value, dict):
for key in ATTRIBUTE_TYPES:
if key in value:
return key
attr_names = [attr.get(ATTR_NAME) for attr in self.data.get(ATTR_DEFINITIONS, [])]
raise ValueError("No attribute {} in {}".format(attribute_name, attr_names))
def get_identifier_map(self, hash_key: str, range_key: Optional[str] = None, key: str = KEY):
"""
Builds the identifier map that is common to several operations
"""
kwargs: Dict[str, Any] = {
key: {
self.hash_keyname: {
self.get_attribute_type(self.hash_keyname): hash_key
}
}
}
if range_key is not None and self.range_keyname is not None:
kwargs[key][self.range_keyname] = {
self.get_attribute_type(self.range_keyname): range_key
}
return kwargs
def get_exclusive_start_key_map(self, exclusive_start_key):
"""
Builds the exclusive start key attribute map
"""
if isinstance(exclusive_start_key, dict) and self.hash_keyname in exclusive_start_key:
# This is useful when paginating results, as the LastEvaluatedKey returned is already
# structured properly
return {
EXCLUSIVE_START_KEY: exclusive_start_key
}
else:
return {
EXCLUSIVE_START_KEY: {
self.hash_keyname: {
self.get_attribute_type(self.hash_keyname): exclusive_start_key
}
}
}
class ConnectionMeta(type):
def __init__(self, name, bases, attrs):
super().__init__(name, bases, attrs)
for attr_name, attr_value in attrs.items():
suffix = "_async"
if attr_name.endswith(suffix) and asyncio.iscoroutinefunction(attr_value):
wrapped_fn = wrap_secretly_sync_async_fn(attr_value)
wrapped_fn.__name__ = wrapped_fn.__name__[:-len(suffix)]
setattr(self, wrapped_fn.__name__, wrapped_fn)
class Connection(metaclass=ConnectionMeta):
"""
A higher level abstraction over botocore
"""
def __init__(self,
region: Optional[str] = None,
host: Optional[str] = None,
read_timeout_seconds: Optional[float] = None,
connect_timeout_seconds: Optional[float] = None,
max_retry_attempts: Optional[int] = None,
base_backoff_ms: Optional[int] = None,
max_pool_connections: Optional[int] = None,
extra_headers: Optional[Mapping[str, str]] = None):
self._tables: Dict[str, MetaTable] = {}
self.host = host
self._local = local()
self._client = None
if region:
self.region = region
else:
self.region = get_settings_value('region')
if connect_timeout_seconds is not None:
self._connect_timeout_seconds = connect_timeout_seconds
else:
self._connect_timeout_seconds = get_settings_value('connect_timeout_seconds')
if read_timeout_seconds is not None:
self._read_timeout_seconds = read_timeout_seconds
else:
self._read_timeout_seconds = get_settings_value('read_timeout_seconds')
if max_retry_attempts is not None:
self._max_retry_attempts_exception = max_retry_attempts
else:
self._max_retry_attempts_exception = get_settings_value('max_retry_attempts')
if base_backoff_ms is not None:
self._base_backoff_ms = base_backoff_ms
else:
self._base_backoff_ms = get_settings_value('base_backoff_ms')
if max_pool_connections is not None:
self._max_pool_connections = max_pool_connections
else:
self._max_pool_connections = get_settings_value('max_pool_connections')
if extra_headers is not None:
self._extra_headers = extra_headers
else:
self._extra_headers = get_settings_value('extra_headers')
def __repr__(self) -> str:
return "Connection<{}>".format("BLOOP")
async def _sign_request(self, client, request):
auth = client._request_signer.get_auth_instance(
client._request_signer.signing_name,
client._request_signer.region_name,
client._request_signer.signature_version)
if asyncio.iscoroutine(auth):
auth = await auth
auth.add_auth(request)
async def _create_prepared_request(
self,
client,
params: Dict,
settings: OperationSettings,
) -> AWSPreparedRequest:
request = create_request_object(params)
await self._sign_request(client, request)
prepared_request = client._endpoint.prepare_request(request)
if self._extra_headers is not None:
prepared_request.headers.update(self._extra_headers)
if settings.extra_headers is not None:
prepared_request.headers.update(settings.extra_headers)
return prepared_request
async def dispatch(self, operation_name: str, operation_kwargs: Dict, settings: OperationSettings = OperationSettings.default) -> Dict:
"""
Dispatches `operation_name` with arguments `operation_kwargs`
Raises TableDoesNotExist if the specified table does not exist
"""
if operation_name not in [DESCRIBE_TABLE, LIST_TABLES, UPDATE_TABLE, UPDATE_TIME_TO_LIVE, DELETE_TABLE, CREATE_TABLE]:
if RETURN_CONSUMED_CAPACITY not in operation_kwargs:
operation_kwargs.update(self.get_consumed_capacity_map(TOTAL))
log.debug("Calling %s with arguments %s", operation_name, operation_kwargs)
table_name = operation_kwargs.get(TABLE_NAME)
req_uuid = uuid.uuid4()
self.send_pre_boto_callback(operation_name, req_uuid, table_name)
data = await self._make_api_call(operation_name, operation_kwargs)
if asyncio.iscoroutine(data):
data = await cast(Any, data)
self.send_post_boto_callback(operation_name, req_uuid, table_name)
if data and CONSUMED_CAPACITY in data:
capacity = data.get(CONSUMED_CAPACITY)
if isinstance(capacity, dict) and CAPACITY_UNITS in capacity:
capacity = capacity.get(CAPACITY_UNITS)
log.debug("%s %s consumed %s units", data.get(TABLE_NAME, ''), operation_name, capacity)
return data
def send_post_boto_callback(self, operation_name, req_uuid, table_name):
try:
post_dynamodb_send.send(self, operation_name=operation_name, table_name=table_name, req_uuid=req_uuid)
except Exception as e:
log.exception("post_boto callback threw an exception.")
def send_pre_boto_callback(self, operation_name, req_uuid, table_name):
try:
pre_dynamodb_send.send(self, operation_name=operation_name, table_name=table_name, req_uuid=req_uuid)
except Exception as e:
log.exception("pre_boto callback threw an exception.")
async def _make_api_call(self, operation_name: str, operation_kwargs: Dict, settings: OperationSettings = OperationSettings.default) -> Dict:
"""
This private method is here for two reasons:
1. It's faster to avoid using botocore's response parsing
2. It provides a place to monkey patch HTTP requests for unit testing
"""
client = await self.client
operation_model = client._service_model.operation_model(operation_name)
request_dict = await client._convert_to_request_dict(
operation_kwargs,
operation_model,
)
for i in range(0, self._max_retry_attempts_exception + 1):
attempt_number = i + 1
is_last_attempt_for_exceptions = i == self._max_retry_attempts_exception
http_response = None
prepared_request = None
try:
if prepared_request is not None:
# If there is a stream associated with the request, we need
# to reset it before attempting to send the request again.
# This will ensure that we resend the entire contents of the
# body.
prepared_request.reset_stream()
# Create a new request for each retry (including a new signature).
prepared_request = await self._create_prepared_request(client, request_dict, settings)
# Implement the before-send event from botocore
event_name = 'before-send.dynamodb.{}'.format(operation_model.name)
event_responses = await client._endpoint._event_emitter.emit(event_name, request=prepared_request)
event_response = first_non_none_response(event_responses)
if event_response is None:
http_response = await client._endpoint._send(prepared_request)
else:
http_response = event_response
is_last_attempt_for_exceptions = True # don't retry if we have an event response
data = json.loads(http_response.content)
except (ValueError, botocore.exceptions.HTTPClientError, botocore.exceptions.ConnectionError) as e:
if is_last_attempt_for_exceptions:
log.debug('Reached the maximum number of retry attempts: %s', attempt_number)
if http_response:
e.args += (http_response.text,)
raise
else:
# No backoff for fast-fail exceptions that likely failed at the frontend
log.debug(
'Retry needed for (%s) after attempt %s, retryable %s caught: %s',
operation_name,
attempt_number,
e.__class__.__name__,
e
)
continue
status_code = http_response.status_code
headers = http_response.headers
if status_code >= 300:
# Extract error code from __type
code = data.get('__type', '')
if '#' in code:
code = code.rsplit('#', 1)[1]
botocore_expected_format = {'Error': {'Message': data.get('message', '') or data.get('Message', ''), 'Code': code}}
verbose_properties = {
'request_id': headers.get('x-amzn-RequestId')
}
if REQUEST_ITEMS in operation_kwargs:
# Batch operations can hit multiple tables, report them comma separated
verbose_properties['table_name'] = ','.join(operation_kwargs[REQUEST_ITEMS])
elif TRANSACT_ITEMS in operation_kwargs:
# Transactional operations can also hit multiple tables, or have multiple updates within
# the same table
table_names = []
for item in operation_kwargs[TRANSACT_ITEMS]:
for op in item.values():
table_names.append(op[TABLE_NAME])
verbose_properties['table_name'] = ','.join(table_names)
else:
verbose_properties['table_name'] = operation_kwargs.get(TABLE_NAME)
try:
raise VerboseClientError(botocore_expected_format, operation_name, verbose_properties)
except VerboseClientError as e:
if is_last_attempt_for_exceptions:
log.debug('Reached the maximum number of retry attempts: %s', attempt_number)
raise
elif status_code < 500 and code not in RATE_LIMITING_ERROR_CODES:
# We don't retry on a ConditionalCheckFailedException or other 4xx (except for
# throughput related errors) because we assume they will fail in perpetuity.
# Retrying when there is already contention could cause other problems
# in part due to unnecessary consumption of throughput.
raise
else:
# We use fully-jittered exponentially-backed-off retries:
# https://www.awsarchitectureblog.com/2015/03/backoff.html
sleep_time_ms = random.randint(0, self._base_backoff_ms * (2 ** i))
log.debug(
'Retry with backoff needed for (%s) after attempt %s,'
'sleeping for %s milliseconds, retryable %s caught: %s',
operation_name,
attempt_number,
sleep_time_ms,
e.__class__.__name__,
e
)
time.sleep(sleep_time_ms / 1000.0)
continue
return self._handle_binary_attributes(data)
assert False # unreachable code
@staticmethod
def _handle_binary_attributes(data):
""" Simulate botocore's binary attribute handling """
if ITEM in data:
for attr in data[ITEM].values():
_convert_binary(attr)
if ITEMS in data:
for item in data[ITEMS]:
for attr in item.values():
_convert_binary(attr)
if RESPONSES in data:
if isinstance(data[RESPONSES], list):
for item in data[RESPONSES]:
for attr in item.values():
_convert_binary(attr)
else:
for item_list in data[RESPONSES].values():
for item in item_list:
for attr in item.values():
_convert_binary(attr)
if LAST_EVALUATED_KEY in data:
for attr in data[LAST_EVALUATED_KEY].values():
_convert_binary(attr)
if UNPROCESSED_KEYS in data:
for table_data in data[UNPROCESSED_KEYS].values():
for item in table_data[KEYS]:
for attr in item.values():
_convert_binary(attr)
if UNPROCESSED_ITEMS in data:
for table_unprocessed_requests in data[UNPROCESSED_ITEMS].values():
for request in table_unprocessed_requests:
for item_mapping in request.values():
for item in item_mapping.values():
for attr in item.values():
_convert_binary(attr)
if ATTRIBUTES in data:
for attr in data[ATTRIBUTES].values():
_convert_binary(attr)
return data
@property
def session(self) -> aiobotocore.session.AioSession:
"""
Returns a valid async aiobotocore session
"""
# botocore client creation is not thread safe as of v1.2.5+ (see issue #153)
if getattr(self._local, 'async_session', None) is None:
self._local.async_session = get_async_session()
return self._local.async_session
@property
async def client(self) -> AioBaseClient:
"""
Returns a botocore dynamodb client
"""
# botocore has a known issue where it will cache empty credentials
# https://github.com/boto/botocore/blob/4d55c9b4142/botocore/credentials.py#L1016-L1021
# if the client does not have credentials, we create a new client
# otherwise the client is permanently poisoned in the case of metadata service flakiness when using IAM roles
if not self._client or (self._client._request_signer and not self._client._request_signer._credentials):
config = AioConfig(
parameter_validation=False, # Disable unnecessary validation for performance
connect_timeout=self._connect_timeout_seconds,
read_timeout=self._read_timeout_seconds,
max_pool_connections=self._max_pool_connections)
self._client = await self.session._create_client(SERVICE_NAME, self.region, endpoint_url=self.host, config=config)
return self._client
async def get_meta_table_async(self, table_name: str, refresh: bool = False):
"""
Returns a MetaTable
"""
if table_name not in self._tables or refresh:
operation_kwargs = {
TABLE_NAME: table_name
}
try:
data = await self.dispatch(DESCRIBE_TABLE, operation_kwargs)
self._tables[table_name] = MetaTable(data.get(TABLE_KEY))
except BotoCoreError as e:
raise TableError("Unable to describe table: {}".format(e), e)
except ClientError as e:
if 'ResourceNotFound' in e.response['Error']['Code']:
raise TableDoesNotExist(e.response['Error']['Message'])
else:
raise
return self._tables[table_name]
async def create_table_async(
self,
table_name: str,
attribute_definitions: Optional[Any] = None,
key_schema: Optional[Any] = None,
read_capacity_units: Optional[int] = None,
write_capacity_units: Optional[int] = None,
global_secondary_indexes: Optional[Any] = None,
local_secondary_indexes: Optional[Any] = None,
stream_specification: Optional[Dict] = None,
billing_mode: str = DEFAULT_BILLING_MODE,
tags: Optional[Dict[str, str]] = None,
) -> Dict:
"""
Performs the CreateTable operation
"""
operation_kwargs: Dict[str, Any] = {
TABLE_NAME: table_name,
BILLING_MODE: billing_mode,
PROVISIONED_THROUGHPUT: {
READ_CAPACITY_UNITS: read_capacity_units,
WRITE_CAPACITY_UNITS: write_capacity_units,
}
}
attrs_list = []
if attribute_definitions is None:
raise ValueError("attribute_definitions argument is required")
for attr in attribute_definitions:
attrs_list.append({
ATTR_NAME: attr.get('attribute_name'),
ATTR_TYPE: attr.get('attribute_type')
})
operation_kwargs[ATTR_DEFINITIONS] = attrs_list
if billing_mode not in AVAILABLE_BILLING_MODES:
raise ValueError("incorrect value for billing_mode, available modes: {}".format(AVAILABLE_BILLING_MODES))
if billing_mode == PAY_PER_REQUEST_BILLING_MODE:
del operation_kwargs[PROVISIONED_THROUGHPUT]
elif billing_mode == PROVISIONED_BILLING_MODE:
del operation_kwargs[BILLING_MODE]
if global_secondary_indexes:
global_secondary_indexes_list = []
for index in global_secondary_indexes:
index_kwargs = {
INDEX_NAME: index.get('index_name'),
KEY_SCHEMA: sorted(index.get('key_schema'), key=lambda x: x.get(KEY_TYPE)),
PROJECTION: index.get('projection'),
PROVISIONED_THROUGHPUT: index.get('provisioned_throughput')
}
if billing_mode == PAY_PER_REQUEST_BILLING_MODE:
del index_kwargs[PROVISIONED_THROUGHPUT]
global_secondary_indexes_list.append(index_kwargs)
operation_kwargs[GLOBAL_SECONDARY_INDEXES] = global_secondary_indexes_list
if key_schema is None:
raise ValueError("key_schema is required")
key_schema_list = []
for item in key_schema:
key_schema_list.append({
ATTR_NAME: item.get('attribute_name'),
KEY_TYPE: str(item.get('key_type')).upper()
})
operation_kwargs[KEY_SCHEMA] = sorted(key_schema_list, key=lambda x: x.get(KEY_TYPE))
local_secondary_indexes_list = []
if local_secondary_indexes:
for index in local_secondary_indexes:
local_secondary_indexes_list.append({
INDEX_NAME: index.get('index_name'),
KEY_SCHEMA: sorted(index.get('key_schema'), key=lambda x: x.get(KEY_TYPE)),
PROJECTION: index.get('projection'),
})
operation_kwargs[LOCAL_SECONDARY_INDEXES] = local_secondary_indexes_list
if stream_specification:
operation_kwargs[STREAM_SPECIFICATION] = {
STREAM_ENABLED: stream_specification['stream_enabled'],
STREAM_VIEW_TYPE: stream_specification['stream_view_type']
}
if tags:
operation_kwargs[TAGS] = [
{
KEY: k,
VALUE: v
} for k, v in tags.items()
]
try:
data = await self.dispatch(CREATE_TABLE, operation_kwargs)
except BOTOCORE_EXCEPTIONS as e:
raise TableError("Failed to create table: {}".format(e), e)
return data
async def update_time_to_live_async(self, table_name: str, ttl_attribute_name: str) -> Dict:
"""
Performs the UpdateTimeToLive operation
"""
operation_kwargs = {
TABLE_NAME: table_name,
TIME_TO_LIVE_SPECIFICATION: {
ATTR_NAME: ttl_attribute_name,
ENABLED: True,
}
}
try:
return await self.dispatch(UPDATE_TIME_TO_LIVE, operation_kwargs)
except BOTOCORE_EXCEPTIONS as e:
raise TableError("Failed to update TTL on table: {}".format(e), e)
async def delete_table_async(self, table_name: str) -> Dict:
"""
Performs the DeleteTable operation
"""
operation_kwargs = {
TABLE_NAME: table_name
}
try:
data = await self.dispatch(DELETE_TABLE, operation_kwargs)
except BOTOCORE_EXCEPTIONS as e:
raise TableError("Failed to delete table: {}".format(e), e)
return data
async def update_table_async(
self,
table_name: str,
read_capacity_units: Optional[int] = None,
write_capacity_units: Optional[int] = None,
global_secondary_index_updates: Optional[Any] = None,
) -> Dict:
"""
Performs the UpdateTable operation
"""
operation_kwargs: Dict[str, Any] = {
TABLE_NAME: table_name
}
if read_capacity_units and not write_capacity_units or write_capacity_units and not read_capacity_units:
raise ValueError("read_capacity_units and write_capacity_units are required together")
if read_capacity_units and write_capacity_units:
operation_kwargs[PROVISIONED_THROUGHPUT] = {
READ_CAPACITY_UNITS: read_capacity_units,
WRITE_CAPACITY_UNITS: write_capacity_units
}
if global_secondary_index_updates:
global_secondary_indexes_list = []
for index in global_secondary_index_updates:
global_secondary_indexes_list.append({
UPDATE: {
INDEX_NAME: index.get('index_name'),
PROVISIONED_THROUGHPUT: {
READ_CAPACITY_UNITS: index.get('read_capacity_units'),
WRITE_CAPACITY_UNITS: index.get('write_capacity_units')
}
}
})
operation_kwargs[GLOBAL_SECONDARY_INDEX_UPDATES] = global_secondary_indexes_list
try:
return await self.dispatch(UPDATE_TABLE, operation_kwargs)
except BOTOCORE_EXCEPTIONS as e:
raise TableError("Failed to update table: {}".format(e), e)
async def list_tables_async(
self,
exclusive_start_table_name: Optional[str] = None,
limit: Optional[int] = None,
) -> Dict:
"""
Performs the ListTables operation
"""
operation_kwargs: Dict[str, Any] = {}
if exclusive_start_table_name:
operation_kwargs.update({
EXCLUSIVE_START_TABLE_NAME: exclusive_start_table_name
})
if limit is not None:
operation_kwargs.update({
LIMIT: limit
})
try:
return await self.dispatch(LIST_TABLES, operation_kwargs)
except BOTOCORE_EXCEPTIONS as e:
raise TableError("Unable to list tables: {}".format(e), e)
async def describe_table_async(self, table_name: str) -> Dict:
"""
Performs the DescribeTable operation
"""
try:
tbl = await self.get_meta_table_async(table_name, refresh=True)
if tbl:
return tbl.data
except ValueError:
pass
raise TableDoesNotExist(table_name)
async def get_item_attribute_map(
self,
table_name: str,
attributes: Any,
item_key: str = ITEM,
pythonic_key: bool = True,
) -> Dict:
"""
Builds up a dynamodb compatible AttributeValue map
"""
tbl = await self.get_meta_table_async(table_name)
if tbl is None:
raise TableError("No such table {}".format(table_name))
return tbl.get_item_attribute_map(
attributes,
item_key=item_key,
pythonic_key=pythonic_key)
def parse_attribute(
self,
attribute: Any,
return_type: bool = False
) -> Any:
"""
Returns the attribute value, where the attribute can be
a raw attribute value, or a dictionary containing the type:
{'S': 'String value'}
"""
if isinstance(attribute, dict):
for key in ATTRIBUTE_TYPES:
if key in attribute:
if return_type:
return key, attribute.get(key)
return attribute.get(key)
raise ValueError("Invalid attribute supplied: {}".format(attribute))
else:
if return_type:
return None, attribute
return attribute
async def get_attribute_type(
self,
table_name: str,
attribute_name: str,
value: Optional[Any] = None
) -> str:
"""
Returns the proper attribute type for a given attribute name
:param value: The attribute value an be supplied just in case the type is already included
"""
tbl = await self.get_meta_table_async(table_name)
if tbl is None:
raise TableError("No such table {}".format(table_name))
return tbl.get_attribute_type(attribute_name, value=value)
async def get_identifier_map(
self,
table_name: str,
hash_key: str,
range_key: Optional[str] = None,
key: str = KEY
) -> Dict:
"""
Builds the identifier map that is common to several operations
"""
tbl = await self.get_meta_table_async(table_name)
if tbl is None:
raise TableError("No such table {}".format(table_name))
return tbl.get_identifier_map(hash_key, range_key=range_key, key=key)
def get_consumed_capacity_map(self, return_consumed_capacity: str) -> Dict:
"""
Builds the consumed capacity map that is common to several operations
"""
if return_consumed_capacity.upper() not in RETURN_CONSUMED_CAPACITY_VALUES:
raise ValueError("{} must be one of {}".format(RETURN_ITEM_COLL_METRICS, RETURN_CONSUMED_CAPACITY_VALUES))
return {
RETURN_CONSUMED_CAPACITY: str(return_consumed_capacity).upper()
}
def get_return_values_map(self, return_values: str) -> Dict:
"""
Builds the return values map that is common to several operations
"""
if return_values.upper() not in RETURN_VALUES_VALUES:
raise ValueError("{} must be one of {}".format(RETURN_VALUES, RETURN_VALUES_VALUES))
return {
RETURN_VALUES: str(return_values).upper()
}
def get_return_values_on_condition_failure_map(
self,
return_values_on_condition_failure: str
) -> Dict:
"""
Builds the return values map that is common to several operations
"""
if return_values_on_condition_failure.upper() not in RETURN_VALUES_VALUES:
raise ValueError("{} must be one of {}".format(
RETURN_VALUES_ON_CONDITION_FAILURE,
RETURN_VALUES_ON_CONDITION_FAILURE_VALUES
))
return {
RETURN_VALUES_ON_CONDITION_FAILURE: str(return_values_on_condition_failure).upper()
}
def get_item_collection_map(self, return_item_collection_metrics: str) -> Dict:
"""
Builds the item collection map
"""
if return_item_collection_metrics.upper() not in RETURN_ITEM_COLL_METRICS_VALUES:
raise ValueError("{} must be one of {}".format(RETURN_ITEM_COLL_METRICS, RETURN_ITEM_COLL_METRICS_VALUES))
return {
RETURN_ITEM_COLL_METRICS: str(return_item_collection_metrics).upper()
}
async def get_exclusive_start_key_map(self, table_name: str, exclusive_start_key: str) -> Dict:
"""
Builds the exclusive start key attribute map
"""
tbl = await self.get_meta_table_async(table_name)
if tbl is None:
raise TableError("No such table {}".format(table_name))
return tbl.get_exclusive_start_key_map(exclusive_start_key)
async def get_operation_kwargs(
self,
table_name: str,
hash_key: str,
range_key: Optional[str] = None,
key: str = KEY,
attributes: Optional[Any] = None,
attributes_to_get: Optional[Any] = None,
actions: Optional[Sequence[Action]] = None,
condition: Optional[Condition] = None,
consistent_read: Optional[bool] = None,
return_values: Optional[str] = None,
return_consumed_capacity: Optional[str] = None,
return_item_collection_metrics: Optional[str] = None,
return_values_on_condition_failure: Optional[str] = None
) -> Dict:
self._check_condition('condition', condition)
operation_kwargs: Dict[str, Any] = {}
name_placeholders: Dict[str, str] = {}
expression_attribute_values: Dict[str, Any] = {}
operation_kwargs[TABLE_NAME] = table_name
operation_kwargs.update(await self.get_identifier_map(table_name, hash_key, range_key, key=key))
if attributes and operation_kwargs.get(ITEM) is not None:
attrs = await self.get_item_attribute_map(table_name, attributes)
operation_kwargs[ITEM].update(attrs[ITEM])
if attributes_to_get is not None:
projection_expression = create_projection_expression(attributes_to_get, name_placeholders)
operation_kwargs[PROJECTION_EXPRESSION] = projection_expression
if condition is not None:
condition_expression = condition.serialize(name_placeholders, expression_attribute_values)
operation_kwargs[CONDITION_EXPRESSION] = condition_expression
if consistent_read is not None:
operation_kwargs[CONSISTENT_READ] = consistent_read
if return_values is not None:
operation_kwargs.update(self.get_return_values_map(return_values))
if return_values_on_condition_failure is not None:
operation_kwargs.update(self.get_return_values_on_condition_failure_map(return_values_on_condition_failure))
if return_consumed_capacity is not None:
operation_kwargs.update(self.get_consumed_capacity_map(return_consumed_capacity))
if return_item_collection_metrics is not None:
operation_kwargs.update(self.get_item_collection_map(return_item_collection_metrics))
if actions is not None:
update_expression = Update(*actions)
operation_kwargs[UPDATE_EXPRESSION] = update_expression.serialize(
name_placeholders,
expression_attribute_values
)
if name_placeholders:
operation_kwargs[EXPRESSION_ATTRIBUTE_NAMES] = self._reverse_dict(name_placeholders)
if expression_attribute_values:
operation_kwargs[EXPRESSION_ATTRIBUTE_VALUES] = expression_attribute_values
return operation_kwargs
async def delete_item(
self,
table_name: str,
hash_key: str,
range_key: Optional[str] = None,
condition: Optional[Condition] = None,
return_values: Optional[str] = None,
return_consumed_capacity: Optional[str] = None,
return_item_collection_metrics: Optional[str] = None,
settings: OperationSettings = OperationSettings.default,
) -> Dict:
"""
Performs the DeleteItem operation and returns the result
"""
operation_kwargs = await self.get_operation_kwargs(
table_name,
hash_key,
range_key=range_key,
condition=condition,
return_values=return_values,
return_consumed_capacity=return_consumed_capacity,
return_item_collection_metrics=return_item_collection_metrics
)
try:
return await self.dispatch(DELETE_ITEM, operation_kwargs, settings)
except BOTOCORE_EXCEPTIONS as e:
raise DeleteError("Failed to delete item: {}".format(e), e)
async def update_item(
self,
table_name: str,
hash_key: str,
range_key: Optional[str] = None,
actions: Optional[Sequence[Action]] = None,
condition: Optional[Condition] = None,
return_consumed_capacity: Optional[str] = None,
return_item_collection_metrics: Optional[str] = None,
return_values: Optional[str] = None,
settings: OperationSettings = OperationSettings.default,
) -> Dict:
"""