forked from anvilco/python-anvil
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrequests.py
More file actions
163 lines (123 loc) · 5.03 KB
/
requests.py
File metadata and controls
163 lines (123 loc) · 5.03 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
from typing import Any, Dict
from python_anvil.http import HTTPClient
class AnvilRequest:
show_headers = False
_client: HTTPClient
def get_url(self):
raise NotImplementedError
def _request(self, method, url, **kwargs):
if not self._client:
raise AssertionError(
"Client has not been initialized. Please use the constructors "
"provided by the other request implementations."
)
if method.upper() not in ["GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"]:
raise ValueError("Invalid HTTP method provided")
full_url = "/".join([self.get_url(), url]) if len(url) > 0 else self.get_url()
return self._client.request(method, full_url, **kwargs)
def handle_error(self, response, status_code, headers):
extra = None
if self.show_headers:
extra = headers
if hasattr(response, "decode"):
message = f"Error: {status_code}: {response.decode()} {extra}"
else:
message = f"Error: {status_code}: {response} {extra}"
# pylint: disable=broad-exception-raised
raise Exception(message)
def process_response(self, response, status_code, headers, **kwargs):
res = response
if not 200 <= status_code < 300:
self.handle_error(response, status_code, headers)
debug = kwargs.pop("debug", False)
# Include headers alongside the response.
# This is useful for figuring out rate limits outside of this library's
# scope and to manage waiting.
include_headers = kwargs.pop("include_headers", False)
if debug or include_headers:
return {"response": res, "headers": headers}
return res
class BaseAnvilHttpRequest(AnvilRequest):
def __init__(self, client, options=None):
self._client = client
self._options = options
def get_url(self):
raise NotImplementedError
def get(self, url, params=None, **kwargs):
retry = kwargs.pop("retry", True)
content, status_code, headers = self._request(
"GET", url, params=params, retry=retry
)
return self.process_response(content, status_code, headers, **kwargs)
def post(self, url, data=None, **kwargs):
retry = kwargs.pop("retry", True)
params = kwargs.pop("params", None)
content, status_code, headers = self._request(
"POST", url, json=data, retry=retry, params=params
)
return self.process_response(content, status_code, headers, **kwargs)
class GraphqlRequest(AnvilRequest):
"""Create a GraphQL request.
.. deprecated :: 2.0.0
Use `python_anvil.http.GQLClient` to make GraphQL queries and mutations.
"""
API_HOST = "https://graphql.useanvil.com"
def __init__(self, client: HTTPClient):
self._client = client
def get_url(self):
return f"{self.API_HOST}"
def post(self, query, variables=None, **kwargs):
return self.run_query("POST", query, variables=variables, **kwargs)
def post_multipart(self, files=None, **kwargs):
return self.run_query("POST", None, files=files, is_multipart=True, **kwargs)
def run_query(
self, method, query, variables=None, files=None, is_multipart=False, **kwargs
):
if not query and not files:
raise AssertionError(
"Either `query` or `files` must be passed into this method."
)
data: Dict[str, Any] = {}
if query:
data["query"] = query
if files and is_multipart:
# Make sure `data` is nothing when we're doing a multipart request.
data = {}
elif variables:
data["variables"] = variables
# Optional debug kwargs.
# At this point, only the CLI will pass this in as a
# "show me everything" sort of switch.
debug = kwargs.pop("debug", False)
include_headers = kwargs.pop("include_headers", False)
content, status_code, headers = self._request(
method,
# URL blank here since graphql requests don't append to url
'',
# Queries need to be wrapped by curly braces(?) based on the
# current API implementation.
# The current library for graphql query generation doesn't do this(?)
json=data,
files=files,
parse_json=True,
**kwargs,
)
return self.process_response(
content,
status_code,
headers,
debug=debug,
include_headers=include_headers,
**kwargs,
)
class RestRequest(BaseAnvilHttpRequest):
API_HOST = "https://app.useanvil.com"
API_BASE = "api"
API_VERSION = "v1"
def get_url(self):
return f"{self.API_HOST}/{self.API_BASE}/{self.API_VERSION}"
class PlainRequest(BaseAnvilHttpRequest):
API_HOST = "https://app.useanvil.com"
API_BASE = "api"
def get_url(self):
return f"{self.API_HOST}/{self.API_BASE}"