-
Notifications
You must be signed in to change notification settings - Fork 147
feat(sdmx): Add SDMX client with custom endpoint and usage samples #1601
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
Merged
Merged
Changes from 7 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
c429941
feat(sdmx): Add new SDMX tools module and utilities
SandeepTuniki 8d4c63c
fix(sdmx): Address review comments for SDMX utils
SandeepTuniki 3da4cc6
fix(sdmx): Address review comments from rohitkumarbhagat
SandeepTuniki 96bd900
feat(sdmx): Add sample scripts for OECD and Eurostat
SandeepTuniki dcf9a4d
Merge remote-tracking branch 'origin/master' into feat/sdmx-samples
SandeepTuniki f3b3b4c
chore: run linter on code samples
SandeepTuniki 96e8658
feat(sdmx): Use custom REST endpoints instead of client IDs
SandeepTuniki 5f900b0
fix(sdmx): Address review comments
SandeepTuniki 7294374
fix(sdmx): Address all review comments
SandeepTuniki 2204989
refactor: address review comments
SandeepTuniki 4a3edb9
refactor: use tofile for direct download
SandeepTuniki bae0ba9
docs: update samples README
SandeepTuniki 3b90272
Merge branch 'master' into feat/sdmx-custom-endpoints
SandeepTuniki File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| # SDMX Utility Sample Scripts | ||
|
|
||
| This directory contains sample scripts demonstrating how to use the functions in the `tools.sdmx.dataflow` module to download data and metadata from different SDMX APIs. | ||
|
|
||
| ## Scripts | ||
|
|
||
| ### OECD | ||
|
|
||
| * `fetch_oecd_gdp_metadata.py`: Downloads the complete metadata for the OECD's Quarterly GDP Growth dataset. | ||
| * `fetch_oecd_gdp_data.py`: Fetches a specific slice of data from the same GDP dataset and saves it as a CSV. | ||
| * `fetch_oecd_full_gdp_dataset.py`: A more complete example that combines both functions to download the metadata and then the full dataset. | ||
|
|
||
| ### Eurostat | ||
|
|
||
| * `fetch_eurostat_gdp_metadata.py`: Downloads the metadata for the annual GDP dataset from Eurostat. | ||
| * `fetch_eurostat_gdp_data.py`: Downloads a slice of the annual GDP data for Germany, France, and Italy from Eurostat. | ||
|
|
||
| ## Running the Samples | ||
|
|
||
| You can execute each script from the root of the repository, for example: | ||
|
|
||
| ```bash | ||
| python3 tools/sdmx/samples/fetch_oecd_gdp_metadata.py | ||
| ``` | ||
|
|
||
| The scripts will download the requested data/metadata and save it as `.xml` or `.csv` files in the project's root directory. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| """ | ||
| fetch_eurostat_gdp_data.py | ||
|
|
||
| This script provides a complete example of fetching a specific dataset | ||
| from Eurostat using the reusable functions in the dataflow module. | ||
| """ | ||
|
|
||
| import logging | ||
| import sys | ||
| import os | ||
|
|
||
| # Add the project root to the Python path | ||
| sys.path.append( | ||
| os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..', '..'))) | ||
|
|
||
| from tools.sdmx import dataflow | ||
|
|
||
| # Configure logging | ||
| logging.basicConfig(level=logging.INFO, | ||
| format='%(asctime)s - %(levelname)s - %(message)s') | ||
|
|
||
|
|
||
| def main(): | ||
| """Downloads a slice of the Eurostat GDP dataset.""" | ||
| # --- 1. Define Parameters for the Eurostat GDP Dataset --- | ||
| agency_id = "ESTAT" | ||
| dataflow_id = "TEC00001" | ||
| output_path = "eurostat_gdp_data.csv" | ||
|
SandeepTuniki marked this conversation as resolved.
Outdated
|
||
| endpoint = "https://ec.europa.eu/eurostat/api/dissemination/sdmx/2.1/" | ||
|
|
||
| # Key to select a slice of data | ||
| data_key = { | ||
| 'freq': 'A', | ||
| 'na_item': 'B1GQ', | ||
| 'unit': 'CP_MEUR', | ||
| 'geo': 'DE+FR+IT' | ||
| } | ||
| # Parameters for the query | ||
| data_params = {'startPeriod': '2020'} | ||
|
|
||
| logging.info(f"--- Fetching Eurostat Data: {dataflow_id} ---") | ||
|
|
||
| # --- 2. Use the Reusable Function --- | ||
| try: | ||
| dataflow.fetch_and_save_data_as_csv(dataflow_id=dataflow_id, | ||
| agency_id=agency_id, | ||
| key=data_key, | ||
| params=data_params, | ||
| output_path=output_path, | ||
| endpoint=endpoint) | ||
| logging.info(f"--- Successfully downloaded data to {output_path} ---") | ||
| except Exception as e: | ||
| logging.error(f"Failed to download data. Error: {e}") | ||
|
SandeepTuniki marked this conversation as resolved.
Outdated
|
||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| """ | ||
| fetch_eurostat_gdp_metadata.py | ||
|
|
||
| This script provides a complete example of fetching metadata for a specific | ||
| dataset from Eurostat using the reusable functions in the dataflow module. | ||
| """ | ||
|
|
||
| import logging | ||
| import sys | ||
| import os | ||
|
|
||
| # Add the project root to the Python path | ||
| sys.path.append( | ||
| os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..', '..'))) | ||
|
|
||
| from tools.sdmx import dataflow | ||
|
|
||
| # Configure logging | ||
| logging.basicConfig(level=logging.INFO, | ||
| format='%(asctime)s - %(levelname)s - %(message)s') | ||
|
|
||
|
|
||
| def main(): | ||
| """Downloads the metadata for the Eurostat GDP dataset.""" | ||
| # --- 1. Define Parameters for the Eurostat GDP Dataset --- | ||
| agency_id = "ESTAT" | ||
| dataflow_id = "TEC00001" | ||
| output_path = "eurostat_gdp_metadata.xml" | ||
| endpoint = "https://ec.europa.eu/eurostat/api/dissemination/sdmx/2.1/" | ||
|
|
||
| logging.info(f"--- Fetching Eurostat Metadata: {dataflow_id} ---") | ||
|
|
||
| # --- 2. Use the Reusable Function --- | ||
| try: | ||
| dataflow.fetch_and_save_metadata(dataflow_id=dataflow_id, | ||
| agency_id=agency_id, | ||
| output_path=output_path, | ||
| endpoint=endpoint) | ||
| logging.info( | ||
| f"--- Successfully downloaded metadata to {output_path} ---") | ||
| except Exception as e: | ||
| logging.error(f"Failed to download metadata. Error: {e}") | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| """ | ||
| fetch_oecd_full_gdp_dataset.py | ||
|
|
||
| This script provides a complete example of fetching both the metadata and the | ||
| full data series for the OECD's Quarterly GDP Growth dataset. | ||
| """ | ||
|
|
||
| import logging | ||
| import sys | ||
| import os | ||
|
|
||
| # Add the project root to the Python path | ||
| sys.path.append( | ||
| os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..', '..'))) | ||
|
|
||
| from tools.sdmx import dataflow | ||
|
|
||
| # Configure logging | ||
| logging.basicConfig(level=logging.INFO, | ||
| format='%(asctime)s - %(levelname)s - %(message)s') | ||
|
|
||
|
|
||
| def main(): | ||
| """Downloads the full OECD Quarterly GDP Growth dataset and its metadata.""" | ||
| # --- 1. Define Common Parameters --- | ||
| agency_id = "OECD.SDD.NAD" | ||
| dataflow_id = "DSD_NAMAIN1@DF_QNA_EXPENDITURE_GROWTH_OECD" | ||
| metadata_output_path = "oecd_gdp_full_metadata.xml" | ||
| data_output_path = "oecd_gdp_full_data.csv" | ||
| endpoint = "https://sdmx.oecd.org/public/rest/" | ||
|
|
||
| # --- 2. Fetch Metadata --- | ||
| logging.info("--- Step 1: Starting Metadata Download ---") | ||
| try: | ||
| dataflow.fetch_and_save_metadata(dataflow_id=dataflow_id, | ||
| agency_id=agency_id, | ||
| output_path=metadata_output_path, | ||
| endpoint=endpoint) | ||
| logging.info( | ||
| f"--- Successfully downloaded metadata to {metadata_output_path} ---" | ||
| ) | ||
| except Exception as e: | ||
| logging.error(f"Failed to download metadata. Error: {e}") | ||
| # Exit if metadata fails, as it's needed for context | ||
| return | ||
|
SandeepTuniki marked this conversation as resolved.
Outdated
|
||
|
|
||
| # --- 3. Fetch Full Data Series --- | ||
| logging.info("\n--- Step 2: Starting Full Data Download ---") | ||
| # For the full dataset, we use an empty key and no time parameters | ||
| data_key = {} | ||
| data_params = {} | ||
|
|
||
| try: | ||
| dataflow.fetch_and_save_data_as_csv(dataflow_id=dataflow_id, | ||
| agency_id=agency_id, | ||
| key=data_key, | ||
| params=data_params, | ||
| output_path=data_output_path, | ||
| endpoint=endpoint) | ||
| logging.info( | ||
| f"--- Successfully downloaded data to {data_output_path} ---") | ||
| except Exception as e: | ||
| logging.error(f"Failed to download data. Error: {e}") | ||
|
SandeepTuniki marked this conversation as resolved.
|
||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.