Skip to content
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

Fixed the request function #5336

Merged
merged 6 commits into from
Aug 17, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 16 additions & 18 deletions openbb_terminal/helper_funcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -2078,16 +2078,19 @@ def check_start_less_than_end(start_date: str, end_date: str) -> bool:

# Write an abstract helper to make requests from a url with potential headers and params
def request(
url: str, method: str = "GET", timeout: int = 0, **kwargs
url: str, method: str = "get", timeout: int = 0, **kwargs
) -> requests.Response:
"""Abstract helper to make requests from a url with potential headers and params.

Parameters
----------
url : str
Url to make the request to
method : str, optional
HTTP method to use. Can be "GET" or "POST", by default "GET"
method : str
HTTP method to use. Choose from:
delete, get, head, patch, post, put, by default "get"
timeout : int
How many seconds to wait for the server to send data

Returns
-------
Expand All @@ -2099,6 +2102,9 @@ def request(
ValueError
If invalid method is passed
"""
method = method.lower()
if method not in ["delete", "get", "head", "patch", "post", "put"]:
raise ValueError(f"Invalid method: {method}")
current_user = get_current_user()
# We want to add a user agent to the request, so check if there are any headers
# If there are headers, check if there is a user agent, if not add one.
Expand All @@ -2108,21 +2114,13 @@ def request(

if "User-Agent" not in headers:
headers["User-Agent"] = get_user_agent()
if method.upper() == "GET":
return requests.get(
url,
headers=headers,
timeout=timeout,
**kwargs,
)
if method.upper() == "POST":
return requests.post(
url,
headers=headers,
timeout=timeout,
**kwargs,
)
raise ValueError("Method must be GET or POST")
func = getattr(requests, method)
return func(
url,
headers=headers,
timeout=timeout,
**kwargs,
)


def remove_timezone_from_dataframe(df: pd.DataFrame) -> pd.DataFrame:
Expand Down