-
Notifications
You must be signed in to change notification settings - Fork 68
/
Copy pathcosmosdb.py
82 lines (59 loc) · 2.24 KB
/
cosmosdb.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
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
import collections.abc
import typing
from azure.functions import _json as json
from azure.functions import _cosmosdb as cdb
from . import meta
class CosmosDBConverter(meta.InConverter, meta.OutConverter,
binding='cosmosDB'):
@classmethod
def check_input_type_annotation(cls, pytype: type) -> bool:
return issubclass(pytype, cdb.DocumentList)
@classmethod
def check_output_type_annotation(cls, pytype: type) -> bool:
return issubclass(pytype, (cdb.DocumentList, cdb.Document))
@classmethod
def decode(cls,
data: meta.Datum,
*,
trigger_metadata) -> typing.Optional[cdb.DocumentList]:
if data is None or data.type is None:
return None
data_type = data.type
if data_type in ['string', 'json']:
body = data.value
elif data_type == 'bytes':
body = data.value.decode('utf-8')
else:
raise NotImplementedError(
f'unsupported queue payload type: {data_type}')
documents = json.loads(body)
if not isinstance(documents, list):
documents = [documents]
return cdb.DocumentList(
(None if doc is None else cdb.Document.from_dict(doc))
for doc in documents)
@classmethod
def encode(cls, obj: typing.Any, *,
expected_type: typing.Optional[type]) -> meta.Datum:
if isinstance(obj, cdb.Document):
data = cdb.DocumentList([obj])
elif isinstance(obj, cdb.DocumentList):
data = obj
elif isinstance(obj, collections.abc.Iterable):
data = cdb.DocumentList()
for doc in obj:
if not isinstance(doc, cdb.Document):
raise NotImplementedError
else:
data.append(doc)
else:
raise NotImplementedError
return meta.Datum(
type='json',
value=json.dumps([dict(d) for d in data])
)
class CosmosDBTriggerConverter(CosmosDBConverter,
binding='cosmosDBTrigger', trigger=True):
pass