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

[Feature] Add SSL Certificate Context And Setting For Async/Sync HTTP Requests #6976

Open
wants to merge 57 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
57 commits
Select commit Hold shift + click to select a range
6634dd0
add SSL certificate context and setting
deeleeramone Nov 29, 2024
ea65641
codespell
deeleeramone Nov 29, 2024
1a0a679
linting
deeleeramone Nov 29, 2024
1d36365
cleanup section
deeleeramone Nov 30, 2024
f6159ab
allow defining uvicorn.run kwargs in system_settings
deeleeramone Dec 1, 2024
44da668
Merge branch 'develop' into feature/ssl-context
deeleeramone Dec 1, 2024
a38770a
Merge branch 'develop' into feature/ssl-context
deeleeramone Dec 3, 2024
dec94bc
refactor to return session objects
deeleeramone Dec 5, 2024
46362fa
hub service rename it as _request_session
deeleeramone Dec 5, 2024
43b753a
typing
deeleeramone Dec 5, 2024
1754813
more linting
deeleeramone Dec 5, 2024
2297169
pylint
deeleeramone Dec 5, 2024
cc46123
missing __init__ files
deeleeramone Dec 5, 2024
1a9e2fe
fix test_hub_service
deeleeramone Dec 5, 2024
8dad0f9
missing init files..?
deeleeramone Dec 5, 2024
b759d90
shouldn't need an init file there..
deeleeramone Dec 5, 2024
09404c2
now for some random linting
deeleeramone Dec 5, 2024
4645282
maybe need that __init__ after all...?
deeleeramone Dec 5, 2024
1e061d5
fix test_make_request
deeleeramone Dec 5, 2024
dbcd973
relative import in conftest..?
deeleeramone Dec 5, 2024
ef4704a
issue can't be from missing init files..
deeleeramone Dec 5, 2024
dcd81ad
revert the inits
deeleeramone Dec 5, 2024
61ff2f3
small cleanup
deeleeramone Dec 5, 2024
6d87de5
pass the kwargs to the async session object on init for use outside o…
deeleeramone Dec 5, 2024
dfa5c2c
Merge branch 'develop' into feature/ssl-context
deeleeramone Dec 5, 2024
e64ec0d
try something...
deeleeramone Dec 5, 2024
de000a5
try something else..
deeleeramone Dec 5, 2024
08c5dac
turn on verbosity for errors..
deeleeramone Dec 5, 2024
ea49615
try something else...
deeleeramone Dec 5, 2024
e62d2f5
undo test change
deeleeramone Dec 5, 2024
714df64
Update lock files
piiq Dec 5, 2024
be3446a
undo another test check thing
deeleeramone Dec 5, 2024
2a0b354
Merge remote-tracking branch 'upstream/feature/ssl-context' into feat…
piiq Dec 5, 2024
cfa1b59
Revert "Update lock files"
deeleeramone Dec 5, 2024
60f6768
try with just the platform lock
deeleeramone Dec 5, 2024
c77af38
unexpected argument
deeleeramone Dec 5, 2024
cb43eed
do a dummy check to return the session object if it is passed in the …
deeleeramone Dec 6, 2024
91e5c64
also dummy check for TCPConnector instance
deeleeramone Dec 6, 2024
c690661
bit more cleanup
deeleeramone Dec 6, 2024
228518a
no implementation of server_hostname
deeleeramone Dec 6, 2024
50d737b
Merge branch 'develop' into feature/ssl-context
deeleeramone Dec 7, 2024
808d93b
Move setting session to PosthogHandler init
deeleeramone Dec 7, 2024
4f9c026
Merge branch 'develop' into feature/ssl-context
deeleeramone Dec 7, 2024
5a65612
Merge branch 'develop' into feature/ssl-context
deeleeramone Dec 11, 2024
1d50c89
Merge branch 'develop' into feature/ssl-context
deeleeramone Dec 13, 2024
a8da6dd
Merge branch 'develop' into feature/ssl-context
deeleeramone Dec 17, 2024
54c1deb
Merge branch 'develop' into feature/ssl-context
deeleeramone Jan 3, 2025
d548cb7
Merge branch 'develop' into feature/ssl-context
deeleeramone Jan 8, 2025
5edc443
Merge branch 'develop' into feature/ssl-context
deeleeramone Jan 8, 2025
0597722
Merge branch 'develop' of https://github.com/OpenBB-finance/OpenBB in…
deeleeramone Jan 8, 2025
86ce65f
add comments
deeleeramone Jan 8, 2025
1a693a4
Merge branch 'feature/ssl-context' of https://github.com/OpenBB-finan…
deeleeramone Jan 8, 2025
01ab7bb
review items
deeleeramone Jan 8, 2025
d7698c1
Merge branch 'develop' into feature/ssl-context
deeleeramone Jan 10, 2025
060fe9e
Merge branch 'develop' into feature/ssl-context
deeleeramone Jan 10, 2025
e88e956
merge branch develop
deeleeramone Jan 14, 2025
3289b31
update PythonSettings model definitions and annotations
deeleeramone Jan 15, 2025
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
13 changes: 12 additions & 1 deletion openbb_platform/core/openbb_core/api/rest_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,17 @@ async def lifespan(_: FastAPI):


if __name__ == "__main__":
# pylint: disable=import-outside-toplevel
import uvicorn

uvicorn.run("openbb_core.api.rest_api:app", reload=True)
# This initializes the OpenBB environment variables so they can be read before uvicorn is run.
Env()
deeleeramone marked this conversation as resolved.
Show resolved Hide resolved
uvicorn_kwargs = system.python_settings.model_dump().get("uvicorn", {})
uvicorn_reload = uvicorn_kwargs.pop("reload", None)

if uvicorn_reload is None or uvicorn_reload:
uvicorn_kwargs["reload"] = True

uvicorn_app = uvicorn_kwargs.pop("app", "openbb_core.api.rest_api:app")

uvicorn.run(uvicorn_app, **uvicorn_kwargs)
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,17 @@ class AllowedEvents(Enum):

def __init__(self, settings: LoggingSettings):
"""Initialize Posthog Handler."""
# pylint: disable=import-outside-toplevel
from openbb_core.provider.utils.helpers import get_requests_session

super().__init__()
self._settings = settings
self.logged_in = False
posthog.api_key = "phc_6FXLqu4uW9yxfyN8DpPdgzCdlYXOmIWdMGh6GnBgJLX" # pragma: allowlist secret # noqa
posthog.host = "https://app.posthog.com" # noqa
posthog.request._session = ( # pylint: disable=protected-access
get_requests_session()
)

@property
def settings(self) -> LoggingSettings:
Expand Down
53 changes: 52 additions & 1 deletion openbb_platform/core/openbb_core/app/model/python_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,70 @@

from typing import List, Optional

from pydantic import BaseModel, Field, PositiveInt
from pydantic import BaseModel, ConfigDict, Field, PositiveInt


class PythonSettings(BaseModel):
"""Settings model for Python interface configuration."""

model_config = ConfigDict(extra="allow")

docstring_sections: List[str] = Field(
default_factory=lambda: ["description", "parameters", "returns", "examples"],
description="Sections to include in autogenerated docstrings.",
)
docstring_max_length: Optional[PositiveInt] = Field(
default=None, description="Maximum length of autogenerated docstrings."
)
http: Optional[dict] = Field(
default_factory=dict,
description="HTTP settings covers all requests made by the internal, utility, functions."
+ " The configuration applies to both the requests and aiohttp libraries."
+ "\n "
+ """Available settings:
- cafile: str - Path to a CA certificate file.
- certfile: str - Path to a client certificate file.
- keyfile: str - Path to a client key file.
- password: str - Password for the client key file. # aiohttp only
- verify_ssl: bool - Verify SSL certificates.
- fingerprint: str - SSL fingerprint. # aiohttp only
- proxy: str - Proxy URL.
- proxy_auth: str | list - Proxy authentication. # aiohttp only
- proxy_headers: dict - Proxy headers. # aiohttp only
- timeout: int - Request timeout.
- auth: str | list - Basic authentication.
- headers: dict - Request headers.
- cookies: dict - Dictionary of session cookies.

Any additional keys supplied will be ignored unless explicitly implemented via custom code.

The settings are passed into the `requests.Session` object and the `aiohttp.ClientSession` object by:
- `openbb_core.provider.utils.helpers.make_request` - Sync
- `openbb_core.provider.utils.helpers.amake_request` - Async
- `openbb_core.provider.utils.helpers.amake_requests` - Async (multiple requests)
- Inserted to use with YFinance & Finviz library implementations.

Return a session object with the settings applied by:
- `openbb_core.provider.utils.helpers.get_requests_session`
- `openbb_core.provider.utils.helpers.get_async_requests_session`
""",
)
uvicorn: Optional[dict] = Field(
default_factory=dict,
description="Uvicorn settings, covers all the launch of FastAPI when using the following entry points:"
+ "\n "
+ """
- Running the FastAPI as a Python module script.
- python -m openbb_core.api.rest_api
- Running the `openbb-api` command.
- openbb-api

All settings are passed directly to `uvicorn.run`, and can be found in the Uvicorn documentation.
- https://www.uvicorn.org/settings/

Keyword arguments supplied to the command line will take priority over the settings in this configuration.
""",
)

def __repr__(self) -> str:
"""Return a string representation of the model."""
Expand Down
19 changes: 10 additions & 9 deletions openbb_platform/core/openbb_core/app/service/hub_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
from openbb_core.app.model.profile import Profile
from openbb_core.app.model.user_settings import UserSettings
from openbb_core.env import Env
from requests import get, post, put


class HubService:
Expand All @@ -39,9 +38,13 @@ def __init__(
base_url: Optional[str] = None,
):
"""Initialize Hub service."""
# pylint: disable=import-outside-toplevel
from openbb_core.provider.utils.helpers import get_requests_session

self._base_url = base_url or Env().HUB_BACKEND
self._session = session
self._hub_user_settings: Optional[HubUserSettings] = None
self._request_session = get_requests_session()

@property
def base_url(self) -> str:
Expand Down Expand Up @@ -112,7 +115,7 @@ def _get_session_from_email_password(self, email: str, password: str) -> HubSess
if not password:
raise OpenBBError("Password not found.")

response = post(
response = self._request_session.post(
url=self._base_url + "/login",
json={
"email": email,
Expand Down Expand Up @@ -144,7 +147,7 @@ def _get_session_from_platform_token(self, token: str) -> HubSession:

self._check_token_expiration(token)

response = post(
response = self._request_session.post(
url=self._base_url + "/sdk/login",
json={
"token": token,
Expand Down Expand Up @@ -173,7 +176,7 @@ def _post_logout(self, session: HubSession) -> bool:
token_type = session.token_type
authorization = f"{token_type.title()} {access_token}"

response = get(
response = self._request_session.get(
url=self._base_url + "/logout",
headers={"Authorization": authorization},
json={"token": access_token},
Expand All @@ -193,12 +196,12 @@ def _get_user_settings(self, session: HubSession) -> HubUserSettings:
access_token = session.access_token.get_secret_value()
token_type = session.token_type
authorization = f"{token_type.title()} {access_token}"

response = get(
response = self._request_session.get(
url=self._base_url + "/terminal/user",
headers={"Authorization": authorization},
timeout=self.TIMEOUT,
)

if response.status_code == 200:
user_settings = response.json()
filtered = {k: v for k, v in user_settings.items() if v is not None}
Expand All @@ -214,14 +217,12 @@ def _put_user_settings(
access_token = session.access_token.get_secret_value()
token_type = session.token_type
authorization = f"{token_type.title()} {access_token}"

response = put(
response = self._request_session.put(
url=self._base_url + "/user",
headers={"Authorization": authorization},
json=settings.model_dump(exclude_defaults=True),
timeout=self.TIMEOUT,
)

if response.status_code == 200:
return True
status_code = response.status_code
Expand Down
2 changes: 1 addition & 1 deletion openbb_platform/core/openbb_core/provider/utils/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,4 @@ def __init__(
msg = msg.replace("<provider name>", provider_name)
message = msg
self.message = message
super().__init__(self.message)
super().__init__(str(self.message))
Loading
Loading