Hone logo
Hone
Problems

Fetching Data from a Public API

This challenge will test your ability to make HTTP requests in Python, a fundamental skill for interacting with web services and APIs. You will fetch data from a well-known public API and process the relevant information.

Problem Description

Your task is to write a Python program that retrieves data from the JSONPlaceholder API, a free fake online REST API for testing and prototyping. Specifically, you will fetch a list of posts and then, for a specific post, retrieve its associated comments.

Here are the key requirements:

  • Use the requests library in Python to make HTTP GET requests.
  • Fetch a list of all posts from the /posts endpoint.
  • Given a specific postId, fetch the comments associated with that post from the /posts/{postId}/comments endpoint.
  • Your program should accept the postId as a command-line argument.
  • Print the title of the post and the body of each of its comments.
  • Handle potential errors, such as invalid postId or network issues.

Expected Behavior: The program should:

  1. Receive a postId from the command line.
  2. If the postId is not provided or is invalid, print an error message and exit.
  3. Make a GET request to https://jsonplaceholder.typicode.com/posts/{postId}.
  4. If the post is found, print its title.
  5. Make a GET request to https://jsonplaceholder.typicode.com/posts/{postId}/comments.
  6. If comments are found, iterate through the list of comments and print the body of each comment.
  7. If the post or its comments are not found, or if there's a network error, print an appropriate error message.

Examples

Example 1:

Command: python your_script_name.py 1
Output:
Post Title: sunt aut facere repellat provident occaecati excepturi optio reprehenderit
Comment 1: est rerum tempore vitae
sequi sint nihil reprehenderit
doloremque ipsum quia et aut
voluptatem commodi dignissimos
... (other comments will follow) ...

Explanation: The program fetches post with ID 1, prints its title, and then fetches and prints the bodies of all comments associated with post ID 1.

Example 2:

Command: python your_script_name.py 101
Output:
Error: Post with ID 101 not found.

Explanation: Post ID 101 does not exist in the JSONPlaceholder API. The program should detect this and print an error message.

Example 3:

Command: python your_script_name.py
Output:
Usage: python your_script_name.py <postId>
Error: Please provide a valid postId as a command-line argument.

Explanation: The program requires a postId to be provided as a command-line argument. If it's missing, it prints a usage message and an error.

Constraints

  • The postId will be an integer.
  • The program should handle postId values from 1 to 100 (as per JSONPlaceholder's documentation for posts).
  • Network requests should not take longer than 10 seconds to respond.
  • You must use the requests library.

Notes

  • The requests library needs to be installed if you don't have it already (pip install requests).
  • You can use sys.argv to access command-line arguments.
  • Pay attention to the structure of the JSON responses from the API to extract the correct fields (e.g., title for posts, body for comments).
  • HTTP status codes are important for determining the success or failure of a request. Check for status codes like 200 (OK), 404 (Not Found), etc.
Loading editor...
python