Skip to content

Commit

Permalink
[Feature] Add Top Export Destinations By Country From UN Comtrade Int…
Browse files Browse the repository at this point in the history
…ernational Trade Statistics Database (EconDB) (OpenBB-finance#6644)

* add economy.export_destinations

* mypy

* elif

* unused argument

---------

Co-authored-by: Igor Radovanovic <74266147+IgorWounds@users.noreply.github.com>
  • Loading branch information
deeleeramone and IgorWounds authored Sep 5, 2024
1 parent 4311c68 commit de8b73e
Show file tree
Hide file tree
Showing 11 changed files with 571 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
"""Export Destinations Standard Model."""

from typing import Union

from pydantic import Field

from openbb_core.provider.abstract.data import Data
from openbb_core.provider.abstract.query_params import QueryParams
from openbb_core.provider.utils.descriptions import QUERY_DESCRIPTIONS


class ExportDestinationsQueryParams(QueryParams):
"""Export Destinations Query."""

country: str = Field(description=QUERY_DESCRIPTIONS.get("country", ""))


class ExportDestinationsData(Data):
"""Export Destinations Data."""

origin_country: str = Field(
description="The country of origin.",
)
destination_country: str = Field(
description="The destination country.",
)
value: Union[float, int] = Field(
description="The value of the export.",
json_schema_extra={"x-unit_measurement": "currency"},
)
23 changes: 23 additions & 0 deletions openbb_platform/extensions/economy/integration/test_economy_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -1107,3 +1107,26 @@ def test_economy_survey_bls_series(params, headers):
result = requests.get(url, headers=headers, timeout=10)
assert isinstance(result, requests.Response)
assert result.status_code == 200


@parametrize(
"params",
[
(
{
"provider": "econdb",
"country": "IN,CN",
}
),
],
)
@pytest.mark.integration
def test_economy_export_destinations(params, headers):
"""Test the economy export destinations endpoint."""
params = {p: v for p, v in params.items() if v}

query_str = get_querystring(params, [])
url = f"http://0.0.0.0:8000/api/v1/economy/export_destinations?{query_str}"
result = requests.get(url, headers=headers, timeout=10)
assert isinstance(result, requests.Response)
assert result.status_code == 200
Original file line number Diff line number Diff line change
Expand Up @@ -1065,3 +1065,25 @@ def test_economy_survey_bls_series(params, obb):
assert result
assert isinstance(result, OBBject)
assert len(result.results) > 0


@parametrize(
"params",
[
(
{
"provider": "econdb",
"country": "IN,CN",
}
),
],
)
@pytest.mark.integration
def test_economy_export_destinations(params, obb):
"""Test the economy export destinations endpoint"""
params = {p: v for p, v in params.items() if v}

result = obb.economy.export_destinations(**params)
assert result
assert isinstance(result, OBBject)
assert len(result.results) > 0
Original file line number Diff line number Diff line change
Expand Up @@ -647,3 +647,19 @@ async def pce(
) -> OBBject:
"""Get Personal Consumption Expenditures (PCE) reports."""
return await OBBject.from_query(Query(**locals()))


@router.command(
model="ExportDestinations",
examples=[
APIEx(parameters={"provider": "econdb", "country": "us"}),
],
)
async def export_destinations(
cc: CommandContext,
provider_choices: ProviderChoices,
standard_params: StandardParams,
extra_params: ExtraParams,
) -> OBBject:
"""Get top export destinations by country from the UN Comtrade International Trade Statistics Database."""
return await OBBject.from_query(Query(**locals()))
105 changes: 105 additions & 0 deletions openbb_platform/openbb/assets/reference.json
Original file line number Diff line number Diff line change
Expand Up @@ -8849,6 +8849,111 @@
},
"model": "PersonalConsumptionExpenditures"
},
"/economy/export_destinations": {
"deprecated": {
"flag": null,
"message": null
},
"description": "Get top export destinations by country from the UN Comtrade International Trade Statistics Database.",
"examples": "\nExamples\n--------\n\n```python\nfrom openbb import obb\nobb.economy.export_destinations(provider='econdb', country='us')\n```\n\n",
"parameters": {
"standard": [
{
"name": "country",
"type": "Union[str, List[str]]",
"description": "The country to get data. Multiple items allowed for provider(s): econdb.",
"default": "",
"optional": false,
"choices": null
}
],
"econdb": []
},
"returns": {
"OBBject": [
{
"name": "results",
"type": "List[ExportDestinations]",
"description": "Serializable results."
},
{
"name": "provider",
"type": "Optional[Literal['econdb']]",
"description": "Provider name."
},
{
"name": "warnings",
"type": "Optional[List[Warning_]]",
"description": "List of warnings."
},
{
"name": "chart",
"type": "Optional[Chart]",
"description": "Chart object."
},
{
"name": "extra",
"type": "Dict[str, Any]",
"description": "Extra info."
}
]
},
"data": {
"standard": [
{
"name": "origin_country",
"type": "str",
"description": "The country of origin.",
"default": "",
"optional": false,
"choices": null
},
{
"name": "destination_country",
"type": "str",
"description": "The destination country.",
"default": "",
"optional": false,
"choices": null
},
{
"name": "value",
"type": "Union[float, int]",
"description": "The value of the export.",
"default": "",
"optional": false,
"choices": null
}
],
"econdb": [
{
"name": "units",
"type": "str",
"description": "The units of measurement for the value.",
"default": "",
"optional": false,
"choices": null
},
{
"name": "title",
"type": "str",
"description": "The title of the data.",
"default": "",
"optional": false,
"choices": null
},
{
"name": "footnote",
"type": "str",
"description": "The footnote for the data.",
"default": "",
"optional": false,
"choices": null
}
]
},
"model": "ExportDestinations"
},
"/equity/calendar/ipo": {
"deprecated": {
"flag": null,
Expand Down
85 changes: 85 additions & 0 deletions openbb_platform/openbb/package/economy.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class ROUTER_economy(Container):
composite_leading_indicator
country_profile
cpi
export_destinations
fred_regional
fred_release_table
fred_search
Expand Down Expand Up @@ -1185,6 +1186,90 @@ def cpi(
)
)

@exception_handler
@validate
def export_destinations(
self,
country: Annotated[
Union[str, List[str]],
OpenBBField(
description="The country to get data. Multiple comma separated items allowed for provider(s): econdb."
),
],
provider: Annotated[
Optional[Literal["econdb"]],
OpenBBField(
description="The provider to use, by default None. If None, the priority list configured in the settings is used. Default priority: econdb."
),
] = None,
**kwargs
) -> OBBject:
"""Get top export destinations by country from the UN Comtrade International Trade Statistics Database.
Parameters
----------
country : Union[str, List[str]]
The country to get data. Multiple comma separated items allowed for provider(s): econdb.
provider : Optional[Literal['econdb']]
The provider to use, by default None. If None, the priority list configured in the settings is used. Default priority: econdb.
Returns
-------
OBBject
results : List[ExportDestinations]
Serializable results.
provider : Optional[Literal['econdb']]
Provider name.
warnings : Optional[List[Warning_]]
List of warnings.
chart : Optional[Chart]
Chart object.
extra : Dict[str, Any]
Extra info.
ExportDestinations
------------------
origin_country : str
The country of origin.
destination_country : str
The destination country.
value : Union[float, int]
The value of the export.
units : Optional[str]
The units of measurement for the value. (provider: econdb)
title : Optional[str]
The title of the data. (provider: econdb)
footnote : Optional[str]
The footnote for the data. (provider: econdb)
Examples
--------
>>> from openbb import obb
>>> obb.economy.export_destinations(provider='econdb', country='us')
""" # noqa: E501

return self._run(
"/economy/export_destinations",
**filter_inputs(
provider_choices={
"provider": self._get_provider(
provider,
"economy.export_destinations",
("econdb",),
)
},
standard_params={
"country": country,
},
extra_params=kwargs,
info={
"country": {
"econdb": {"multiple_items_allowed": True, "choices": None}
}
},
)
)

@exception_handler
@validate
def fred_regional(
Expand Down
2 changes: 2 additions & 0 deletions openbb_platform/providers/econdb/openbb_econdb/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from openbb_econdb.models.available_indicators import EconDbAvailableIndicatorsFetcher
from openbb_econdb.models.country_profile import EconDbCountryProfileFetcher
from openbb_econdb.models.economic_indicators import EconDbEconomicIndicatorsFetcher
from openbb_econdb.models.export_destinations import EconDbExportDestinationsFetcher
from openbb_econdb.models.gdp_nominal import EconDbGdpNominalFetcher
from openbb_econdb.models.gdp_real import EconDbGdpRealFetcher
from openbb_econdb.models.yield_curve import EconDbYieldCurveFetcher
Expand All @@ -24,6 +25,7 @@
"AvailableIndicators": EconDbAvailableIndicatorsFetcher,
"CountryProfile": EconDbCountryProfileFetcher,
"EconomicIndicators": EconDbEconomicIndicatorsFetcher,
"ExportDestinations": EconDbExportDestinationsFetcher,
"GdpNominal": EconDbGdpNominalFetcher,
"GdpReal": EconDbGdpRealFetcher,
"YieldCurve": EconDbYieldCurveFetcher,
Expand Down
Loading

0 comments on commit de8b73e

Please sign in to comment.