Skip to content

Commit

Permalink
Use requests session
Browse files Browse the repository at this point in the history
  • Loading branch information
Anze committed Nov 30, 2019
1 parent 6ea4534 commit 3870a5a
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions grafoleancollector/collector.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,22 +205,23 @@ def fetch_job_configs(self, protocol):
to do their job.
"""
# find all the accounts we have access to:
r = requests.get('{}/accounts/?b={}'.format(self.backend_url, self.bot_token))
requests_session = requests.Session()
r = requests_session.get('{}/accounts/?b={}'.format(self.backend_url, self.bot_token))
if r.status_code != 200:
raise Exception("Invalid bot token or network error, got status {} while retrieving {}/accounts".format(r.status_code, self.backend_url))
j = r.json()
accounts_ids = [a["id"] for a in j["list"]]

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

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

r = requests.get('{}/accounts/{}/credentials/{}?b={}'.format(self.backend_url, account_id, credential_id, self.bot_token))
r = requests_session.get('{}/accounts/{}/credentials/{}?b={}'.format(self.backend_url, account_id, credential_id, self.bot_token))
if r.status_code != 200:
raise Exception("Network error, got status {} while retrieving {}/accounts/{}/credentials/{}".format(r.status_code, self.backend_url, account_id, credential_id))
credential = r.json()
Expand All @@ -245,7 +246,7 @@ def fetch_job_configs(self, protocol):
sensors = []
for sensor_info in entity_info["protocols"][protocol]["sensors"]:
sensor_id = sensor_info["sensor"]
r = requests.get('{}/accounts/{}/sensors/{}?b={}'.format(self.backend_url, account_id, sensor_id, self.bot_token))
r = requests_session.get('{}/accounts/{}/sensors/{}?b={}'.format(self.backend_url, account_id, sensor_id, self.bot_token))
if r.status_code != 200:
raise Exception("Network error, got status {} while retrieving {}/accounts/{}/sensors/{}".format(r.status_code, self.backend_url, account_id, sensor["sensor"]))
sensor = r.json()
Expand Down Expand Up @@ -275,6 +276,8 @@ def fetch_job_configs(self, protocol):

yield entity_info

requests_session.close()

def refresh_jobs(self):
wanted_jobs = set()
for job_id, intervals, job_func, job_data in self.jobs():
Expand Down

0 comments on commit 3870a5a

Please sign in to comment.