-
Notifications
You must be signed in to change notification settings - Fork 21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refactor: Simplify token refresh logic #62
Conversation
WalkthroughThe latest updates in Changes
Sequence Diagram(s)sequenceDiagram
participant Client
participant SomeClass
Client->>+SomeClass: request()
SomeClass->>SomeClass: Handle request
SomeClass->>SomeClass: handle_http_error(response)
SomeClass-->>-Client: Return response
Poem
Tip AI model upgrade
|
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #62 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 34 34
Lines 1267 1269 +2
=========================================
+ Hits 1267 1269 +2
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. |
@coderabbitai review |
Actions performedReview triggered.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 2
Outside diff range comments (1)
garth/http.py (1)
Line range hint
168-173
: Add type annotations to theconnectapi
method.The method is missing type annotations for
self
,method
, and**kwargs
.- def connectapi(self, path: str, method="GET", **kwargs): + def connectapi(self, path: str, method: str = "GET", **kwargs: Any) -> Any:Tools
Ruff
164-164: Missing return type annotation for public function
refresh_oauth2
Add return type annotation:
None
(ANN201)
164-164: Missing type annotation for
self
in method(ANN101)
165-165: Use of
assert
detected(S101)
168-168: Missing return type annotation for public function
connectapi
(ANN201)
168-168: Missing type annotation for
self
in method(ANN101)
168-168: Missing type annotation for function argument
method
(ANN001)
168-168: Missing type annotation for
**kwargs
(ANN003)
Review details
Configuration used: CodeRabbit UI
Review profile: ASSERTIVE
Files selected for processing (1)
- garth/http.py (1 hunks)
Additional context used
Ruff
garth/http.py
164-164: Missing return type annotation for public function
refresh_oauth2
Add return type annotation:
None
(ANN201)
164-164: Missing type annotation for
self
in method(ANN101)
165-165: Use of
assert
detected(S101)
168-168: Missing return type annotation for public function
connectapi
(ANN201)
168-168: Missing type annotation for
self
in method(ANN101)
168-168: Missing type annotation for function argument
method
(ANN001)
168-168: Missing type annotation for
**kwargs
(ANN003)
@@ -163,8 +163,6 @@ | |||
|
|||
def refresh_oauth2(self): | |||
assert self.oauth1_token |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Avoid using assert statements in production code.
Using assert
statements for runtime checks is not recommended in production code as they can be disabled with the -O
flag. Consider raising an exception instead.
- assert self.oauth1_token
+ if not self.oauth1_token:
+ raise ValueError("OAuth1 token is required to refresh OAuth2 token.")
Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
assert self.oauth1_token | |
if not self.oauth1_token: | |
raise ValueError("OAuth1 token is required to refresh OAuth2 token.") |
Tools
Ruff
165-165: Use of
assert
detected(S101)
@coderabbitai review |
Actions performedReview triggered.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 2
Review details
Configuration used: CodeRabbit UI
Review profile: ASSERTIVE
Files selected for processing (1)
- garth/http.py (2 hunks)
Additional context used
Ruff
garth/http.py
141-141: Missing return type annotation for public function
handle_http_error
Add return type annotation:
None
(ANN201)
141-141: Missing type annotation for
self
in method(ANN101)
145-148: Within an
except
clause, raise exceptions withraise ... from err
orraise ... from None
to distinguish them from errors in exception handling(B904)
150-150: Missing type annotation for
self
in method(ANN101)
150-150: Missing type annotation for
*args
(ANN002)
150-150: Missing type annotation for
**kwargs
(ANN003)
167-167: Missing return type annotation for public function
refresh_oauth2
Add return type annotation:
None
(ANN201)
167-167: Missing type annotation for
self
in method(ANN101)
168-168: Use of
assert
detected(S101)
171-171: Missing return type annotation for public function
connectapi
(ANN201)
171-171: Missing type annotation for
self
in method(ANN101)
171-171: Missing type annotation for function argument
method
(ANN001)
171-171: Missing type annotation for
**kwargs
(ANN003)
Additional comments not posted (1)
garth/http.py (1)
138-139
: Verify the new call tohandle_http_error
.Ensure that the new call to
handle_http_error
correctly handles all HTTP errors before returning the response.Verification successful
The new call to
handle_http_error
correctly handles HTTP errors before returning the response.
- The
handle_http_error
function attempts to raise the status of the response and catchesHTTPError
, raising a customGarthHTTPError
with a message and the original error.Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash # Description: Verify the integration of the new call to `handle_http_error`. # Test: Search for the function usage. Expect: Proper error handling. rg --type python $'handle_http_error'Length of output: 67
Script:
#!/bin/bash # Description: Verify the integration of the new call to `handle_http_error`. # Corrected Test: Search for the function usage in Python files. Expect: Proper error handling. rg --glob '*.py' 'handle_http_error'Length of output: 164
Script:
#!/bin/bash # Description: Extract the implementation of `handle_http_error` from `garth/http.py`. # Extract the function definition and its body ast-grep --lang python --pattern $'def handle_http_error($_, $_):\n $$$'Length of output: 485
def refresh_oauth2(self): | ||
assert self.oauth1_token |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add type annotations and replace assert
statement.
Add type annotations for self
and the return type. Replace the assert
statement with a proper exception.
- def refresh_oauth2(self):
+ def refresh_oauth2(self) -> None:
- assert self.oauth1_token
+ if not self.oauth1_token:
+ raise ValueError("OAuth1 token is required to refresh OAuth2 token.")
self.oauth2_token = sso.exchange(self.oauth1_token, self)
Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
def refresh_oauth2(self): | |
assert self.oauth1_token | |
def refresh_oauth2(self) -> None: | |
if not self.oauth1_token: | |
raise ValueError("OAuth1 token is required to refresh OAuth2 token.") | |
self.oauth2_token = sso.exchange(self.oauth1_token, self) |
Tools
Ruff
167-167: Missing return type annotation for public function
refresh_oauth2
Add return type annotation:
None
(ANN201)
167-167: Missing type annotation for
self
in method(ANN101)
168-168: Use of
assert
detected(S101)
def handle_http_error(self, response: Response): | ||
try: | ||
self.last_resp.raise_for_status() | ||
response.raise_for_status() | ||
except HTTPError as e: | ||
raise GarthHTTPError( | ||
msg="Error in request", | ||
error=e, | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add missing type annotations and improve error handling.
Add type annotations for self
and the return type. Improve error handling by using raise ... from err
to distinguish from errors in exception handling.
- def handle_http_error(self, response: Response):
+ def handle_http_error(self, response: Response) -> None:
try:
response.raise_for_status()
except HTTPError as e:
- raise GarthHTTPError(
+ raise GarthHTTPError from e (
msg="Error in request",
error=e,
)
Committable suggestion was skipped due to low confidence.
Tools
Ruff
141-141: Missing return type annotation for public function
handle_http_error
Add return type annotation:
None
(ANN201)
141-141: Missing type annotation for
self
in method(ANN101)
145-148: Within an
except
clause, raise exceptions withraise ... from err
orraise ... from None
to distinguish them from errors in exception handling(B904)
Not a useful change. |
Refactor: Simplify token refresh logic
Description
This pull request refactors the
refresh_oauth2
method in theClient
class to simplify the token refresh logic. The refactoring removes an unnecessary comment and retains the essential logic to refresh the OAuth2 token using thesso.exchange
function.Changes
refresh_oauth2
method by removing a comment and retaining the essential logic to refresh the OAuth2 token using thesso.exchange
function.request
method by creating a new method namedhandle_http_error
to encapsulate the error handling logic, improving readability and maintainability.Testing
make test
and passed successfully, confirming that the refactoring did not introduce any issues and that the functionality remains intact.Link to Devin run
https://preview.devin.ai/devin/d4b452b678ab41a6bac61141f15414f7
Requested by
Matin
Summary by CodeRabbit