Skip to content
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

Add examples #18

Merged
merged 4 commits into from
Oct 20, 2024
Merged

Add examples #18

merged 4 commits into from
Oct 20, 2024

Conversation

ssghait007
Copy link

@ssghait007 ssghait007 commented Oct 17, 2024

fixes #11

Summary by Sourcery

Introduce example scripts for JSONPlaceholder, OpenWeatherMap, and GitHub APIs to demonstrate data fetching and path tracing using the trace_dkey module.

New Features:

  • Add a JSONPlaceholder API example script to demonstrate fetching and tracing data paths for a post and its comments.
  • Add an OpenWeatherMap API example script to demonstrate fetching weather data and tracing data paths for temperature and weather description.
  • Add a GitHub API example script to demonstrate fetching repository data and tracing data paths for stargazers count and owner's login.

Copy link
Contributor

sourcery-ai bot commented Oct 17, 2024

Reviewer's Guide by Sourcery

This pull request adds three example files demonstrating the usage of the trace_dkey function with different APIs: JSONPlaceholder, OpenWeatherMap, and GitHub. Each example file contains a function that makes API requests, processes the response, and uses the trace_dkey function to find specific keys in the JSON data.

Class diagram for the new example scripts

classDiagram
    class jsonplaceholder_api_example {
        +requests.get(url)
        +trace(post_data, 'title')
        +trace(comments_data[0], 'email')
    }
    class openweathermap_api_example {
        +requests.get(url)
        +trace(data, 'temp')
        +trace(data, 'description')
    }
    class github_api_example {
        +requests.get(url)
        +trace(data, 'stargazers_count')
        +trace(data, 'login')
    }
    class trace_dkey {
        +trace(data, key)
    }
    jsonplaceholder_api_example --> trace_dkey
    openweathermap_api_example --> trace_dkey
    github_api_example --> trace_dkey
Loading

File-Level Changes

Change Details Files
Add JSONPlaceholder API example
  • Import required libraries
  • Define a function to demonstrate the usage
  • Make API requests to get a post and its comments
  • Use trace_dkey to find paths to 'title' and 'email' keys
  • Print the paths and corresponding values
examples/jsonplaceholder_api_example.py
Add OpenWeatherMap API example
  • Import required libraries
  • Define a function to demonstrate the usage
  • Make an API request to get weather data for a city
  • Use trace_dkey to find paths to 'temp' and 'description' keys
  • Print the paths and corresponding values
  • Include API key handling using environment variables
examples/openweathermap_api_example.py
Add GitHub API example
  • Import required libraries
  • Define a function to demonstrate the usage
  • Make an API request to get repository information
  • Use trace_dkey to find paths to 'stargazers_count' and 'login' keys
  • Print the paths and corresponding values
examples/github_api_example.py

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time. You can also use
    this command to specify where the summary should be inserted.

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey @ssghait007 - I've reviewed your changes - here's some feedback:

Overall Comments:

  • Consider removing the hardcoded API key in the OpenWeatherMap example and use a more secure method for handling API keys, such as a configuration file.
  • The examples could benefit from some refactoring to reduce code duplication. Consider creating a common function for making API requests and processing responses.
  • To enhance the educational value of these examples, consider adding more detailed comments explaining the trace-dkey functionality and implement error handling for API requests.
Here's what I looked at during the review
  • 🟡 General issues: 1 issue found
  • 🟡 Security: 1 issue found
  • 🟢 Testing: all looks good
  • 🟢 Complexity: all looks good
  • 🟡 Documentation: 4 issues found

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

def jsonplaceholder_api_example():
# Get a sample post
url = "https://jsonplaceholder.typicode.com/posts/1"
response = requests.get(url)
Copy link
Contributor

Choose a reason for hiding this comment

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

suggestion: Consider adding error handling for API responses

It's important to handle potential exceptions when making API calls. You could use a try-except block to catch and handle requests.exceptions.RequestException.

try:
    response = requests.get(url)
    response.raise_for_status()
except requests.exceptions.RequestException as e:
    print(f"An error occurred: {e}")
    return

@Agent-Hellboy
Copy link
Owner

Agent-Hellboy commented Oct 17, 2024

Thanks @ssghait007 , resolve these comments, I have disabled Sourcery, feels like unnecessary noise

Agent-Hellboy and others added 3 commits October 20, 2024 22:14
Co-authored-by: sourcery-ai[bot] <58596630+sourcery-ai[bot]@users.noreply.github.com>
Co-authored-by: sourcery-ai[bot] <58596630+sourcery-ai[bot]@users.noreply.github.com>
@Agent-Hellboy Agent-Hellboy merged commit ed5b58b into Agent-Hellboy:main Oct 20, 2024
22 checks passed
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.

Add examples in example folder with response of famous opensource APIs
2 participants