Skip to content

Commit 7808b3c

Browse files
authored
Merge branch 'main' into handle-binary-file-crashes
2 parents a6a48c5 + 4280e03 commit 7808b3c

File tree

1 file changed

+24
-3
lines changed

1 file changed

+24
-3
lines changed

codeflash/api/cfapi.py

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import json
44
import os
5+
import sys
56
from functools import lru_cache
67
from pathlib import Path
78
from typing import TYPE_CHECKING, Any, Optional
@@ -12,11 +13,13 @@
1213
from codeflash.cli_cmds.console import console, logger
1314
from codeflash.code_utils.env_utils import ensure_codeflash_api_key, get_codeflash_api_key, get_pr_number
1415
from codeflash.code_utils.git_utils import get_repo_owner_and_name
16+
from codeflash.version import __version__
1517

1618
if TYPE_CHECKING:
1719
from requests import Response
1820

1921
from codeflash.github.PrComment import FileDiffContent, PrComment
22+
from packaging import version
2023

2124
if os.environ.get("CODEFLASH_CFAPI_SERVER", default="prod").lower() == "local":
2225
CFAPI_BASE_URL = "http://localhost:3001"
@@ -26,7 +29,9 @@
2629
CFAPI_BASE_URL = "https://app.codeflash.ai"
2730

2831

29-
def make_cfapi_request(endpoint: str, method: str, payload: dict[str, Any] | None = None) -> Response:
32+
def make_cfapi_request(
33+
endpoint: str, method: str, payload: dict[str, Any] | None = None, extra_headers: dict[str, str] | None = None
34+
) -> Response:
3035
"""Make an HTTP request using the specified method, URL, headers, and JSON payload.
3136
3237
:param endpoint: The endpoint URL to send the request to.
@@ -36,6 +41,8 @@ def make_cfapi_request(endpoint: str, method: str, payload: dict[str, Any] | Non
3641
"""
3742
url = f"{CFAPI_BASE_URL}/cfapi{endpoint}"
3843
cfapi_headers = {"Authorization": f"Bearer {get_codeflash_api_key()}"}
44+
if extra_headers:
45+
cfapi_headers.update(extra_headers)
3946
if method.upper() == "POST":
4047
json_payload = json.dumps(payload, indent=None, default=pydantic_encoder)
4148
cfapi_headers["Content-Type"] = "application/json"
@@ -54,9 +61,23 @@ def get_user_id() -> Optional[str]:
5461
if not ensure_codeflash_api_key():
5562
return None
5663

57-
response = make_cfapi_request(endpoint="/cli-get-user", method="GET")
64+
response = make_cfapi_request(endpoint="/cli-get-user", method="GET", extra_headers={"cli_version": __version__})
5865
if response.status_code == 200:
59-
return response.text
66+
if "min_version" not in response.text:
67+
return response.text
68+
resp_json = response.json()
69+
userid: str | None = resp_json.get("userId")
70+
min_version: str | None = resp_json.get("min_version")
71+
if userid:
72+
if min_version and version.parse(min_version) > version.parse(__version__):
73+
msg = "Your Codeflash CLI version is outdated. Please update to the latest version using `pip install --upgrade codeflash`."
74+
console.print(f"[bold red]{msg}[/bold red]")
75+
sys.exit(1)
76+
return userid
77+
78+
logger.error("Failed to retrieve userid from the response.")
79+
return None
80+
6081
logger.error(f"Failed to look up your userid; is your CF API key valid? ({response.reason})")
6182
return None
6283

0 commit comments

Comments
 (0)