Skip to content
37 changes: 26 additions & 11 deletions codeflash/api/cfapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,31 @@ def make_cfapi_request(
cfapi_headers = {"Authorization": f"Bearer {get_codeflash_api_key()}"}
if extra_headers:
cfapi_headers.update(extra_headers)
if method.upper() == "POST":
json_payload = json.dumps(payload, indent=None, default=pydantic_encoder)
cfapi_headers["Content-Type"] = "application/json"
response = requests.post(url, data=json_payload, headers=cfapi_headers, timeout=60)
else:
response = requests.get(url, headers=cfapi_headers, timeout=60)
return response
try:
if method.upper() == "POST":
json_payload = json.dumps(payload, indent=None, default=pydantic_encoder)
cfapi_headers["Content-Type"] = "application/json"
response = requests.post(url, data=json_payload, headers=cfapi_headers, timeout=60)
else:
response = requests.get(url, headers=cfapi_headers, timeout=60)
response.raise_for_status()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is good.

return response # noqa: TRY300
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Q: whats noqa?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's for ignoring ruff linter for this line.

except requests.exceptions.HTTPError:
# response may be either a string or JSON, so we handle both cases
error_message = ""
try:
json_response = response.json()
if "error" in json_response:
error_message = json_response["error"]
elif "message" in json_response:
error_message = json_response["message"]
except (ValueError, TypeError):
error_message = response.text

logger.error(
f"CF_API_Error:: making request to Codeflash API (url: {url}, method: {method}, status {response.status_code}): {error_message}"
)
return response


@lru_cache(maxsize=1)
Expand Down Expand Up @@ -165,10 +183,7 @@ def is_github_app_installed_on_repo(owner: str, repo: str) -> bool:
:return: The response object.
"""
response = make_cfapi_request(endpoint=f"/is-github-app-installed?repo={repo}&owner={owner}", method="GET")
if not response.ok or response.text != "true":
logger.error(f"Error: {response.text}")
return False
return True
return response.ok and response.text == "true"


def get_blocklisted_functions() -> dict[str, set[str]] | dict[str, Any]:
Expand Down
Loading