Spaces:
Sleeping
Sleeping
Add apis package for travel and macroeconomic data
Browse files- apis/__init__.py +33 -0
- apis/econdb.py +67 -0
- apis/passport_index.py +104 -0
- apis/passport_visa.py +46 -0
- apis/statistics_of_the_world.py +55 -0
- apis/visadb.py +55 -0
- apis/world_bank.py +49 -0
apis/__init__.py
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# apis/__init__.py
|
| 2 |
+
from apis.econdb import list_datasets, list_series, list_sources
|
| 3 |
+
from apis.passport_index import (
|
| 4 |
+
get_visa_requirement as get_passport_index_requirement,
|
| 5 |
+
get_visa_requirement_matrix,
|
| 6 |
+
load_matrix_dataset,
|
| 7 |
+
load_tidy_dataset,
|
| 8 |
+
)
|
| 9 |
+
from apis.passport_visa import passport_stats, visa_requirement
|
| 10 |
+
from apis.statistics_of_the_world import (
|
| 11 |
+
get_history,
|
| 12 |
+
list_countries,
|
| 13 |
+
list_indicators,
|
| 14 |
+
)
|
| 15 |
+
from apis.visadb import get_questions
|
| 16 |
+
from apis.world_bank import get_indicator_series
|
| 17 |
+
|
| 18 |
+
__all__ = [
|
| 19 |
+
"get_indicator_series",
|
| 20 |
+
"get_history",
|
| 21 |
+
"list_countries",
|
| 22 |
+
"list_indicators",
|
| 23 |
+
"list_sources",
|
| 24 |
+
"list_datasets",
|
| 25 |
+
"list_series",
|
| 26 |
+
"visa_requirement",
|
| 27 |
+
"passport_stats",
|
| 28 |
+
"load_tidy_dataset",
|
| 29 |
+
"load_matrix_dataset",
|
| 30 |
+
"get_passport_index_requirement",
|
| 31 |
+
"get_visa_requirement_matrix",
|
| 32 |
+
"get_questions",
|
| 33 |
+
]
|
apis/econdb.py
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Econdb API client for macroeconomic time series discovery."""
|
| 2 |
+
|
| 3 |
+
from __future__ import annotations
|
| 4 |
+
|
| 5 |
+
import os
|
| 6 |
+
from typing import Any
|
| 7 |
+
|
| 8 |
+
import requests
|
| 9 |
+
|
| 10 |
+
BASE_URL = "https://www.econdb.com/api"
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
def _headers() -> dict[str, str]:
|
| 14 |
+
headers = {"Accept": "application/json"}
|
| 15 |
+
token = os.getenv("ECONDB_API_TOKEN")
|
| 16 |
+
if token:
|
| 17 |
+
headers["Authorization"] = f"Token {token}"
|
| 18 |
+
return headers
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
def _get_page(
|
| 22 |
+
path: str,
|
| 23 |
+
*,
|
| 24 |
+
page: int | None = None,
|
| 25 |
+
timeout: float = 30,
|
| 26 |
+
) -> dict[str, Any]:
|
| 27 |
+
url = f"{BASE_URL}/{path.strip('/')}/"
|
| 28 |
+
params = {"page": page} if page is not None else None
|
| 29 |
+
resp = requests.get(url, params=params, headers=_headers(), timeout=timeout)
|
| 30 |
+
resp.raise_for_status()
|
| 31 |
+
return resp.json()
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
def list_sources(
|
| 35 |
+
*,
|
| 36 |
+
page: int | None = None,
|
| 37 |
+
timeout: float = 30,
|
| 38 |
+
) -> list[dict[str, Any]]:
|
| 39 |
+
"""List data sources (first page by default)."""
|
| 40 |
+
return _get_page("sources", page=page, timeout=timeout).get("results", [])
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
def list_datasets(
|
| 44 |
+
*,
|
| 45 |
+
page: int | None = None,
|
| 46 |
+
timeout: float = 30,
|
| 47 |
+
) -> list[dict[str, Any]]:
|
| 48 |
+
"""List datasets (first page by default)."""
|
| 49 |
+
return _get_page("datasets", page=page, timeout=timeout).get("results", [])
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
def list_series(
|
| 53 |
+
*,
|
| 54 |
+
page: int | None = None,
|
| 55 |
+
timeout: float = 30,
|
| 56 |
+
) -> list[dict[str, Any]]:
|
| 57 |
+
"""
|
| 58 |
+
List series (first page by default).
|
| 59 |
+
|
| 60 |
+
Requires ``ECONDB_API_TOKEN``; the series endpoint is authenticated.
|
| 61 |
+
"""
|
| 62 |
+
token = os.getenv("ECONDB_API_TOKEN")
|
| 63 |
+
if not token:
|
| 64 |
+
raise ValueError(
|
| 65 |
+
"ECONDB_API_TOKEN environment variable is required for list_series()"
|
| 66 |
+
)
|
| 67 |
+
return _get_page("series", page=page, timeout=timeout).get("results", [])
|
apis/passport_index.py
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# apis/passport_index.py
|
| 2 |
+
"""Passport Index CSV dataset loader (ilyankou/passport-index-dataset)."""
|
| 3 |
+
|
| 4 |
+
from __future__ import annotations
|
| 5 |
+
|
| 6 |
+
import pandas as pd
|
| 7 |
+
|
| 8 |
+
TIDY_URL = (
|
| 9 |
+
"https://raw.githubusercontent.com/ilyankou/passport-index-dataset/"
|
| 10 |
+
"master/passport-index-tidy-iso2.csv"
|
| 11 |
+
)
|
| 12 |
+
MATRIX_URL = (
|
| 13 |
+
"https://raw.githubusercontent.com/ilyankou/passport-index-dataset/"
|
| 14 |
+
"master/passport-index-matrix-iso2.csv"
|
| 15 |
+
)
|
| 16 |
+
|
| 17 |
+
_tidy_cache: pd.DataFrame | None = None
|
| 18 |
+
_matrix_cache: pd.DataFrame | None = None
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
def load_tidy_dataset(*, use_cache: bool = True) -> pd.DataFrame:
|
| 22 |
+
"""
|
| 23 |
+
Load the tidy ISO-2 dataset (columns: passport, destination, requirement).
|
| 24 |
+
|
| 25 |
+
Args:
|
| 26 |
+
use_cache: Reuse the in-memory copy on subsequent calls.
|
| 27 |
+
"""
|
| 28 |
+
global _tidy_cache
|
| 29 |
+
if use_cache and _tidy_cache is not None:
|
| 30 |
+
return _tidy_cache
|
| 31 |
+
df = pd.read_csv(TIDY_URL)
|
| 32 |
+
df = df.rename(
|
| 33 |
+
columns={
|
| 34 |
+
"Passport": "passport",
|
| 35 |
+
"Destination": "destination",
|
| 36 |
+
"Requirement": "requirement",
|
| 37 |
+
}
|
| 38 |
+
)
|
| 39 |
+
if use_cache:
|
| 40 |
+
_tidy_cache = df
|
| 41 |
+
return df
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
def load_matrix_dataset(*, use_cache: bool = True) -> pd.DataFrame:
|
| 45 |
+
"""
|
| 46 |
+
Load the matrix ISO-2 dataset (passport rows, destination columns).
|
| 47 |
+
|
| 48 |
+
Args:
|
| 49 |
+
use_cache: Reuse the in-memory copy on subsequent calls.
|
| 50 |
+
"""
|
| 51 |
+
global _matrix_cache
|
| 52 |
+
if use_cache and _matrix_cache is not None:
|
| 53 |
+
return _matrix_cache
|
| 54 |
+
df = pd.read_csv(MATRIX_URL)
|
| 55 |
+
if "Passport" in df.columns:
|
| 56 |
+
df = df.rename(columns={"Passport": "passport"})
|
| 57 |
+
if "passport" not in df.columns and len(df.columns):
|
| 58 |
+
df = df.rename(columns={df.columns[0]: "passport"})
|
| 59 |
+
if use_cache:
|
| 60 |
+
_matrix_cache = df
|
| 61 |
+
return df
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
def get_visa_requirement(
|
| 65 |
+
passport_iso2: str,
|
| 66 |
+
destination_iso2: str,
|
| 67 |
+
*,
|
| 68 |
+
use_cache: bool = True,
|
| 69 |
+
) -> str | None:
|
| 70 |
+
"""
|
| 71 |
+
Look up visa requirement from the tidy dataset.
|
| 72 |
+
|
| 73 |
+
Returns:
|
| 74 |
+
Requirement code (e.g. ``visa free``, ``e-visa``) or ``None`` if not found.
|
| 75 |
+
"""
|
| 76 |
+
df = load_tidy_dataset(use_cache=use_cache)
|
| 77 |
+
match = df[
|
| 78 |
+
(df["passport"] == passport_iso2) & (df["destination"] == destination_iso2)
|
| 79 |
+
]
|
| 80 |
+
if match.empty:
|
| 81 |
+
return None
|
| 82 |
+
return str(match.iloc[0]["requirement"])
|
| 83 |
+
|
| 84 |
+
|
| 85 |
+
def get_visa_requirement_matrix(
|
| 86 |
+
passport_iso2: str,
|
| 87 |
+
destination_iso2: str,
|
| 88 |
+
*,
|
| 89 |
+
use_cache: bool = True,
|
| 90 |
+
) -> str | None:
|
| 91 |
+
"""
|
| 92 |
+
Look up visa requirement from the matrix dataset.
|
| 93 |
+
|
| 94 |
+
Returns:
|
| 95 |
+
Requirement code or ``None`` if the passport row is missing.
|
| 96 |
+
"""
|
| 97 |
+
mat = load_matrix_dataset(use_cache=use_cache)
|
| 98 |
+
rows = mat.loc[mat["passport"] == passport_iso2]
|
| 99 |
+
if rows.empty or destination_iso2 not in mat.columns:
|
| 100 |
+
return None
|
| 101 |
+
value = rows.iloc[0][destination_iso2]
|
| 102 |
+
if pd.isna(value):
|
| 103 |
+
return None
|
| 104 |
+
return str(value)
|
apis/passport_visa.py
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# apis/passport_visa.py
|
| 2 |
+
"""Passport Visa API client (community visa requirements service)."""
|
| 3 |
+
|
| 4 |
+
from __future__ import annotations
|
| 5 |
+
|
| 6 |
+
from typing import Any
|
| 7 |
+
|
| 8 |
+
import requests
|
| 9 |
+
|
| 10 |
+
BASE_URL = "https://rough-sun-2523.fly.dev"
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
def visa_requirement(
|
| 14 |
+
passport_iso2: str,
|
| 15 |
+
destination_iso2: str,
|
| 16 |
+
*,
|
| 17 |
+
timeout: float = 10,
|
| 18 |
+
) -> dict[str, Any]:
|
| 19 |
+
"""
|
| 20 |
+
Get visa requirement between two countries.
|
| 21 |
+
|
| 22 |
+
Args:
|
| 23 |
+
passport_iso2: Issuing passport country (ISO-2, e.g. ``HK``).
|
| 24 |
+
destination_iso2: Destination country (ISO-2, e.g. ``GB``).
|
| 25 |
+
"""
|
| 26 |
+
url = f"{BASE_URL}/visa/{passport_iso2}/{destination_iso2}"
|
| 27 |
+
resp = requests.get(url, timeout=timeout)
|
| 28 |
+
resp.raise_for_status()
|
| 29 |
+
return resp.json()
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
def passport_stats(
|
| 33 |
+
passport_iso2: str,
|
| 34 |
+
*,
|
| 35 |
+
timeout: float = 10,
|
| 36 |
+
) -> dict[str, Any]:
|
| 37 |
+
"""
|
| 38 |
+
Get aggregate visa stats for a passport (VR, VOA, EV, VF, NA counts).
|
| 39 |
+
|
| 40 |
+
Args:
|
| 41 |
+
passport_iso2: Passport country (ISO-2, e.g. ``HK``).
|
| 42 |
+
"""
|
| 43 |
+
url = f"{BASE_URL}/country/{passport_iso2}"
|
| 44 |
+
resp = requests.get(url, timeout=timeout)
|
| 45 |
+
resp.raise_for_status()
|
| 46 |
+
return resp.json()
|
apis/statistics_of_the_world.py
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# apis/statistics_of_the_world.py
|
| 2 |
+
"""Statistics of the World API client."""
|
| 3 |
+
|
| 4 |
+
from __future__ import annotations
|
| 5 |
+
|
| 6 |
+
import os
|
| 7 |
+
from typing import Any
|
| 8 |
+
|
| 9 |
+
import requests
|
| 10 |
+
|
| 11 |
+
BASE_URL = "https://statisticsoftheworld.com/api"
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
def _headers() -> dict[str, str]:
|
| 15 |
+
api_key = os.getenv("SOTW_API_KEY")
|
| 16 |
+
if api_key:
|
| 17 |
+
return {"X-API-Key": api_key}
|
| 18 |
+
return {}
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
def get_history(
|
| 22 |
+
indicator: str,
|
| 23 |
+
country: str,
|
| 24 |
+
*,
|
| 25 |
+
timeout: float = 30,
|
| 26 |
+
) -> list[dict[str, Any]]:
|
| 27 |
+
"""
|
| 28 |
+
Fetch historical data for an indicator/country pair.
|
| 29 |
+
|
| 30 |
+
Args:
|
| 31 |
+
indicator: Indicator code (e.g. ``IMF.NGDPD``).
|
| 32 |
+
country: ISO-3 country code (e.g. ``USA``).
|
| 33 |
+
|
| 34 |
+
Returns:
|
| 35 |
+
List of yearly observations from ``payload["data"]``.
|
| 36 |
+
"""
|
| 37 |
+
url = f"{BASE_URL}/v2/history"
|
| 38 |
+
params = {"indicator": indicator, "country": country}
|
| 39 |
+
resp = requests.get(url, params=params, headers=_headers(), timeout=timeout)
|
| 40 |
+
resp.raise_for_status()
|
| 41 |
+
return resp.json().get("data", [])
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
def list_countries(*, timeout: float = 30) -> list[dict[str, Any]]:
|
| 45 |
+
"""List all countries available in the API."""
|
| 46 |
+
resp = requests.get(f"{BASE_URL}/v1/countries", headers=_headers(), timeout=timeout)
|
| 47 |
+
resp.raise_for_status()
|
| 48 |
+
return resp.json().get("data", [])
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
def list_indicators(*, timeout: float = 30) -> list[dict[str, Any]]:
|
| 52 |
+
"""List all indicators available in the API."""
|
| 53 |
+
resp = requests.get(f"{BASE_URL}/v1/indicators", headers=_headers(), timeout=timeout)
|
| 54 |
+
resp.raise_for_status()
|
| 55 |
+
return resp.json().get("data", [])
|
apis/visadb.py
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# apis/visadb.py
|
| 2 |
+
"""VisaDB API client (paid visa & travel data)."""
|
| 3 |
+
|
| 4 |
+
from __future__ import annotations
|
| 5 |
+
|
| 6 |
+
import os
|
| 7 |
+
from typing import Any
|
| 8 |
+
|
| 9 |
+
import requests
|
| 10 |
+
|
| 11 |
+
QUESTION_BANK_URL = "https://question-bank-api.vercel.app"
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
def _auth_headers() -> dict[str, str]:
|
| 15 |
+
api_key = os.getenv("VISADB_API_KEY")
|
| 16 |
+
if not api_key:
|
| 17 |
+
raise ValueError(
|
| 18 |
+
"VISADB_API_KEY environment variable is required for VisaDB requests"
|
| 19 |
+
)
|
| 20 |
+
return {"Authorization": api_key}
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
def get_questions(*, timeout: float = 10) -> list[dict[str, Any]]:
|
| 24 |
+
"""Fetch questions from the VisaDB question bank."""
|
| 25 |
+
resp = requests.get(
|
| 26 |
+
f"{QUESTION_BANK_URL}/get/questions",
|
| 27 |
+
headers=_auth_headers(),
|
| 28 |
+
timeout=timeout,
|
| 29 |
+
)
|
| 30 |
+
resp.raise_for_status()
|
| 31 |
+
return resp.json()
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
def request(
|
| 35 |
+
base_url: str,
|
| 36 |
+
path: str,
|
| 37 |
+
*,
|
| 38 |
+
params: dict[str, str] | None = None,
|
| 39 |
+
timeout: float = 10,
|
| 40 |
+
) -> Any:
|
| 41 |
+
"""
|
| 42 |
+
Generic authenticated VisaDB request.
|
| 43 |
+
|
| 44 |
+
Use this for visa/entry/safety endpoints documented at visadb.io/api;
|
| 45 |
+
pass the appropriate ``base_url`` and ``path``.
|
| 46 |
+
"""
|
| 47 |
+
url = f"{base_url.rstrip('/')}/{path.lstrip('/')}"
|
| 48 |
+
resp = requests.get(
|
| 49 |
+
url,
|
| 50 |
+
params=params,
|
| 51 |
+
headers=_auth_headers(),
|
| 52 |
+
timeout=timeout,
|
| 53 |
+
)
|
| 54 |
+
resp.raise_for_status()
|
| 55 |
+
return resp.json()
|
apis/world_bank.py
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# apis/world_bank.py
|
| 2 |
+
"""World Bank API client for economic indicators."""
|
| 3 |
+
|
| 4 |
+
from __future__ import annotations
|
| 5 |
+
|
| 6 |
+
from typing import Any
|
| 7 |
+
|
| 8 |
+
import requests
|
| 9 |
+
|
| 10 |
+
BASE_URL = "http://api.worldbank.org/v2"
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
def get_indicator_series(
|
| 14 |
+
indicator: str,
|
| 15 |
+
country: str,
|
| 16 |
+
*,
|
| 17 |
+
per_page: int = 100,
|
| 18 |
+
mrv: int | None = None,
|
| 19 |
+
timeout: float = 30,
|
| 20 |
+
) -> list[dict[str, Any]]:
|
| 21 |
+
"""
|
| 22 |
+
Fetch a time series for a World Bank indicator and country.
|
| 23 |
+
|
| 24 |
+
Args:
|
| 25 |
+
indicator: World Bank indicator code (e.g. ``NY.GDP.MKTP.CD``).
|
| 26 |
+
country: ISO-3 country code (e.g. ``HKG``).
|
| 27 |
+
per_page: Max observations per request (increase for longer histories).
|
| 28 |
+
mrv: If set, return only the *m* most recent values.
|
| 29 |
+
timeout: Request timeout in seconds.
|
| 30 |
+
|
| 31 |
+
Returns:
|
| 32 |
+
List of observation dicts with ``date``, ``value``, ``country``, etc.
|
| 33 |
+
"""
|
| 34 |
+
url = f"{BASE_URL}/country/{country}/indicator/{indicator}"
|
| 35 |
+
params: dict[str, str | int] = {"format": "json", "per_page": per_page}
|
| 36 |
+
if mrv is not None:
|
| 37 |
+
params["mrv"] = mrv
|
| 38 |
+
|
| 39 |
+
resp = requests.get(url, params=params, timeout=timeout)
|
| 40 |
+
resp.raise_for_status()
|
| 41 |
+
data = resp.json()
|
| 42 |
+
|
| 43 |
+
if not isinstance(data, list) or len(data) < 2:
|
| 44 |
+
return []
|
| 45 |
+
|
| 46 |
+
observations = data[1]
|
| 47 |
+
if observations is None:
|
| 48 |
+
return []
|
| 49 |
+
return observations
|