Skip to content

Commit 7d1c0ca

Browse files
feat(sdmx): Add SDMX client with custom endpoint and usage samples (#1601)
1 parent f6fe9c4 commit 7d1c0ca

9 files changed

Lines changed: 422 additions & 144 deletions

tools/sdmx/dataflow.py

Lines changed: 0 additions & 144 deletions
This file was deleted.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# SDMX Utility Sample Scripts
2+
3+
This directory contains sample scripts demonstrating how to use the SdmxClient in the `tools.sdmx_import.sdmx_client` module to download data and metadata from different SDMX APIs.
4+
5+
## Scripts
6+
7+
### OECD
8+
9+
* `fetch_oecd_gdp_metadata.py`: Downloads the complete metadata for the OECD's Quarterly GDP Growth dataset.
10+
* `fetch_oecd_gdp_data.py`: Fetches a specific slice of data from the same GDP dataset and saves it as a CSV.
11+
* `fetch_oecd_full_gdp_dataset.py`: A more complete example that combines both functions to download the metadata and then the full dataset.
12+
13+
### Eurostat
14+
15+
* `fetch_eurostat_gdp_metadata.py`: Downloads the metadata for the annual GDP dataset from Eurostat.
16+
* `fetch_eurostat_gdp_data.py`: Downloads a slice of the annual GDP data for Germany, France, and Italy from Eurostat.
17+
18+
## Running the Samples
19+
20+
You can execute each script from the root of the repository, for example:
21+
22+
```bash
23+
python3 tools/sdmx_import/samples/fetch_oecd_gdp_metadata.py
24+
```
25+
26+
The scripts will download the requested data/metadata and save it as `.xml` or `.csv` files in a new `output` directory inside the `samples` folder.
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
"""
2+
fetch_eurostat_gdp_data.py
3+
4+
This script provides a complete example of fetching a specific dataset
5+
from Eurostat using the reusable functions in the dataflow module.
6+
"""
7+
8+
import logging
9+
import sys
10+
import os
11+
12+
# Add the project root to the Python path
13+
sys.path.append(
14+
os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..', '..')))
15+
16+
from tools.sdmx_import.sdmx_client import SdmxClient
17+
18+
# Configure logging
19+
logging.basicConfig(level=logging.INFO,
20+
format='%(asctime)s - %(levelname)s - %(message)s')
21+
22+
23+
def main():
24+
"""Downloads a slice of the Eurostat GDP dataset."""
25+
# --- 1. Define Parameters for the Eurostat GDP Dataset ---
26+
agency_id = "ESTAT"
27+
dataflow_id = "TEC00001"
28+
endpoint = "https://ec.europa.eu/eurostat/api/dissemination/sdmx/2.1/"
29+
30+
# Create output directory inside the samples folder
31+
output_dir = os.path.join(os.path.dirname(__file__), "output")
32+
os.makedirs(output_dir, exist_ok=True)
33+
output_path = os.path.join(output_dir, "eurostat_gdp_data.csv")
34+
35+
# Key to select a slice of data
36+
data_key = {
37+
'freq': 'A',
38+
'na_item': 'B1GQ',
39+
'unit': 'CP_MEUR',
40+
'geo': 'DE+FR+IT'
41+
}
42+
# Parameters for the query
43+
data_params = {'startPeriod': '2020'}
44+
45+
logging.info(f"--- Fetching Eurostat Data: {dataflow_id} ---")
46+
47+
# --- 2. Use the SdmxClient ---
48+
client = SdmxClient(endpoint, agency_id)
49+
client.download_data_as_csv(dataflow_id=dataflow_id,
50+
key=data_key,
51+
params=data_params,
52+
output_path=output_path)
53+
logging.info(f"--- Successfully downloaded data to {output_path} ---")
54+
55+
56+
if __name__ == "__main__":
57+
main()
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
"""
2+
fetch_eurostat_gdp_metadata.py
3+
4+
This script provides a complete example of fetching metadata for a specific
5+
dataset from Eurostat using the reusable functions in the dataflow module.
6+
"""
7+
8+
import logging
9+
import sys
10+
import os
11+
12+
# Add the project root to the Python path
13+
sys.path.append(
14+
os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..', '..')))
15+
16+
from tools.sdmx_import.sdmx_client import SdmxClient
17+
18+
# Configure logging
19+
logging.basicConfig(level=logging.INFO,
20+
format='%(asctime)s - %(levelname)s - %(message)s')
21+
22+
23+
def main():
24+
"""Downloads the metadata for the Eurostat GDP dataset."""
25+
# --- 1. Define Parameters for the Eurostat GDP Dataset ---
26+
agency_id = "ESTAT"
27+
dataflow_id = "TEC00001"
28+
endpoint = "https://ec.europa.eu/eurostat/api/dissemination/sdmx/2.1/"
29+
30+
# Create output directory inside the samples folder
31+
output_dir = os.path.join(os.path.dirname(__file__), "output")
32+
os.makedirs(output_dir, exist_ok=True)
33+
output_path = os.path.join(output_dir, "eurostat_gdp_metadata.xml")
34+
35+
logging.info(f"--- Fetching Eurostat Metadata: {dataflow_id} ---")
36+
37+
# --- 2. Use the SdmxClient ---
38+
client = SdmxClient(endpoint, agency_id)
39+
client.download_metadata(dataflow_id=dataflow_id, output_path=output_path)
40+
logging.info(f"--- Successfully downloaded metadata to {output_path} ---")
41+
42+
43+
if __name__ == "__main__":
44+
main()
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
"""
2+
fetch_oecd_full_gdp_dataset.py
3+
4+
This script provides a complete example of fetching both the metadata and the
5+
full data series for the OECD's Quarterly GDP Growth dataset.
6+
"""
7+
8+
import logging
9+
import sys
10+
import os
11+
12+
# Add the project root to the Python path
13+
sys.path.append(
14+
os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..', '..')))
15+
16+
from tools.sdmx_import.sdmx_client import SdmxClient
17+
18+
# Configure logging
19+
logging.basicConfig(level=logging.INFO,
20+
format='%(asctime)s - %(levelname)s - %(message)s')
21+
22+
23+
def main():
24+
"""Downloads the full OECD Quarterly GDP Growth dataset and its metadata."""
25+
# --- 1. Define Common Parameters ---
26+
agency_id = "OECD.SDD.NAD"
27+
dataflow_id = "DSD_NAMAIN1@DF_QNA_EXPENDITURE_GROWTH_OECD"
28+
endpoint = "https://sdmx.oecd.org/public/rest/"
29+
30+
# Create output directory inside the samples folder
31+
output_dir = os.path.join(os.path.dirname(__file__), "output")
32+
os.makedirs(output_dir, exist_ok=True)
33+
metadata_output_path = os.path.join(output_dir,
34+
"oecd_gdp_full_metadata.xml")
35+
data_output_path = os.path.join(output_dir, "oecd_gdp_full_data.csv")
36+
37+
# --- 2. Initialize Client ---
38+
client = SdmxClient(endpoint, agency_id)
39+
40+
# --- 3. Fetch Metadata ---
41+
logging.info("--- Step 1: Starting Metadata Download ---")
42+
try:
43+
client.download_metadata(dataflow_id=dataflow_id,
44+
output_path=metadata_output_path)
45+
logging.info(
46+
f"--- Successfully downloaded metadata to {metadata_output_path} ---"
47+
)
48+
except Exception as e:
49+
logging.error(f"Failed to download metadata. Error: {e}")
50+
# Exit with a non-zero status code to indicate failure.
51+
sys.exit(1)
52+
53+
# --- 4. Fetch Full Data Series ---
54+
logging.info("\n--- Step 2: Starting Full Data Download ---")
55+
# For the full dataset, we use an empty key and no time parameters
56+
data_key = {}
57+
data_params = {}
58+
59+
try:
60+
client.download_data_as_csv(dataflow_id=dataflow_id,
61+
key=data_key,
62+
params=data_params,
63+
output_path=data_output_path)
64+
logging.info(
65+
f"--- Successfully downloaded data to {data_output_path} ---")
66+
except Exception as e:
67+
logging.error(f"Failed to download data. Error: {e}")
68+
sys.exit(1)
69+
70+
71+
if __name__ == "__main__":
72+
main()

0 commit comments

Comments
 (0)