-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathtransport.py
More file actions
198 lines (160 loc) · 6.55 KB
/
transport.py
File metadata and controls
198 lines (160 loc) · 6.55 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
import concurrent.futures
import threading
import time
from typing import Dict, Optional
import requests
from requests.adapters import HTTPAdapter
from token_bucket import Limiter, MemoryStorage
from urllib3.util.retry import Retry
from .constants import default_thread_count, retryable_statuses
from .exceptions import PresentationException
from .utils import PaginatedResponse
from .version import ClientVersion
class HTTPMethods:
GET = "get"
POST = "post"
PUT = "put"
DELETE = "delete"
PATCH = "patch"
HEAD = "head"
class HTTPClient(object):
"""HTTP Client base that automatically handles the following:
- Shared thread/session object
- Client version headers
- Automated retries
"""
def __init__(self, threads: Optional[int] = default_thread_count):
"""
:param threads: Number of threads to use concurrently.
"""
# Setup number of threads to use
self.threads = threads
# Initialize empty thread object
self.thread_local = None
self.client_version = ClientVersion.version()
self.shared_headers = {"x-frameio-client": f"python/{self.client_version}"}
# Configure retry strategy (very broad right now)
try:
self.retry_strategy = Retry(
total=100,
backoff_factor=2,
status_forcelist=retryable_statuses,
allowed_methods=["GET", "POST", "PUT", "GET", "DELETE"],
)
except TypeError: # to save compatibility with older versions of urllib3
self.retry_strategy = Retry(
total=100,
backoff_factor=2,
status_forcelist=retryable_statuses,
method_whitelist=["GET", "POST", "PUT", "GET", "DELETE"],
)
# Create real thread
self._initialize_thread()
def _initialize_thread(self):
self.thread_local = threading.local()
def _get_session(self):
# Create session only if needed
if not hasattr(self.thread_local, "session"):
http = requests.Session()
adapter = HTTPAdapter(max_retries=self.retry_strategy)
adapter.add_headers(self.shared_headers) # add version header
http.mount("https://", adapter)
http.mount("http://", adapter)
self.thread_local.session = http
# Return session
return self.thread_local.session
class APIClient(HTTPClient, object):
"""Frame.io API Client that handles automatic pagination, and lots of other nice things.
Args:
HTTPClient (class): HTTP Client base class
token (str): Frame.io developer token, JWT, or OAuth access token.
threads (int): Number of threads to concurrently use for uploads/downloads.
progress (bool): If True, show status bars in console.
"""
def __init__(self, token: str, host: str, threads: int, progress: bool):
super().__init__(threads)
self.host = host
self.token = token
self.threads = threads
self.progress = progress
self._initialize_thread()
self.session = self._get_session()
self.auth_header = {"Authorization": f"Bearer {self.token}"}
def _format_api_call(self, endpoint: str):
return f"{self.host}/v2{endpoint}"
def _api_call(
self, method, endpoint: str, payload: Dict = {}, limit: Optional[int] = None
):
headers = {**self.shared_headers, **self.auth_header}
r = self.session.request(
method, self._format_api_call(endpoint), headers=headers, json=payload
)
if r.ok:
if r.headers.get("page-number"):
if int(r.headers.get("total-pages")) > 1:
return PaginatedResponse(
results=r.json(),
limit=limit,
page_size=r.headers["per-page"],
total_pages=r.headers["total-pages"],
total=r.headers["total"],
endpoint=endpoint,
method=method,
payload=payload,
client=self,
)
if isinstance(r.json(), list):
return r.json()[:limit]
return r.json()
if r.status_code == 422 and "presentation" in endpoint:
raise PresentationException
return r.raise_for_status()
def get_specific_page(
self, method: HTTPMethods, endpoint: str, payload: Dict, page: int
):
"""
Gets a specific page for that endpoint, used by Pagination Class
:Args:
method (string): 'get', 'post'
endpoint (string): endpoint ('/accounts/<ACCOUNT_ID>/teams')
payload (dict): Request payload
page (int): What page to get
"""
if method == HTTPMethods.GET:
endpoint = "{endpoint}?page={page}"
return self._api_call(method, endpoint)
if method == HTTPMethods.POST:
payload["page"] = page
return self._api_call(method, endpoint, payload=payload)
def exec_stream(callable, iterable, sync=lambda _: False, capacity=10, rate=10):
"""
Executes a stream according to a defined rate limit.
"""
limiter = Limiter(capacity, rate, MemoryStorage())
futures = set()
def execute(operation):
return (operation, callable(operation))
with concurrent.futures.ThreadPoolExecutor(max_workers=capacity) as executor:
while True:
if not limiter.consume("stream", 1):
start = int(time.time())
done, pending = concurrent.futures.wait(
futures, return_when=concurrent.futures.FIRST_COMPLETED
)
for future in done:
yield future.result()
futures = pending
if (int(time.time()) - start) < 1:
time.sleep(
1.0 / rate
) # guarantee there's capacity in the rate limit at end of the loop
operation = next(iterable, None)
if not operation:
done, _ = concurrent.futures.wait(futures)
for future in done:
yield future.result()
break
if sync(operation):
yield execute(operation)
continue
futures.add(executor.submit(execute, operation))