|
1 | 1 | from __future__ import annotations |
2 | 2 |
|
3 | | -import hashlib |
4 | 3 | import json |
5 | 4 | import os |
6 | 5 | import sys |
7 | 6 | from functools import lru_cache |
8 | 7 | from pathlib import Path |
9 | | -from typing import TYPE_CHECKING, Any, Optional, Dict |
| 8 | +from typing import TYPE_CHECKING, Any, Dict, Optional |
10 | 9 |
|
11 | 10 | import requests |
12 | 11 | import sentry_sdk |
13 | 12 | from pydantic.json import pydantic_encoder |
| 13 | +from requests import Response |
14 | 14 |
|
15 | 15 | from codeflash.cli_cmds.console import console, logger |
16 | 16 | from codeflash.code_utils.env_utils import ensure_codeflash_api_key, get_codeflash_api_key, get_pr_number |
17 | 17 | from codeflash.code_utils.git_utils import get_repo_owner_and_name |
18 | | -from codeflash.models.models import CodeOptimizationContext |
19 | 18 | from codeflash.version import __version__ |
20 | 19 |
|
21 | 20 | if TYPE_CHECKING: |
@@ -43,15 +42,26 @@ def make_cfapi_request( |
43 | 42 | :return: The response object from the API. |
44 | 43 | """ |
45 | 44 | url = f"{CFAPI_BASE_URL}/cfapi{endpoint}" |
46 | | - cfapi_headers = {"Authorization": f"Bearer {get_codeflash_api_key()}"} |
| 45 | + |
| 46 | + headers = {"Authorization": f"Bearer {get_codeflash_api_key()}"} |
47 | 47 | if extra_headers: |
48 | | - cfapi_headers.update(extra_headers) |
49 | | - if method.upper() == "POST": |
50 | | - json_payload = json.dumps(payload, indent=None, default=pydantic_encoder) |
51 | | - cfapi_headers["Content-Type"] = "application/json" |
52 | | - response = requests.post(url, data=json_payload, headers=cfapi_headers, timeout=60) |
| 48 | + headers.update(extra_headers) |
| 49 | + method_u = method.upper() |
| 50 | + |
| 51 | + if method_u == "POST": |
| 52 | + # Use native requests post JSON argument for faster serialization and header setting. |
| 53 | + # Only use custom encoder if needed. |
| 54 | + try: |
| 55 | + response = requests.post(url, json=payload, headers=headers, timeout=60) |
| 56 | + except TypeError: |
| 57 | + # Fallback to manual dumping in rare user-supplied object cases |
| 58 | + json_payload = _json_dumps_fast(payload) |
| 59 | + # Only add the header if it wasn't already in extra_headers |
| 60 | + if "Content-Type" not in headers: |
| 61 | + headers["Content-Type"] = "application/json" |
| 62 | + response = requests.post(url, data=json_payload, headers=headers, timeout=60) |
53 | 63 | else: |
54 | | - response = requests.get(url, headers=cfapi_headers, timeout=60) |
| 64 | + response = requests.get(url, headers=headers, timeout=60) |
55 | 65 | return response |
56 | 66 |
|
57 | 67 |
|
@@ -200,12 +210,16 @@ def is_function_being_optimized_again(owner: str, repo: str, pr_number: int, cod |
200 | 210 | response = make_cfapi_request( |
201 | 211 | "/is-already-optimized", |
202 | 212 | "POST", |
203 | | - { |
204 | | - "owner": owner, |
205 | | - "repo": repo, |
206 | | - "pr_number": pr_number, |
207 | | - "code_contexts": code_contexts |
208 | | - } |
| 213 | + {"owner": owner, "repo": repo, "pr_number": pr_number, "code_contexts": code_contexts}, |
209 | 214 | ) |
210 | 215 | response.raise_for_status() |
211 | 216 | return response.json() |
| 217 | + |
| 218 | + |
| 219 | +def _json_dumps_fast(payload): |
| 220 | + # Try standard JSON serialization first |
| 221 | + try: |
| 222 | + return json.dumps(payload, indent=None) |
| 223 | + except (TypeError, ValueError): |
| 224 | + # Fallback to pydantic_encoder only if necessary |
| 225 | + return json.dumps(payload, indent=None, default=pydantic_encoder) |
0 commit comments