Skip to content

feat(sdmx): Add SDMX client with custom endpoint and usage samples#1601

Merged
SandeepTuniki merged 13 commits into
masterfrom
feat/sdmx-custom-endpoints
Sep 9, 2025
Merged

feat(sdmx): Add SDMX client with custom endpoint and usage samples#1601
SandeepTuniki merged 13 commits into
masterfrom
feat/sdmx-custom-endpoints

Conversation

@SandeepTuniki

@SandeepTuniki SandeepTuniki commented Sep 4, 2025

Copy link
Copy Markdown
Contributor

(Note: This PR is an alternate option for #1600. Only one of these is necessary)

This pull request introduces a SdmxClient for interacting with SDMX APIs and adds a set of sample scripts to demonstrate its use.

This new approach also supports any custom SDMX REST endpoint and improves error handling.

Key Changes:

  1. SdmxClient Class (tools/sdmx_import/sdmx_client.py):

    • The previous standalone functions have been refactored into an SdmxClient class. This encapsulates the client creation logic, manages state, and provides a cleaner API.
    • The client is initialized with a specific endpoint and agency_id, making it capable of connecting to any SDMX-compliant source.
    • Client creation now handles potential source caching issues by creating a unique, overridable source for each agency/endpoint pair.
  2. Improved Error Handling:

    • Error log filenames are now sanitized to prevent crashes when dataflow_id contains special characters (e.g., @).
  3. Sample Scripts (tools/sdmx_import/samples/):

    • OECD Samples: Includes scripts to fetch metadata, a slice of data, and a full dataset.
    • Eurostat Samples: Includes scripts to fetch both metadata and a slice of data, demonstrating the multi-source capability.
    • README: A README.md has been added to the samples directory, explaining the purpose of each script and how to run them.

This commit adds a new SDMX tools module with utilities to fetch metadata and data from SDMX APIs. It also updates the requirements to include the sdmx1 package.
This commit refactors the SDMX utility functions to address review comments. It replaces print statements with logging, improves error handling by allowing exceptions to propagate, and adds type hints for better code clarity.
This commit addresses review comments by:
- Renaming `utils.py` to `dataflow.py`.
- Adding the request URL to the success logs.
- Providing more context in error logs.
- Generalizing a log message to be format-agnostic.
This commit adds a new `samples` directory to the `tools/sdmx` module. It includes scripts that demonstrate how to use the dataflow utilities to fetch data and metadata from both OECD and Eurostat, along with a README explaining how to run them.
This commit refactors the SDMX utilities to accept a REST endpoint URL instead of a `client_id`. This decouples the utilities from the `sdmx1` librarys internal source list, allowing them to be used with any SDMX-compliant API.

The sample scripts have been updated to reflect this new, more flexible approach. A new sample for fetching Eurostat metadata has also been added.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Summary of Changes

Hello @SandeepTuniki, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request significantly enhances the flexibility of the SDMX data utility by enabling connections to arbitrary SDMX REST endpoints, moving beyond hardcoded client IDs. This change broadens the applicability of the module, allowing users to interact with a wider range of data sources. Additionally, a comprehensive set of sample scripts has been introduced to guide users on how to effectively utilize these newly generalized functions for various data fetching scenarios.

Highlights

  • Custom SDMX Endpoints: The core SDMX utility functions, fetch_and_save_metadata and fetch_and_save_data_as_csv in tools/sdmx/dataflow.py, now accept a generic endpoint URL parameter instead of a hardcoded client_id. This refactoring allows the module to connect to any SDMX-compliant API by dynamically creating sdmx.Client sources.
  • New Sample Scripts: A new tools/sdmx/samples/ directory has been introduced, containing a set of example Python scripts. These scripts demonstrate how to fetch data and metadata from both OECD and Eurostat SDMX APIs, showcasing the flexibility of the newly generalized functions.
  • Improved Error Handling: The SDMX utility functions now include enhanced error handling. When an HTTPError occurs during data or metadata fetching, the raw HTTP response content is saved to a local HTML file (e.g., metadata_error_*.html or data_error_*.html) for easier debugging and inspection of the API's error message.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in issue comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request is a great enhancement, refactoring the SDMX utilities to support custom endpoints and adding helpful sample scripts. The code is well-structured and the changes align with the PR's goal. My review focuses on a few key areas:

  • A potential bug in how SDMX clients are cached, which could lead to using incorrect endpoints.
  • Improving the robustness of error handling by sanitizing filenames.
  • Best practices for the new sample scripts, including managing output files and handling exceptions correctly to ensure proper exit codes on failure.

Overall, these are solid changes and the feedback below should help make the utilities and samples even more robust and maintainable.

Comment thread tools/sdmx/dataflow.py Outdated
Comment thread tools/sdmx/dataflow.py Outdated
Comment thread tools/sdmx/dataflow.py Outdated
Comment thread tools/sdmx/dataflow.py Outdated
Comment thread tools/sdmx/samples/fetch_eurostat_gdp_data.py Outdated
Comment thread tools/sdmx/samples/fetch_eurostat_gdp_data.py Outdated
Comment thread tools/sdmx/samples/fetch_oecd_full_gdp_dataset.py Outdated
Comment thread tools/sdmx/samples/fetch_oecd_full_gdp_dataset.py
Comment thread tools/sdmx/dataflow.py Outdated
This commit addresses review comments by:
- Refactoring the client creation logic into a helper function to fix a caching bug and remove duplication.
- Sanitizing the `dataflow_id` for use in filenames.
- Modifying the sample scripts to write their output to a dedicated `output/` directory.
- Removing redundant `try...except` blocks from the sample scripts and ensuring non-zero exit codes on failure.
This commit addresses all outstanding review comments by:
- Refactoring the SDMX utilities into an `SdmxClient` class to improve design and remove duplication.
- Sanitizing the `dataflow_id` for use in filenames.
- Modifying the sample scripts to write their output to a dedicated `output/` directory within the samples folder.
- Removing redundant `try...except` blocks from the sample scripts and ensuring non-zero exit codes on failure.
@SandeepTuniki SandeepTuniki changed the title feat(sdmx): Support custom endpoints and add sample scripts feat(sdmx): Add SDMX client with custom endpoint and usage samples Sep 4, 2025
Comment thread tools/sdmx/dataflow.py Outdated
Comment thread tools/sdmx/dataflow.py
Comment thread tools/sdmx/dataflow.py Outdated
Comment thread tools/sdmx/dataflow.py Outdated
Comment thread tools/sdmx/dataflow.py Outdated
Comment thread tools/sdmx/dataflow.py Outdated
Comment thread tools/sdmx/samples/fetch_oecd_gdp_metadata.py
- Rename sdmx module to sdmx_import to avoid conflicts
- Rename dataflow.py to sdmx_client.py
- Rename methods in SdmxClient and update call sites
- Add more logging to download_data_as_csv
@SandeepTuniki SandeepTuniki enabled auto-merge (squash) September 9, 2025 09:59
@SandeepTuniki SandeepTuniki merged commit 7d1c0ca into master Sep 9, 2025
9 checks passed
@SandeepTuniki SandeepTuniki deleted the feat/sdmx-custom-endpoints branch September 9, 2025 10:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants