Skip to content

Commit 3870a5a

Browse files
author
Anze
committed
Use requests session
1 parent 6ea4534 commit 3870a5a

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

grafoleancollector/collector.py

+8-5
Original file line numberDiff line numberDiff line change
@@ -205,22 +205,23 @@ def fetch_job_configs(self, protocol):
205205
to do their job.
206206
"""
207207
# find all the accounts we have access to:
208-
r = requests.get('{}/accounts/?b={}'.format(self.backend_url, self.bot_token))
208+
requests_session = requests.Session()
209+
r = requests_session.get('{}/accounts/?b={}'.format(self.backend_url, self.bot_token))
209210
if r.status_code != 200:
210211
raise Exception("Invalid bot token or network error, got status {} while retrieving {}/accounts".format(r.status_code, self.backend_url))
211212
j = r.json()
212213
accounts_ids = [a["id"] for a in j["list"]]
213214

214215
# find all entities for each of the accounts:
215216
for account_id in accounts_ids:
216-
r = requests.get('{}/accounts/{}/entities/?b={}'.format(self.backend_url, account_id, self.bot_token))
217+
r = requests_session.get('{}/accounts/{}/entities/?b={}'.format(self.backend_url, account_id, self.bot_token))
217218
if r.status_code != 200:
218219
raise Exception("Network error, got status {} while retrieving {}/accounts/{}/entities".format(r.status_code, self.backend_url, account_id))
219220
j = r.json()
220221
entities_ids = [e["id"] for e in j["list"]]
221222

222223
for entity_id in entities_ids:
223-
r = requests.get('{}/accounts/{}/entities/{}?b={}'.format(self.backend_url, account_id, entity_id, self.bot_token))
224+
r = requests_session.get('{}/accounts/{}/entities/{}?b={}'.format(self.backend_url, account_id, entity_id, self.bot_token))
224225
if r.status_code != 200:
225226
raise Exception("Network error, got status {} while retrieving {}/accounts/{}/entities/{}".format(r.status_code, self.backend_url, account_id, entity_id))
226227
entity_info = r.json()
@@ -236,7 +237,7 @@ def fetch_job_configs(self, protocol):
236237
if not entity_info["protocols"][protocol]["sensors"]:
237238
continue
238239

239-
r = requests.get('{}/accounts/{}/credentials/{}?b={}'.format(self.backend_url, account_id, credential_id, self.bot_token))
240+
r = requests_session.get('{}/accounts/{}/credentials/{}?b={}'.format(self.backend_url, account_id, credential_id, self.bot_token))
240241
if r.status_code != 200:
241242
raise Exception("Network error, got status {} while retrieving {}/accounts/{}/credentials/{}".format(r.status_code, self.backend_url, account_id, credential_id))
242243
credential = r.json()
@@ -245,7 +246,7 @@ def fetch_job_configs(self, protocol):
245246
sensors = []
246247
for sensor_info in entity_info["protocols"][protocol]["sensors"]:
247248
sensor_id = sensor_info["sensor"]
248-
r = requests.get('{}/accounts/{}/sensors/{}?b={}'.format(self.backend_url, account_id, sensor_id, self.bot_token))
249+
r = requests_session.get('{}/accounts/{}/sensors/{}?b={}'.format(self.backend_url, account_id, sensor_id, self.bot_token))
249250
if r.status_code != 200:
250251
raise Exception("Network error, got status {} while retrieving {}/accounts/{}/sensors/{}".format(r.status_code, self.backend_url, account_id, sensor["sensor"]))
251252
sensor = r.json()
@@ -275,6 +276,8 @@ def fetch_job_configs(self, protocol):
275276

276277
yield entity_info
277278

279+
requests_session.close()
280+
278281
def refresh_jobs(self):
279282
wanted_jobs = set()
280283
for job_id, intervals, job_func, job_data in self.jobs():

0 commit comments

Comments
 (0)