-
Notifications
You must be signed in to change notification settings - Fork 1
/
request_process.py
89 lines (46 loc) · 1.97 KB
/
request_process.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
83
84
85
86
87
88
89
import time
from arguments import Arguments
from request import Request
from sleep import Sleep
class RequestProcess:
def __init__(self, url):
self.request = Request(url)
self.arguments = Arguments()
self.sleep = Sleep()
def get(self, headers=None, timeout=None):
if not headers:
headers = self.format_request_headers()
response = self.request.get(headers, timeout)
if response.status_code == 429:
if self.is_wait_limit(response):
return self.get(headers, timeout)
if response.status_code == 403:
self.print_error_message(response)
if self.is_github_ratelimit(response):
return self.get(headers, timeout)
return response
def format_request_headers(self):
headers = {}
github_token = self.arguments.get_github_token()
if github_token:
headers['Authorization'] = f'token {github_token}'
return headers
def is_wait_limit(self, response):
sleep_time = None
if 'Retry-After' in response.headers:
sleep_time = int(response.headers['Retry-After'])
self.sleep.start(sleep_time)
return False
def is_github_ratelimit(self, response):
if 'X-RateLimit-Remaining' in response.headers:
limit_remaining = int(response.headers['X-RateLimit-Remaining'])
if limit_remaining == 0 and 'X-RateLimit-Reset' in response.headers:
reset_time = int(response.headers['X-RateLimit-Reset'])
current_time = int(time.time())
sleep_time = reset_time - current_time + 1
return self.sleep.start(sleep_time)
return False
def print_error_message(self, response):
error_resopnse = response.json()
if 'message' in error_response:
print(error_resopnse['message'])