From 8e445fead096767d69471795c0d98e2a90c26be2 Mon Sep 17 00:00:00 2001 From: Pratyush Shukla Date: Thu, 8 Feb 2024 04:02:12 +0530 Subject: [PATCH] [Enhancement] - Improve `crypto` router docs and examples (#6048) * crypto search add validation for '-'-separated pairs * set default query value to None * improve /crypto description with custom examples * improve /crypto/price/historical description with custom examples * updated static for crypto menu * ignore line too long * linting --------- Co-authored-by: Igor Radovanovic <74266147+IgorWounds@users.noreply.github.com> --- .../crypto/openbb_crypto/crypto_router.py | 7 +++++-- .../openbb_crypto/price/price_router.py | 14 ++++++++++--- openbb_platform/openbb/package/crypto.py | 4 +++- .../openbb/package/crypto_price.py | 6 +++++- .../fmp/openbb_fmp/models/crypto_search.py | 20 +++++++++++++++---- 5 files changed, 40 insertions(+), 11 deletions(-) diff --git a/openbb_platform/extensions/crypto/openbb_crypto/crypto_router.py b/openbb_platform/extensions/crypto/openbb_crypto/crypto_router.py index 0bee75ebdfb..e4f6d24ed31 100644 --- a/openbb_platform/extensions/crypto/openbb_crypto/crypto_router.py +++ b/openbb_platform/extensions/crypto/openbb_crypto/crypto_router.py @@ -17,12 +17,15 @@ router.include_router(price_router) # pylint: disable=unused-argument -@router.command(model="CryptoSearch") +@router.command( + model="CryptoSearch", + examples=['obb.crypto.search("BTCUSD")', 'obb.crypto.search("ETH-USD")'], +) async def search( cc: CommandContext, provider_choices: ProviderChoices, standard_params: StandardParams, extra_params: ExtraParams, ) -> OBBject: - """Cryptocurrency Search. Search available cryptocurrency pairs.""" + """Search available cryptocurrency pairs within a provider.""" return await OBBject.from_query(Query(**locals())) diff --git a/openbb_platform/extensions/crypto/openbb_crypto/price/price_router.py b/openbb_platform/extensions/crypto/openbb_crypto/price/price_router.py index 0c172cb033a..6c95097c075 100644 --- a/openbb_platform/extensions/crypto/openbb_crypto/price/price_router.py +++ b/openbb_platform/extensions/crypto/openbb_crypto/price/price_router.py @@ -14,13 +14,21 @@ from openbb_core.app.router import Router router = Router(prefix="/price") -# pylint: disable=unused-argument -@router.command(model="CryptoHistorical") +# pylint: disable=unused-argument,line-too-long +@router.command( + model="CryptoHistorical", + examples=[ + 'obb.crypto.price.historical("BTCUSD", start_date="2024-01-01", end_date="2024-01-31")', + 'obb.crypto.price.historical("ETH-USD", provider="yfinance", interval="1mo", start_date="2024-01-01", end_date="2024-12-31")', # noqa: E501 + 'obb.crypto.price.historical("BTCUSD,ETH-USD", provider="yfinance", interval="1d", start_date="2024-01-01", end_date="2024-01-31")', # noqa: E501 + 'obb.crypto.price.historical(["BTCUSD", "ETH-USD"], start_date="2024-01-01", end_date="2024-01-31")', + ], +) async def historical( cc: CommandContext, provider_choices: ProviderChoices, standard_params: StandardParams, extra_params: ExtraParams, ) -> OBBject: - """Cryptocurrency Historical Price. Cryptocurrency historical price data.""" + """Get historical price data for cryptocurrency pair(s) within a provider.""" return await OBBject.from_query(Query(**locals())) diff --git a/openbb_platform/openbb/package/crypto.py b/openbb_platform/openbb/package/crypto.py index bf09bd7fda7..7bb5228c299 100644 --- a/openbb_platform/openbb/package/crypto.py +++ b/openbb_platform/openbb/package/crypto.py @@ -35,7 +35,7 @@ class ROUTER_crypto(Container): provider: Optional[Literal["fmp"]] = None, **kwargs ) -> OBBject: - """Cryptocurrency Search. Search available cryptocurrency pairs. + """Search available cryptocurrency pairs within a provider. Parameters ---------- @@ -77,6 +77,8 @@ class ROUTER_crypto(Container): ------- >>> from openbb import obb >>> obb.crypto.search() + >>> obb.crypto.search("BTCUSD") + >>> obb.crypto.search("ETH-USD") """ # noqa: E501 return self._run( diff --git a/openbb_platform/openbb/package/crypto_price.py b/openbb_platform/openbb/package/crypto_price.py index 06c9790dcca..42ae9ca3dba 100644 --- a/openbb_platform/openbb/package/crypto_price.py +++ b/openbb_platform/openbb/package/crypto_price.py @@ -43,7 +43,7 @@ class ROUTER_crypto_price(Container): provider: Optional[Literal["fmp", "polygon", "tiingo", "yfinance"]] = None, **kwargs ) -> OBBject: - """Cryptocurrency Historical Price. Cryptocurrency historical price data. + """Get historical price data for cryptocurrency pair(s) within a provider. Parameters ---------- @@ -121,6 +121,10 @@ class ROUTER_crypto_price(Container): ------- >>> from openbb import obb >>> obb.crypto.price.historical(symbol="BTCUSD") + >>> obb.crypto.price.historical("BTCUSD", start_date="2024-01-01", end_date="2024-01-31") + >>> obb.crypto.price.historical("ETH-USD", provider="yfinance", interval="1mo", start_date="2024-01-01", end_date="2024-12-31") + >>> obb.crypto.price.historical("BTCUSD,ETH-USD", provider="yfinance", interval="1d", start_date="2024-01-01", end_date="2024-01-31") + >>> obb.crypto.price.historical(["BTCUSD", "ETH-USD"], start_date="2024-01-01", end_date="2024-01-31") """ # noqa: E501 return self._run( diff --git a/openbb_platform/providers/fmp/openbb_fmp/models/crypto_search.py b/openbb_platform/providers/fmp/openbb_fmp/models/crypto_search.py index 2e278ab6d7d..38a53ee45b3 100644 --- a/openbb_platform/providers/fmp/openbb_fmp/models/crypto_search.py +++ b/openbb_platform/providers/fmp/openbb_fmp/models/crypto_search.py @@ -9,11 +9,21 @@ from openbb_core.provider.standard_models.crypto_search import ( CryptoSearchQueryParams, ) from openbb_fmp.utils.helpers import create_url, get_data_many -from pydantic import Field +from pydantic import Field, field_validator class FMPCryptoSearchQueryParams(CryptoSearchQueryParams): - """FMP Crypto Search Query.""" + """FMP Crypto Search Query. + + Source: https://site.financialmodelingprep.com/developer/docs/cryptocurrency-historical-data-api + """ + + @field_validator("query", mode="after", check_fields=False) + def validate_query(cls, v: str) -> str: # pylint: disable=no-self-argument + """Return the query.""" + if isinstance(v, str): + return v.replace("-", "") if "-" in v else v + return None class FMPCryptoSearchData(CryptoSearchData): @@ -49,7 +59,7 @@ class FMPCryptoSearchFetcher( @staticmethod async def aextract_data( - query: FMPCryptoSearchQueryParams, + query: FMPCryptoSearchQueryParams, # pylint: disable=unused-argument credentials: Optional[Dict[str, str]], **kwargs: Any, ) -> List[Dict]: @@ -66,7 +76,9 @@ class FMPCryptoSearchFetcher( @staticmethod def transform_data( - query: FMPCryptoSearchQueryParams, data: List[Dict], **kwargs: Any + query: FMPCryptoSearchQueryParams, # pylint: disable=unused-argument + data: List[Dict], + **kwargs: Any, ) -> List[FMPCryptoSearchData]: """Return the transformed data.""" cryptos = pd.DataFrame(data)