-
-
Notifications
You must be signed in to change notification settings - Fork 87
Expand file tree
/
Copy pathrequest.py
More file actions
220 lines (181 loc) · 5.82 KB
/
request.py
File metadata and controls
220 lines (181 loc) · 5.82 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
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Low-level HTTP functionality"""
from collections import namedtuple
from typing import (
TYPE_CHECKING,
Any,
Dict,
List,
Literal,
Optional,
Tuple,
TypedDict,
Union,
overload,
)
from requests import RequestException, Response, Session, JSONDecodeError
if TYPE_CHECKING:
from io import TextIOWrapper
Json = List[Dict[str, Any]]
EmptyJson = List[dict]
__author__ = "Scott Burns <scott.s.burns@gmail.com>"
__license__ = "MIT"
__copyright__ = "2014, Vanderbilt University"
RedcapError = RequestException
_session = Session()
class FileUpload(TypedDict):
"""Typing for the file upload API"""
file: Tuple[str, "TextIOWrapper"]
_ContentConfig = namedtuple("_ContentConfig", ["return_empty_json", "return_bytes"])
class _RCRequest:
"""
Private class wrapping the REDCap API. Decodes response from redcap
and returns it.
"""
def __init__(
self,
url: str,
payload: Dict[str, Any],
config: _ContentConfig,
session=_session,
):
"""Constructor
Args:
url: REDCap API URL
payload: Keys and values corresponding to the REDCap API
config: Configuration values for getting content
"""
self.url = url
self.payload = payload
self.config = config
self.session = session
self.fmt = self._get_format_key(payload)
@staticmethod
def _get_format_key(
payload: Dict[str, Any]
) -> Optional[Literal["json", "csv", "xml"]]:
"""Determine format of the response
Args:
payload: Payload to be sent in POST request
Returns:
The expected format of the response, if a format
key was provided. Otherwise returns None to signal
a non-standard response format e.g bytes, empty json, etc.
Raises:
ValueError: Unsupported format
"""
if "returnFormat" in payload:
fmt_key = "returnFormat"
elif "format" in payload:
fmt_key = "format"
else:
return None
return payload[fmt_key]
@overload
@staticmethod
def get_content(
response: Response,
format_type: None,
return_empty_json: Literal[True],
return_bytes: Literal[False],
) -> EmptyJson:
...
@overload
@staticmethod
def get_content(
response: Response,
format_type: None,
return_empty_json: Literal[False],
return_bytes: Literal[True],
) -> bytes:
...
@overload
@staticmethod
def get_content(
response: Response,
format_type: Literal["json"],
return_empty_json: Literal[False],
return_bytes: Literal[False],
) -> Union[Json, Dict[str, str]]:
# This should return json, but might also return an error dict
...
@overload
@staticmethod
def get_content(
response: Response,
format_type: Literal["csv", "xml"],
return_empty_json: Literal[False],
return_bytes: Literal[False],
) -> str:
...
@staticmethod
def get_content(
response: Response,
format_type: Optional[Literal["json", "csv", "xml"]],
return_empty_json: bool,
return_bytes: bool,
):
"""Abstraction for grabbing content from a returned response"""
if return_bytes:
return response.content
if return_empty_json:
return [{}]
if format_type == "json":
try:
return response.json()
except JSONDecodeError as jde:
raise RedcapError("Unable to decode response as JSON") from jde
# don't do anything to csv/xml strings
return response.text
def execute(
self,
verify_ssl: Union[bool, str],
return_headers: bool,
file: Optional[FileUpload],
**kwargs,
):
"""Execute the API request and return data
Args:
verify_ssl: Verify SSL. Can also be a path to CA_BUNDLE
return_headers:
Whether or not response headers should be returned along
with the request content
file: A file object to send along with the request
**kwargs: passed to requesets.request() to control
the configuration to perform requests to the api
Returns:
Data object from JSON decoding process if format=='json',
else return raw string (ie format=='csv'|'xml')
Raises:
RedcapError:
Badly formed request i.e record doesn't
exist, field doesn't exist, etc.
"""
response = self.session.post(
self.url, data=self.payload, verify=verify_ssl, files=file, **kwargs
)
if response.status_code == 500:
raise RedcapError(f"HTTP error 500 {response.reason}")
content = self.get_content(
response,
format_type=self.fmt,
return_empty_json=self.config.return_empty_json,
return_bytes=self.config.return_bytes,
)
if self.fmt == "json":
try:
bad_request = "error" in content.keys() # type: ignore
except AttributeError:
# we're not dealing with an error dict
bad_request = False
elif self.fmt == "csv":
bad_request = content.lower().startswith("error:") # type: ignore
# xml is the default returnFormat for error messages
elif self.fmt == "xml" or self.fmt is None:
bad_request = "<error>" in str(content).lower()
if bad_request:
raise RedcapError(content)
if return_headers:
return content, response.headers
return content