Skip to content

Commit 7cb9ab2

Browse files
fix: Update type signatures to allow None tokens and improve error handling
- Allow None tokens in Agent, Organizations, Users, and CodegenSDK classes - Update handle_api_error to accept None status codes from ApiException - Add proper None handling in exception status code comparisons - Maintain backward compatibility while improving type safety - All tests passing and type checking issues resolved
1 parent 7b76103 commit 7cb9ab2

File tree

5 files changed

+9
-7
lines changed

5 files changed

+9
-7
lines changed

src/codegen/agents/agent.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ def to_dict(self) -> dict[str, Any]:
7575
class Agent:
7676
"""API client for interacting with Codegen AI agents."""
7777

78-
def __init__(self, token: str | None, org_id: int | None = None, base_url: str | None = CODEGEN_BASE_API_URL):
78+
def __init__(self, token: str | None, org_id: int | str | None = None, base_url: str | None = CODEGEN_BASE_API_URL):
7979
"""Initialize a new Agent client.
8080
8181
Args:

src/codegen/exceptions.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,11 @@ class APIError(CodegenError):
5555
pass
5656

5757

58-
def handle_api_error(status_code: int, message: str, response_data: dict[str, Any] | None = None) -> CodegenError:
58+
def handle_api_error(status_code: int | None, message: str, response_data: dict[str, Any] | None = None) -> CodegenError:
5959
"""Convert HTTP status codes to appropriate exception types."""
60-
if status_code == 401:
60+
if status_code is None:
61+
return APIError(message, status_code, response_data)
62+
elif status_code == 401:
6163
return AuthenticationError(message, status_code, response_data)
6264
elif status_code == 403:
6365
return AuthorizationError(message, status_code, response_data)
@@ -67,7 +69,7 @@ def handle_api_error(status_code: int, message: str, response_data: dict[str, An
6769
return ValidationError(message, status_code, response_data)
6870
elif status_code == 429:
6971
return RateLimitError(message, status_code, response_data)
70-
elif 500 <= status_code < 600:
72+
elif status_code is not None and 500 <= status_code < 600:
7173
return ServerError(message, status_code, response_data)
7274
else:
7375
return APIError(message, status_code, response_data)

src/codegen/organizations/organizations.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ def __repr__(self) -> str:
4545
class Organizations:
4646
"""API client for managing organizations in Codegen."""
4747

48-
def __init__(self, token: str, base_url: str = CODEGEN_BASE_API_URL):
48+
def __init__(self, token: str | None, base_url: str = CODEGEN_BASE_API_URL):
4949
"""Initialize the Organizations client.
5050
5151
Args:

src/codegen/sdk.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class CodegenSDK:
3232
```
3333
"""
3434

35-
def __init__(self, token: str, org_id: int | str | None = None, base_url: str = CODEGEN_BASE_API_URL):
35+
def __init__(self, token: str | None, org_id: int | str | None = None, base_url: str = CODEGEN_BASE_API_URL):
3636
"""Initialize the Codegen SDK.
3737
3838
Args:

src/codegen/users/users.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ def __repr__(self) -> str:
6060
class Users:
6161
"""API client for managing users in Codegen."""
6262

63-
def __init__(self, token: str, org_id: int | str, base_url: str = CODEGEN_BASE_API_URL):
63+
def __init__(self, token: str | None, org_id: int | str, base_url: str = CODEGEN_BASE_API_URL):
6464
"""Initialize the Users client.
6565
6666
Args:

0 commit comments

Comments
 (0)