Skip to content

Commit

Permalink
Merge pull request #13 from kayqueGovetri/BUG/timeout-requests
Browse files Browse the repository at this point in the history
BUG: Fix timeout in requests
  • Loading branch information
hhslepicka authored Jan 27, 2023
2 parents b87dead + faf107a commit c3effec
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 30 deletions.
1 change: 1 addition & 0 deletions botcity/maestro/impl/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ def __init__(self, server: Optional[str] = None, login: Optional[str] = None, ke
self._task_id = None
self._impl: BotMaestroSDKInterface = None # type: ignore
self._version = None
self.timeout = 30.0

self.server = server

Expand Down
52 changes: 26 additions & 26 deletions botcity/maestro/impl/v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def login(self, server: Optional[str] = None, login: Optional[str] = None, key:
data = {"login": self._sdk.organization, "key": self._sdk._key}
headers = {'Content-Type': 'application/json'}

with requests.post(url, data=json.dumps(data), headers=headers) as req:
with requests.post(url, data=json.dumps(data), headers=headers, timeout=self.timeout) as req:
if req.ok:
self.access_token = req.json()['accessToken']
else:
Expand All @@ -86,7 +86,7 @@ def alert(self, task_id: str, title: str, message: str, alert_type: model.AlertT
data = {"taskId": task_id, "title": title,
"message": message, "type": alert_type}

with requests.post(url, json=data, headers=self._headers()) as req:
with requests.post(url, json=data, headers=self._headers(), timeout=self.timeout) as req:
if req.ok:
return model.ServerMessage.from_json(req.text)
else:
Expand Down Expand Up @@ -116,7 +116,7 @@ def message(self, email: List[str], users: List[str], subject: str, body: str,

data = {"emails": email, "logins": users, "subject": subject, "body": body,
"type": msg_type, "group": group}
with requests.post(url, json=data, headers=self._headers()) as req:
with requests.post(url, json=data, headers=self._headers(), timeout=self.timeout) as req:
if req.status_code != 200:
raise ValueError(
'Error during message. Server returned %d. %s' %
Expand All @@ -143,7 +143,7 @@ def create_task(self, activity_label: str, parameters: Dict[str, object],
"activityLabel": activity_label, "test": test, "parameters": parameters
}
headers = self._headers()
with requests.post(url, json=data, headers=headers) as req:
with requests.post(url, json=data, headers=headers, timeout=self.timeout) as req:
if req.ok:
return model.AutomationTask.from_json(req.text)
else:
Expand Down Expand Up @@ -172,7 +172,7 @@ def finish_task(self, task_id: str, status: model.AutomationTaskFinishStatus,
data = {"finishStatus": status, "finishMessage": message,
"state": "FINISHED"}
headers = self._headers()
with requests.post(url, json=data, headers=headers) as req:
with requests.post(url, json=data, headers=headers, timeout=self.timeout) as req:
if req.ok:
return model.ServerMessage.from_json(req.text)
else:
Expand All @@ -196,7 +196,7 @@ def restart_task(self, task_id: str) -> model.ServerMessage:
url = f'{self._sdk._server}/api/v2/task/{task_id}'
data = {"state": "START"}
headers = self._headers()
with requests.post(url, json=data, headers=headers) as req:
with requests.post(url, json=data, headers=headers, timeout=self.timeout) as req:
if req.ok:
return model.ServerMessage.from_json(req.text)
else:
Expand All @@ -219,7 +219,7 @@ def get_task(self, task_id: str) -> model.AutomationTask:
"""
url = f'{self._sdk._server}/api/v2/task/{task_id}'

with requests.get(url, headers=self._headers()) as req:
with requests.get(url, headers=self._headers(), timeout=self.timeout) as req:
if req.ok:
payload = req.text
return model.AutomationTask.from_json(payload)
Expand All @@ -244,7 +244,7 @@ def interrupt_task(self, task_id: str) -> model.ServerMessage:
url = f'{self._sdk._server}/api/v2/task/{task_id}'
data = {"interrupted": True}
headers = self._headers()
with requests.post(url, json=data, headers=headers) as req:
with requests.post(url, json=data, headers=headers, timeout=self.timeout) as req:
if req.ok:
return model.ServerMessage.from_json(req.text)
else:
Expand All @@ -271,7 +271,7 @@ def new_log(self, activity_label: str, columns: List[model.Column]) -> model.Ser
cols = [asdict(c) for c in columns]

data = {"activityLabel": activity_label, "columns": cols, 'organizationLabel': self._sdk.organization}
with requests.post(url, json=data, headers=self._headers()) as req:
with requests.post(url, json=data, headers=self._headers(), timeout=self.timeout) as req:
if req.ok:
return model.ServerMessage.from_json(req.text)
else:
Expand All @@ -295,7 +295,7 @@ def new_log_entry(self, activity_label: str, values: Dict[str, object]) -> model
"""
url = f'{self._sdk._server}/api/v2/log/{activity_label}/entry'

with requests.post(url, json=values, headers=self._headers()) as req:
with requests.post(url, json=values, headers=self._headers(), timeout=self.timeout) as req:
if req.status_code != 200:
try:
message = 'Error during new log entry. Server returned %d. %s' % (
Expand Down Expand Up @@ -324,7 +324,7 @@ def get_log(self, activity_label: str, date: Optional[str] = "") -> List[Dict[st
if date:
days = (datetime.datetime.now()-datetime.datetime.strptime(date, "%d/%m/%Y")).days + 1

with requests.get(url, headers=self._headers()) as req:
with requests.get(url, headers=self._headers(), timeout=self.timeout) as req:
if req.ok:
log = req.json()
columns = log.get('columns')
Expand All @@ -334,7 +334,7 @@ def get_log(self, activity_label: str, date: Optional[str] = "") -> List[Dict[st
url = f'{self._sdk._server}/api/v2/log/{activity_label}/entry-list'

data = {"days": days}
with requests.get(url, params=data, headers=self._headers()) as entry_req:
with requests.get(url, params=data, headers=self._headers(), timeout=self.timeout) as entry_req:
if entry_req.ok:
log_data = []
for en in entry_req.json():
Expand Down Expand Up @@ -375,7 +375,7 @@ def delete_log(self, activity_label: str) -> model.ServerMessage:
# date em branco eh tudo
url = f'{self._sdk._server}/api/v2/log/{activity_label}'

with requests.delete(url, headers=self._headers()) as req:
with requests.delete(url, headers=self._headers(), timeout=self.timeout) as req:
if req.status_code != 200:
try:
message = 'Error during log delete. Server returned %d. %s' % (
Expand Down Expand Up @@ -406,7 +406,7 @@ def post_artifact(self, task_id: int, artifact_name: str, filepath: str) -> mode
fields={'file': (artifact_name, f)}
)
headers = {**self._headers(), 'Content-Type': data.content_type}
with requests.post(url, data=data, headers=headers) as req:
with requests.post(url, data=data, headers=headers, timeout=self.timeout) as req:
if req.ok:
return artifact_id
else:
Expand All @@ -432,7 +432,7 @@ def create_artifact(self, task_id: int, name: str, filename: str) -> model.Serve
"""
url = f'{self._sdk._server}/api/v2/artifact'
data = {'taskId': task_id, 'name': name, 'filename': filename}
with requests.post(url, json=data, headers=self._headers()) as req:
with requests.post(url, json=data, headers=self._headers(), timeout=self.timeout) as req:
if req.ok:
return model.ServerMessage.from_json(req.text)
else:
Expand All @@ -452,13 +452,13 @@ def list_artifacts(self, days: int = 7) -> List[model.Artifact]:
"""
url = f'{self._sdk._server}/api/v2/artifact?size=5&page=0&sort=dateCreation,desc&days={days}'

with requests.get(url, headers=self._headers()) as req:
with requests.get(url, headers=self._headers(), timeout=self.timeout) as req:
if req.ok:
content = req.json()['content']
response = [model.Artifact.from_dict(a) for a in content]
for page in range(1, req.json()['totalPages']):
url = f'{self._sdk._server}/api/v2/artifact?size=5&page={page}&sort=dateCreation,desc&days={days}'
with requests.get(url, headers=self._headers()) as req:
with requests.get(url, headers=self._headers(), timeout=self.timeout) as req:
content = req.json()['content']
response.extend([model.Artifact.from_dict(a) for a in content])
return response
Expand All @@ -482,13 +482,13 @@ def get_artifact(self, artifact_id: int) -> Tuple[str, bytes]:
"""
url = f'{self._sdk._server}/api/v2/artifact/{artifact_id}'

with requests.get(url, headers=self._headers()) as req:
with requests.get(url, headers=self._headers(), timeout=self.timeout) as req:
if req.ok:
payload = req.json()
filename = payload['fileName']

url = f'{self.server}/api/v2/artifact/{artifact_id}/file'
with requests.get(url, headers=self._headers()) as req_file:
with requests.get(url, headers=self._headers(), timeout=self.timeout) as req_file:
file_content = req_file.content

return filename, file_content
Expand Down Expand Up @@ -531,7 +531,7 @@ def error(self, task_id: int, exception: Exception, screenshot: Optional[str] =
'stackTrace': trace, 'language': 'PYTHON', 'tags': tags}

response = None
with requests.post(url, json=data, headers=self._headers()) as req:
with requests.post(url, json=data, headers=self._headers(), timeout=self.timeout) as req:
if req.status_code == 201:
response = req.json()
else:
Expand Down Expand Up @@ -612,7 +612,7 @@ def _create_screenshot(self, error_id: int, filepath: str) -> None:
headers = self._headers()
headers['Content-Type'] = data_screenshot.content_type

with requests.post(url_screenshot, data=data_screenshot, headers=headers) as req:
with requests.post(url_screenshot, data=data_screenshot, headers=headers, timeout=self.timeout) as req:
if not req.ok:
try:
message = 'Error during new log entry. Server returned %d. %s' % (
Expand All @@ -638,7 +638,7 @@ def _create_attachment(self, error_id: int, filename: str, buffer: IOBase):
)
headers = self._headers()
headers['Content-Type'] = file.content_type
with requests.post(url_attachments, data=file, headers=headers) as req:
with requests.post(url_attachments, data=file, headers=headers, timeout=self.timeout) as req:
if not req.ok:
try:
message = 'Error during new log entry. Server returned %d. %s' % (
Expand All @@ -660,7 +660,7 @@ def get_credential(self, label: str, key: str) -> str:
"""
url = f'{self._sdk._server}/api/v2/credential/{label}/key/{key}'

with requests.get(url, headers=self._headers()) as req:
with requests.get(url, headers=self._headers(), timeout=self.timeout) as req:
if req.ok:
return req.text
else:
Expand Down Expand Up @@ -692,7 +692,7 @@ def create_credential(self, label: str, key: str, value: str):
'value': value
}
url = f'{self._sdk._server}/api/v2/credential/{label}/key'
with requests.post(url, json=data, headers=self._headers()) as req:
with requests.post(url, json=data, headers=self._headers(), timeout=self.timeout) as req:
if not req.ok:
req.raise_for_status()

Expand All @@ -706,7 +706,7 @@ def _get_credential_by_label(self, label):
"""
url = f'{self._sdk._server}/api/v2/credential/{label}'

with requests.get(url, headers=self._headers()) as req:
with requests.get(url, headers=self._headers(), timeout=self.timeout) as req:
if req.ok:
return model.ServerMessage.from_json(req.text)
else:
Expand All @@ -721,7 +721,7 @@ def _create_credential_by_label(self, label: str, key: str, value):
}
url = f'{self._sdk._server}/api/v2/credential'

with requests.post(url, json=data, headers=self._headers()) as req:
with requests.post(url, json=data, headers=self._headers(), timeout=self.timeout) as req:
if req.ok:
return model.ServerMessage.from_json(req.text)
else:
Expand Down
5 changes: 1 addition & 4 deletions tests/integration/test_task.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# type: ignore

import pytest

from botcity.maestro import (AutomationTask, AutomationTaskFinishStatus,
BotMaestroSDK)

Expand All @@ -22,8 +20,7 @@ def test_get_task(maestro: BotMaestroSDK, task: AutomationTask):


def test_interrupting_task(maestro: BotMaestroSDK, task: AutomationTask):
with pytest.raises(ValueError):
maestro.interrupt_task(task_id=str(task.id))
maestro.interrupt_task(task_id=str(task.id))


def test_finish_task_to_success(maestro: BotMaestroSDK, task: AutomationTask):
Expand Down

0 comments on commit c3effec

Please sign in to comment.