Skip to content

Commit

Permalink
refactor(agent): Split request method up
Browse files Browse the repository at this point in the history
  • Loading branch information
balamurali27 committed Jan 9, 2025
1 parent 7648104 commit 7b14e8e
Showing 1 changed file with 29 additions and 35 deletions.
64 changes: 29 additions & 35 deletions press/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -745,64 +745,58 @@ def get(self, path, raises=True):
def post(self, path, data=None, raises=True):
return self.request("POST", path, data, raises=raises)

def _make_req(self, method, url, headers, data, files, agent_job_id):
intermediate_ca = frappe.db.get_value("Press Settings", "Press Settings", "backbone_intermediate_ca")
if frappe.conf.developer_mode and intermediate_ca:
root_ca = frappe.db.get_value("Certificate Authority", intermediate_ca, "parent_authority")
verify = frappe.get_doc("Certificate Authority", root_ca).certificate_file

Check warning on line 752 in press/agent.py

View check run for this annotation

Codecov / codecov/patch

press/agent.py#L751-L752

Added lines #L751 - L752 were not covered by tests
else:
verify = True
if files:
file_objects = {

Check warning on line 756 in press/agent.py

View check run for this annotation

Codecov / codecov/patch

press/agent.py#L756

Added line #L756 was not covered by tests
key: value
if isinstance(value, _io.BufferedReader)
else frappe.get_doc("File", {"file_url": url}).get_content()
for key, value in files.items()
}
file_objects["json"] = json.dumps(data).encode()
return requests.request(method, url, headers=headers, files=file_objects, verify=verify)

Check warning on line 763 in press/agent.py

View check run for this annotation

Codecov / codecov/patch

press/agent.py#L762-L763

Added lines #L762 - L763 were not covered by tests
return requests.request(method, url, headers=headers, json=data, verify=verify, timeout=(10, 30))

def request(self, method, path, data=None, files=None, agent_job=None, raises=True):
self.raise_if_past_requests_have_failed()
self.response = None
agent_job_id = agent_job.name if agent_job else None
headers = None
url = None

response = headers = url = None
try:
url = f"https://{self.server}:{self.port}/agent/{path}"
agent_job_id = agent_job.name if agent_job else None
password = get_decrypted_password(self.server_type, self.server, "agent_password")
headers = {"Authorization": f"bearer {password}", "X-Agent-Job-Id": agent_job_id}
intermediate_ca = frappe.db.get_value(
"Press Settings", "Press Settings", "backbone_intermediate_ca"
)
if frappe.conf.developer_mode and intermediate_ca:
root_ca = frappe.db.get_value("Certificate Authority", intermediate_ca, "parent_authority")
verify = frappe.get_doc("Certificate Authority", root_ca).certificate_file
else:
verify = True
if files:
file_objects = {
key: value
if isinstance(value, _io.BufferedReader)
else frappe.get_doc("File", {"file_url": url}).get_content()
for key, value in files.items()
}
file_objects["json"] = json.dumps(data).encode()
self.response = requests.request(
method, url, headers=headers, files=file_objects, verify=verify
)
else:
self.response = requests.request(
method, url, headers=headers, json=data, verify=verify, timeout=(10, 30)
)
url = f"https://{self.server}:{self.port}/agent/{path}"

response = self._make_req(method, url, headers, data, files, agent_job_id)
json_response = None
try:
json_response = self.response.json()
if raises and self.response.status_code >= 400:
json_response = response.json()
if raises and response.status_code >= 400:
output = "\n\n".join(

Check warning on line 780 in press/agent.py

View check run for this annotation

Codecov / codecov/patch

press/agent.py#L780

Added line #L780 was not covered by tests
[json_response.get("output", ""), json_response.get("traceback", "")]
)
if output == "\n\n":
output = json.dumps(json_response, indent=2, sort_keys=True)
raise HTTPError(

Check warning on line 785 in press/agent.py

View check run for this annotation

Codecov / codecov/patch

press/agent.py#L783-L785

Added lines #L783 - L785 were not covered by tests
f"{self.response.status_code} {self.response.reason} {output}",
response=self.response,
f"{response.status_code} {response.reason} {output}",
response=response,
)
return json_response
except Exception:
self.handle_request_failure(agent_job, self.response)
self.handle_request_failure(agent_job, response)

Check warning on line 791 in press/agent.py

View check run for this annotation

Codecov / codecov/patch

press/agent.py#L791

Added line #L791 was not covered by tests
log_error(
title="Agent Request Result Exception",
method=method,
url=url,
data=data,
files=files,
headers=headers,
result=json_response or self.response.text,
result=json_response or getattr(response, "text", None),
doc=agent_job,
)
except Exception as exc:
Expand Down

0 comments on commit 7b14e8e

Please sign in to comment.