Skip to content

fix OpenAPI Context #524

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

Closed
wants to merge 12 commits into from
Closed

fix OpenAPI Context #524

wants to merge 12 commits into from

Conversation

Rasic2
Copy link

@Rasic2 Rasic2 commented Jul 20, 2025

Summary by CodeRabbit

  • New Features

    • Added the ability to zip and unzip files and directories directly within the application.
  • Chores

    • Ensured necessary directories are automatically created if missing.
    • Added new dependencies: httpx, distro, and pyhumps.

Copy link
Contributor

coderabbitai bot commented Jul 20, 2025

📝 Walkthrough

Walkthrough

The changes ensure the existence of a server home directory at module load in openapi_context.py and introduce two utility functions, unzip_file and zip_file_list, at the module level, replacing their previous duplicated definitions. Additionally, three new dependencies (httpx, distro, pyhumps) are added to pyproject.toml.

Changes

Cohort / File(s) Change Summary
Module-level utility functions & directory setup
dpdispatcher/contexts/openapi_context.py
Ensures server home directory exists at module load; adds unzip_file and zip_file_list as module-level utilities, removing duplicated definitions.
Dependency additions
pyproject.toml
Adds httpx, distro, and pyhumps to the dependencies list.

Sequence Diagram(s)

sequenceDiagram
    participant Module as openapi_context.py (module load)
    participant OS as os module
    participant Utils as unzip_file / zip_file_list

    Module->>OS: os.makedirs(DP_CLOUD_SERVER_HOME_DIR, exist_ok=True)
    Note right of Module: Ensures directory exists at import
    Module->>Utils: Defines unzip_file and zip_file_list functions
    Note right of Utils: Functions available for later use
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~7 minutes

Possibly related PRs

  • update bohrium-sdk version 0.6.0 #520: Adds the same unzip_file and zip_file_list functions at the module level in dpdispatcher/contexts/openapi_context.py and modifies their usage, indicating a direct code-level relationship.

Suggested reviewers

  • njzjz

Note

⚡️ Unit Test Generation is now available in beta!

Learn more here, or try it out under "Finishing Touches" below.


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between de7afb9 and d36d14a.

📒 Files selected for processing (1)
  • dpdispatcher/contexts/openapi_context.py (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • dpdispatcher/contexts/openapi_context.py
✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai generate unit tests to generate unit tests for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 6

🧹 Nitpick comments (1)
dpdispatcher/contexts/openapi_context.py (1)

76-101: Consider refactoring credential retrieval logic to reduce duplication.

The credential retrieval and validation logic is duplicated between openapi.py and openapi_context.py.

Consider creating a utility function to handle credential retrieval:

# In a shared utility module
def get_bohrium_credentials(remote_profile):
    """Retrieve Bohrium credentials from profile or environment."""
    access_key = (
        remote_profile.get("access_key", None)
        or os.getenv("BOHRIUM_ACCESS_KEY", None)
        or os.getenv("ACCESS_KEY", None)
    )
    project_id = (
        remote_profile.get("project_id", None)
        or os.getenv("BOHRIUM_PROJECT_ID", None)
        or os.getenv("PROJECT_ID", None)
    )
    app_key = (
        remote_profile.get("app_key", None)
        or os.getenv("BOHRIUM_APP_KEY", None)
        or os.getenv("APP_KEY", None)
    )
    
    if access_key is None:
        raise ValueError(
            "remote_profile must contain 'access_key' or set environment variable 'BOHRIUM_ACCESS_KEY'"
        )
    if project_id is None:
        raise ValueError(
            "remote_profile must contain 'project_id' or set environment variable 'BOHRIUM_PROJECT_ID'"
        )
    
    return access_key, project_id, app_key
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between d81c9f6 and fa5bc44.

📒 Files selected for processing (3)
  • dpdispatcher/contexts/openapi_context.py (5 hunks)
  • dpdispatcher/machines/openapi.py (4 hunks)
  • pyproject.toml (1 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (1)
dpdispatcher/machines/openapi.py (5)
dpdispatcher/utils/utils.py (1)
  • customized_script_header_template (202-207)
dpdispatcher/machines/dp_cloud_server.py (1)
  • Bohrium (19-299)
dpdispatcher/submission.py (1)
  • Job (725-939)
dpdispatcher/contexts/openapi_context.py (1)
  • unzip_file (29-32)
dpdispatcher/utils/dpcloudserver/zip_file.py (1)
  • unzip_file (80-83)
🔇 Additional comments (1)
dpdispatcher/machines/openapi.py (1)

44-70: Well-implemented credential handling with proper validation.

The explicit credential retrieval with multiple fallback sources and validation is a good security practice. The error messages are clear and helpful.

Comment on lines 23 to +26
DP_CLOUD_SERVER_HOME_DIR = os.path.join(
os.path.expanduser("~"), ".dpdispatcher/", "dp_cloud_server/"
)
os.makedirs(DP_CLOUD_SERVER_HOME_DIR, exist_ok=True)
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Avoid creating directories at module import time.

Creating directories during module import can cause issues in restricted environments. Consider creating the directory lazily when first needed.

Move the directory creation to methods that actually use it, or create a helper function:

def ensure_home_dir_exists():
    os.makedirs(DP_CLOUD_SERVER_HOME_DIR, exist_ok=True)

Then call this function in methods like write_home_file and read_home_file.

🤖 Prompt for AI Agents
In dpdispatcher/contexts/openapi_context.py around lines 23 to 26, the directory
is created at module import time, which can cause issues in restricted
environments. Remove the os.makedirs call from the module level and instead
create a helper function that ensures the directory exists. Call this helper
function inside methods that use the directory, such as write_home_file and
read_home_file, to create the directory lazily when first needed.

Comment on lines 29 to 32
def unzip_file(zip_file, out_dir="./"):
obj = ZipFile(zip_file, "r")
for item in obj.namelist():
obj.extract(item, out_dir)
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Eliminate code duplication by creating a shared utility module.

The unzip_file function is duplicated in both openapi.py and openapi_context.py. This violates the DRY principle.

Create a shared utility module (e.g., dpdispatcher/utils/zip_utils.py) and move both unzip_file and zip_file_list functions there:

# dpdispatcher/utils/zip_utils.py
from zipfile import ZipFile
import glob
import os
from dpdispatcher.dlog import dlog

def unzip_file(zip_file, out_dir="./"):
    """Extract zip file to specified directory."""
    try:
        with ZipFile(zip_file, "r") as obj:
            for item in obj.namelist():
                obj.extract(item, out_dir)
    except Exception as e:
        dlog.error(f"Failed to unzip file {zip_file}: {str(e)}")
        raise

def zip_file_list(root_path, zip_filename, file_list=[]):
    """Create zip file from list of files."""
    # ... existing implementation with error handling ...

Then import and use these functions in both files.

🤖 Prompt for AI Agents
In dpdispatcher/contexts/openapi_context.py around lines 29 to 32, the
unzip_file function duplicates code found in openapi.py, violating the DRY
principle. To fix this, create a new shared utility module at
dpdispatcher/utils/zip_utils.py, move both unzip_file and zip_file_list
functions there with proper error handling and logging, then update both
openapi_context.py and openapi.py to import and use these functions from the new
utility module.

Comment on lines 35 to 56
def zip_file_list(root_path, zip_filename, file_list=[]):
out_zip_file = os.path.join(root_path, zip_filename)
# print('debug: file_list', file_list)
zip_obj = ZipFile(out_zip_file, "w")
for f in file_list:
matched_files = os.path.join(root_path, f)
for ii in glob.glob(matched_files):
# print('debug: matched_files:ii', ii)
if os.path.isdir(ii):
arcname = os.path.relpath(ii, start=root_path)
zip_obj.write(ii, arcname)
for root, dirs, files in os.walk(ii):
for file in files:
filename = os.path.join(root, file)
arcname = os.path.relpath(filename, start=root_path)
# print('debug: filename:arcname:root_path', filename, arcname, root_path)
zip_obj.write(filename, arcname)
else:
arcname = os.path.relpath(ii, start=root_path)
zip_obj.write(ii, arcname)
zip_obj.close()
return out_zip_file
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue

Fix mutable default argument and add error handling.

The function has several issues that need addressing:

  1. Mutable default argument can cause unexpected behavior
  2. Debug print statements should be removed
  3. Missing error handling
  4. Missing context manager for ZipFile
-def zip_file_list(root_path, zip_filename, file_list=[]):
+def zip_file_list(root_path, zip_filename, file_list=None):
+    if file_list is None:
+        file_list = []
     out_zip_file = os.path.join(root_path, zip_filename)
-    # print('debug: file_list', file_list)
-    zip_obj = ZipFile(out_zip_file, "w")
-    for f in file_list:
-        matched_files = os.path.join(root_path, f)
-        for ii in glob.glob(matched_files):
-            # print('debug: matched_files:ii', ii)
-            if os.path.isdir(ii):
-                arcname = os.path.relpath(ii, start=root_path)
-                zip_obj.write(ii, arcname)
-                for root, dirs, files in os.walk(ii):
-                    for file in files:
-                        filename = os.path.join(root, file)
-                        arcname = os.path.relpath(filename, start=root_path)
-                        # print('debug: filename:arcname:root_path', filename, arcname, root_path)
-                        zip_obj.write(filename, arcname)
-            else:
-                arcname = os.path.relpath(ii, start=root_path)
-                zip_obj.write(ii, arcname)
-    zip_obj.close()
-    return out_zip_file
+    try:
+        with ZipFile(out_zip_file, "w") as zip_obj:
+            for f in file_list:
+                matched_files = os.path.join(root_path, f)
+                for ii in glob.glob(matched_files):
+                    if os.path.isdir(ii):
+                        arcname = os.path.relpath(ii, start=root_path)
+                        zip_obj.write(ii, arcname)
+                        for root, dirs, files in os.walk(ii):
+                            for file in files:
+                                filename = os.path.join(root, file)
+                                arcname = os.path.relpath(filename, start=root_path)
+                                zip_obj.write(filename, arcname)
+                    else:
+                        arcname = os.path.relpath(ii, start=root_path)
+                        zip_obj.write(ii, arcname)
+        return out_zip_file
+    except Exception as e:
+        dlog.error(f"Failed to create zip file {out_zip_file}: {str(e)}")
+        raise
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
def zip_file_list(root_path, zip_filename, file_list=[]):
out_zip_file = os.path.join(root_path, zip_filename)
# print('debug: file_list', file_list)
zip_obj = ZipFile(out_zip_file, "w")
for f in file_list:
matched_files = os.path.join(root_path, f)
for ii in glob.glob(matched_files):
# print('debug: matched_files:ii', ii)
if os.path.isdir(ii):
arcname = os.path.relpath(ii, start=root_path)
zip_obj.write(ii, arcname)
for root, dirs, files in os.walk(ii):
for file in files:
filename = os.path.join(root, file)
arcname = os.path.relpath(filename, start=root_path)
# print('debug: filename:arcname:root_path', filename, arcname, root_path)
zip_obj.write(filename, arcname)
else:
arcname = os.path.relpath(ii, start=root_path)
zip_obj.write(ii, arcname)
zip_obj.close()
return out_zip_file
def zip_file_list(root_path, zip_filename, file_list=None):
if file_list is None:
file_list = []
out_zip_file = os.path.join(root_path, zip_filename)
try:
with ZipFile(out_zip_file, "w") as zip_obj:
for f in file_list:
matched_files = os.path.join(root_path, f)
for ii in glob.glob(matched_files):
if os.path.isdir(ii):
arcname = os.path.relpath(ii, start=root_path)
zip_obj.write(ii, arcname)
for root, dirs, files in os.walk(ii):
for file in files:
filename = os.path.join(root, file)
arcname = os.path.relpath(filename, start=root_path)
zip_obj.write(filename, arcname)
else:
arcname = os.path.relpath(ii, start=root_path)
zip_obj.write(ii, arcname)
return out_zip_file
except Exception as e:
dlog.error(f"Failed to create zip file {out_zip_file}: {e}")
raise
🤖 Prompt for AI Agents
In dpdispatcher/contexts/openapi_context.py lines 35 to 56, fix the mutable
default argument by changing file_list's default to None and initializing it
inside the function. Remove all debug print statements. Add error handling
around the file zipping process to catch and log exceptions. Use a context
manager (with statement) for the ZipFile object to ensure it is properly closed
even if errors occur.

Comment on lines 25 to 29
def unzip_file(zip_file, out_dir="./"):
obj = ZipFile(zip_file, "r")
for item in obj.namelist():
obj.extract(item, out_dir)

Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Add error handling for zip file operations.

The unzip_file function should handle potential errors such as corrupted zip files or extraction failures.

 def unzip_file(zip_file, out_dir="./"):
-    obj = ZipFile(zip_file, "r")
-    for item in obj.namelist():
-        obj.extract(item, out_dir)
+    try:
+        with ZipFile(zip_file, "r") as obj:
+            for item in obj.namelist():
+                obj.extract(item, out_dir)
+    except Exception as e:
+        dlog.error(f"Failed to unzip file {zip_file}: {str(e)}")
+        raise
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
def unzip_file(zip_file, out_dir="./"):
obj = ZipFile(zip_file, "r")
for item in obj.namelist():
obj.extract(item, out_dir)
def unzip_file(zip_file, out_dir="./"):
try:
with ZipFile(zip_file, "r") as obj:
for item in obj.namelist():
obj.extract(item, out_dir)
except Exception as e:
dlog.error(f"Failed to unzip file {zip_file}: {str(e)}")
raise
🤖 Prompt for AI Agents
In dpdispatcher/machines/openapi.py around lines 25 to 29, the unzip_file
function lacks error handling for zip file operations. Add try-except blocks
around the ZipFile opening and extraction code to catch exceptions like
BadZipFile or extraction errors. Log or raise appropriate errors to handle
corrupted zip files or extraction failures gracefully.

pyproject.toml Outdated
Comment on lines 32 to 35
'httpx',
'distro',
'pyhumps',
'bohrium-sdk'
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Remove redundant bohrium-sdk from optional dependencies.

Since bohrium-sdk is now a required dependency (line 35), it should be removed from the optional dependencies to avoid redundancy.

-cloudserver = ["oss2", "tqdm", "bohrium-sdk"]
-bohrium = ["oss2", "tqdm", "bohrium-sdk"]
+cloudserver = ["oss2", "tqdm"]
+bohrium = ["oss2", "tqdm"]

Also applies to: 64-65

🤖 Prompt for AI Agents
In pyproject.toml around lines 32 to 35 and also lines 64 to 65, remove
'bohrium-sdk' from the optional dependencies section because it is already
listed as a required dependency on line 35. This will eliminate redundancy and
keep the dependency declarations clear.

🛠️ Refactor suggestion

Consider adding version constraints for new dependencies.

The newly added dependencies lack version constraints, which could lead to compatibility issues in the future. Consider pinning versions or specifying minimum versions for stability.

-    'httpx',
-    'distro',
-    'pyhumps',
-    'bohrium-sdk'
+    'httpx>=0.24.0',
+    'distro>=1.8.0',
+    'pyhumps>=3.8.0',
+    'bohrium-sdk>=0.1.0'
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
'httpx',
'distro',
'pyhumps',
'bohrium-sdk'
'httpx>=0.24.0',
'distro>=1.8.0',
'pyhumps>=3.8.0',
'bohrium-sdk>=0.1.0'
🤖 Prompt for AI Agents
In pyproject.toml around lines 32 to 35, the newly added dependencies 'httpx',
'distro', 'pyhumps', and 'bohrium-sdk' do not have version constraints
specified. To fix this, update the dependency entries to include either exact
versions or minimum version requirements to ensure compatibility and stability.
Check the latest stable versions of these packages and add appropriate version
specifiers such as '>=x.y.z' or '==x.y.z' for each dependency.

Copy link

codecov bot commented Jul 20, 2025

Codecov Report

Attention: Patch coverage is 18.96552% with 47 lines in your changes missing coverage. Please review.

Project coverage is 47.43%. Comparing base (d81c9f6) to head (de7afb9).

Files with missing lines Patch % Lines
dpdispatcher/contexts/openapi_context.py 19.51% 33 Missing ⚠️
dpdispatcher/machines/openapi.py 17.64% 14 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #524      +/-   ##
==========================================
- Coverage   47.73%   47.43%   -0.30%     
==========================================
  Files          39       39              
  Lines        3863     3904      +41     
==========================================
+ Hits         1844     1852       +8     
- Misses       2019     2052      +33     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Comment on lines +32 to +34
'httpx',
'distro',
'pyhumps'
Copy link
Member

Choose a reason for hiding this comment

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

These should be claimed in bohrium-sdk. I don't think they should be claimed in dpdispatcher

@Rasic2 Rasic2 closed this Aug 6, 2025
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.

3 participants