Skip to content

Commit

Permalink
Merge pull request #27 from hhslepicka/fix/timeout-value
Browse files Browse the repository at this point in the history
FIX: Passing the timeout value downstream to the implementation.
  • Loading branch information
kayqueGovetri authored Jan 5, 2024
2 parents 6db6d99 + 0c49062 commit 5cc4520
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 38 deletions.
16 changes: 8 additions & 8 deletions botcity/maestro/datapool/datapool.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ def activate(self):
url = f'{self.maestro.server}/api/v2/datapool/{self.label}'

with requests.post(url, data=json.dumps(data), headers=self.maestro._headers(),
timeout=self.maestro.timeout) as req:
timeout=self.maestro._timeout) as req:
if req.ok:
self._update_from_json(payload=req.content)
return True
Expand All @@ -121,7 +121,7 @@ def deactivate(self):
data = self.to_dict()
data['active'] = False
with requests.post(url, data=json.dumps(data), headers=self.maestro._headers(),
timeout=self.maestro.timeout) as req:
timeout=self.maestro._timeout) as req:
if req.ok:
self._update_from_json(payload=req.content)
return True
Expand All @@ -135,7 +135,7 @@ def is_active(self) -> bool:
"""
url = f'{self.maestro.server}/api/v2/datapool/{self.label}'

with requests.get(url, headers=self.maestro._headers(), timeout=self.maestro.timeout) as req:
with requests.get(url, headers=self.maestro._headers(), timeout=self.maestro._timeout) as req:
if req.ok:
self._update_from_json(payload=req.content)
return self.active
Expand All @@ -149,7 +149,7 @@ def summary(self) -> dict:
"""
url = f'{self.maestro.server}/api/v2/datapool/{self.label}/summary'

with requests.get(url, headers=self.maestro._headers(), timeout=self.maestro.timeout) as req:
with requests.get(url, headers=self.maestro._headers(), timeout=self.maestro._timeout) as req:
if req.ok:
return json.loads(req.content)
req.raise_for_status()
Expand All @@ -168,7 +168,7 @@ def create_entry(self, entry: DataPoolEntry) -> DataPoolEntry:
url = f'{self.maestro.server}/api/v2/datapool/{self.label}/push'

with requests.post(url, data=entry.to_json(), headers=self.maestro._headers(),
timeout=self.maestro.timeout) as req:
timeout=self.maestro._timeout) as req:
if req.ok:
entry.update_from_json(payload=req.content)
return entry
Expand All @@ -184,7 +184,7 @@ def get_entry(self, entry_id: str) -> DataPoolEntry:
DataPoolEntry: The entry that was fetched.
"""
url = f'{self.maestro.server}/api/v2/datapool/{self.label}/entry/{entry_id}'
with requests.get(url, headers=self.maestro._headers(), timeout=self.maestro.timeout) as req:
with requests.get(url, headers=self.maestro._headers(), timeout=self.maestro._timeout) as req:
if req.ok:
entry = DataPoolEntry()
entry.update_from_json(payload=req.content)
Expand Down Expand Up @@ -222,7 +222,7 @@ def next(self, task_id: Optional[str]) -> Union[DataPoolEntry, None]:
"""
url = f'{self.maestro.server}/api/v2/datapool/{self.label}/pull'
with requests.get(url, headers=self.maestro._headers(), timeout=self.maestro.timeout) as req:
with requests.get(url, headers=self.maestro._headers(), timeout=self.maestro._timeout) as req:
if req.status_code == 204:
return None

Expand All @@ -240,5 +240,5 @@ def _delete(self):
Delete DataPool in Maestro.
"""
url = f'{self.maestro.server}/api/v2/datapool/{self.label}'
with requests.delete(url, headers=self.maestro._headers(), timeout=self.maestro.timeout) as req:
with requests.delete(url, headers=self.maestro._headers(), timeout=self.maestro._timeout) as req:
req.raise_for_status()
2 changes: 1 addition & 1 deletion botcity/maestro/datapool/entry.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ def save(self) -> 'DataPoolEntry':
"""
url = f'{self.maestro.server}/api/v2/datapool/{self.datapool_label}/entry/{self.entry_id}'
data = self.json_to_update()
with requests.post(url, data=data, headers=self.maestro._headers(), timeout=self.maestro.timeout) as req:
with requests.post(url, data=data, headers=self.maestro._headers(), timeout=self.maestro._timeout) as req:
if req.ok:
return self.update_from_json(req.content)
req.raise_for_status()
Expand Down
2 changes: 1 addition & 1 deletion botcity/maestro/impl/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ def __init__(self, server: Optional[str] = None, login: Optional[str] = None, ke
self._task_id = 0
self._impl: BotMaestroSDKInterface = None # type: ignore
self._version = None
self.timeout = 30.0
self._timeout = 30.0

self.server = server

Expand Down
56 changes: 28 additions & 28 deletions botcity/maestro/impl/v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,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, timeout=self.timeout) 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 @@ -87,7 +87,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(), timeout=self.timeout) 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 @@ -117,7 +117,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(), timeout=self.timeout) 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 Down Expand Up @@ -154,7 +154,7 @@ def create_task(self, activity_label: str, parameters: Dict[str, object],
data["minExecutionDate"] = min_execution_date.isoformat()

headers = self._headers()
with requests.post(url, json=data, headers=headers, timeout=self.timeout) 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 @@ -183,7 +183,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, timeout=self.timeout) 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 @@ -207,7 +207,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, timeout=self.timeout) 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 @@ -230,7 +230,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(), timeout=self.timeout) 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 @@ -255,7 +255,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, timeout=self.timeout) 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 @@ -282,7 +282,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(), timeout=self.timeout) 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 @@ -306,7 +306,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(), timeout=self.timeout) 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 @@ -335,7 +335,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(), timeout=self.timeout) 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 @@ -345,7 +345,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(), timeout=self.timeout) 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 @@ -386,7 +386,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(), timeout=self.timeout) 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 @@ -417,7 +417,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, timeout=self.timeout) as req:
with requests.post(url, data=data, headers=headers, timeout=self._timeout) as req:
if req.ok:
return artifact_id
else:
Expand All @@ -443,7 +443,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(), timeout=self.timeout) 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 @@ -463,13 +463,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(), timeout=self.timeout) 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(), timeout=self.timeout) 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 @@ -493,13 +493,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(), timeout=self.timeout) 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(), timeout=self.timeout) 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 @@ -542,7 +542,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(), timeout=self.timeout) 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 @@ -623,7 +623,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, timeout=self.timeout) 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 @@ -649,7 +649,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, timeout=self.timeout) 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 @@ -671,7 +671,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(), timeout=self.timeout) as req:
with requests.get(url, headers=self._headers(), timeout=self._timeout) as req:
if req.ok:
return str(req.text)
else:
Expand Down Expand Up @@ -703,7 +703,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(), timeout=self.timeout) 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 @@ -717,7 +717,7 @@ def _get_credential_by_label(self, label):
"""
url = f'{self._sdk._server}/api/v2/credential/{label}'

with requests.get(url, headers=self._headers(), timeout=self.timeout) 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 @@ -732,7 +732,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(), timeout=self.timeout) 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 @@ -751,7 +751,7 @@ def create_datapool(self, pool) -> DataPool:
url = f'{self._sdk.server}/api/v2/datapool'
pool.maestro = self
with requests.post(url, data=json.dumps(pool.to_dict()), headers=self._headers(),
timeout=self.timeout) as req:
timeout=self._timeout) as req:
if req.ok:
return pool
req.raise_for_status()
Expand All @@ -768,7 +768,7 @@ def get_datapool(self, label: str) -> DataPool:
"""
url = f'{self._sdk._server}/api/v2/datapool/{label}'

with requests.get(url, headers=self._headers(), timeout=self.timeout) as req:
with requests.get(url, headers=self._headers(), timeout=self._timeout) as req:
if req.ok:
return DataPool.from_json(payload=req.content, maestro=self)
req.raise_for_status()
11 changes: 11 additions & 0 deletions botcity/maestro/sdk.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,20 @@ def _define_implementation(self):
raise ex
self._impl = v2.BotMaestroSDKV2(self.server, self._login, self._key, sdk=self)
self._version = "999.0.0"
self._impl._timeout = self.timeout
self._impl.access_token = self.access_token
self._impl._login = self._login

@property
def timeout(self):
return self._timeout

@timeout.setter
def timeout(self, value):
self._timeout = value
if self._impl:
self._impl._timeout = value

def login(self, server: Optional[str] = None, login: Optional[str] = None, key: Optional[str] = None):
"""
Obtain an access token with the configured BotMaestro portal.
Expand Down

0 comments on commit 5cc4520

Please sign in to comment.