-
Notifications
You must be signed in to change notification settings - Fork 1
3 http client #8
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
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
ff0fee8
Error test not working
Moasib-Arif efab255
merged into main
Moasib-Arif fe17166
minor chnages
Moasib-Arif 9cc33fd
Working HTTP Client
Moasib-Arif 628194b
Minor changes
Moasib-Arif 2705d52
Added all the imports
Moasib-Arif 6aaad33
updated pycurl
Moasib-Arif 4b3b006
Minor changes
Moasib-Arif 977bd4f
removed import
Moasib-Arif bae9186
Comments addressed
Moasib-Arif bbcee5f
Http client uses requests library
Moasib-Arif 4fe4e83
Made the Requested Changes
Moasib-Arif File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| { | ||
| "python.testing.pytestArgs": [ | ||
| "tests" | ||
| ], | ||
| "python.testing.unittestEnabled": false, | ||
| "python.testing.pytestEnabled": true | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +0,0 @@ | ||
| from http_clients.http import HttpClient | ||
| from config.config import Config | ||
| from logger.logger import logger | ||
| from slack.slack import SlackNotifier | ||
| from sns.sns import Subscription, publish | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| import backoff | ||
| import requests | ||
| from requests.exceptions import HTTPError | ||
| import logging | ||
|
|
||
|
|
||
| # Function to log retry attempts | ||
| def log_retry(details): | ||
| logging.error(f"Request failed, retrying... Attempt #{details['tries']}") | ||
|
|
||
|
|
||
| class BaseHttpClient: | ||
| # Initialize HttpClient with a backoff_max value | ||
| def __init__(self, backoff_max=30): | ||
| self.backoff_max = backoff_max | ||
|
|
||
| # GET request method with exponential backoff | ||
| @backoff.on_exception( | ||
| backoff.expo, | ||
| HTTPError, | ||
| max_time=30, | ||
| on_backoff=log_retry | ||
| ) | ||
| def get(self, url, *args, **kwargs): | ||
| """ | ||
| Sends a GET request to the specified URL with optional extra arguments. | ||
|
|
||
| This method is a thin wrapper around `requests.get()`. Any additional arguments | ||
| are passed directly to `requests.get()`. For more information on the available | ||
| arguments, refer to the `requests.get()` documentation: | ||
| https://docs.python-requests.org/en/latest/api/#requests.get | ||
|
|
||
| Args: | ||
| url (str): The URL to send the GET request to. | ||
| *args: Optional positional arguments passed to `requests.get()`. | ||
| **kwargs: Optional keyword arguments passed to `requests.get()`. | ||
|
|
||
| Returns: | ||
| Response: The Response object from `requests.get()`. | ||
| Raises: | ||
| HTTPError: If the request fails for a network-related reason. | ||
| """ | ||
| return self._handle_request('GET', url, *args, **kwargs) | ||
|
|
||
| # POST request method with exponential backoff | ||
| @backoff.on_exception( | ||
| backoff.expo, | ||
| HTTPError, | ||
| max_time=30, | ||
| on_backoff=log_retry, | ||
| ) | ||
| def post(self, url, *args, **kwargs): | ||
Moasib-Arif marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| """ | ||
| Sends a POST request to the specified URL with optional extra arguments. | ||
|
|
||
| This method is a thin wrapper around `requests.post()`. Any additional arguments | ||
| are passed directly to `requests.post()`. For more information on the available | ||
| arguments, refer to the `requests.post()` documentation: | ||
| https://docs.python-requests.org/en/latest/api/#requests.post | ||
|
|
||
| Args: | ||
| url (str): The URL to send the POST request to. | ||
| *args: Optional positional arguments passed to `requests.post()`. | ||
| **kwargs: Optional keyword arguments passed to `requests.post()`. | ||
|
|
||
| Returns: | ||
| Response: The Response object from `requests.post()`. | ||
|
|
||
| Raises: | ||
| HTTPError: If the request fails for a network-related reason. | ||
| """ | ||
| return self._handle_request('POST', url, *args, **kwargs) | ||
|
|
||
| # Method to handle requests for GET and POST | ||
| def _handle_request(self, method, url, *args, **kwargs): | ||
| logging.info(f"Sending {method} request to {url}") | ||
| try: | ||
| response = requests.request(method, url, *args, **kwargs) | ||
| response.raise_for_status() | ||
| return response | ||
|
|
||
| except HTTPError as http_err: | ||
| logging.error(f"HTTP error occurred: {http_err} when sending a {method} to {url} with headers {kwargs.get('headers')}") | ||
| raise http_err | ||
| except Exception as err: | ||
| logging.error(f"Other error occurred: {err} when sending a {method} to {url} with headers {kwargs.get('headers')}") | ||
| raise err | ||
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.