-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathencoders.py
More file actions
156 lines (132 loc) · 4.54 KB
/
encoders.py
File metadata and controls
156 lines (132 loc) · 4.54 KB
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
from __future__ import annotations
import enum
import typing as t
import uuid
import requests
from globus_sdk import payload, utils
class RequestEncoder:
"""
A RequestEncoder takes input parameters and outputs a requests.Requests object.
The default encoder requires that the data is text and is a no-op. It can also be
referred to as the ``"text"`` encoder.
"""
def encode(
self,
method: str,
url: str,
params: dict[str, t.Any] | None,
data: t.Any,
headers: dict[str, str],
) -> requests.Request:
if not isinstance(data, (str, bytes)):
raise TypeError(
"Cannot encode non-text in a text request. "
"Either manually encode the data or use `encoding=form|json` to "
"correctly format this data."
)
return requests.Request(
method,
url,
data=self._prepare_data(data),
params=self._prepare_params(params),
headers=self._prepare_headers(headers),
)
def _format_primitive(self, value: t.Any) -> t.Any:
"""
Transformations for primitive values (e.g. stringifiable items) for query
params, headers, and body elements.
Transforms data as follows:
x: UUID -> str(x)
x: Enum -> x.value
x: _ -> x
"""
if isinstance(value, uuid.UUID):
return str(value)
if isinstance(value, enum.Enum):
return value.value
return value
def _prepare_params(
self, params: dict[str, t.Any] | None
) -> dict[str, t.Any] | None:
"""
Prepare the query params for a request.
Filters out MISSING and formats primitives.
"""
if params is None:
return None
return utils.filter_missing(
{k: self._format_primitive(v) for k, v in params.items()}
)
def _prepare_headers(
self, headers: dict[str, t.Any] | None
) -> dict[str, t.Any] | None:
"""
Prepare the headers for a request.
Filters out MISSING and formats primitives.
"""
if headers is None:
return None
return utils.filter_missing(
{k: self._format_primitive(v) for k, v in headers.items()}
)
def _prepare_data(self, data: t.Any) -> t.Any:
"""
Prepare the data (body) for a request.
If the body is a dict or PayloadWrapper, it will be recursively processed to
filter out MISSING and format primitives.
Otherwise, it is returned as-is.
"""
if isinstance(data, payload.Payload):
data = data.asdict()
if isinstance(data, (dict, utils.PayloadWrapper)):
return utils.filter_missing(
{k: self._prepare_data(v) for k, v in data.items()}
)
elif isinstance(data, (list, tuple)):
return [self._prepare_data(x) for x in data if x is not utils.MISSING]
else:
return self._format_primitive(data)
class JSONRequestEncoder(RequestEncoder):
"""
This encoder prepares the data as JSON. It also ensures that content-type is set, so
that APIs requiring a content-type of "application/json" are able to read the data.
"""
def encode(
self,
method: str,
url: str,
params: dict[str, t.Any] | None,
data: t.Any,
headers: dict[str, str],
) -> requests.Request:
if data is not None:
headers = {"Content-Type": "application/json", **headers}
return requests.Request(
method,
url,
json=self._prepare_data(data),
params=self._prepare_params(params),
headers=self._prepare_headers(headers),
)
class FormRequestEncoder(RequestEncoder):
"""
This encoder formats data as a form-encoded body. It requires that the input data is
a dict -- any other datatype will result in errors.
"""
def encode(
self,
method: str,
url: str,
params: dict[str, t.Any] | None,
data: t.Any,
headers: dict[str, str],
) -> requests.Request:
if not isinstance(data, (dict, utils.PayloadWrapper)):
raise TypeError("FormRequestEncoder cannot encode non-dict data")
return requests.Request(
method,
url,
data=self._prepare_data(data),
params=self._prepare_params(params),
headers=self._prepare_headers(headers),
)