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
requestslibrary in Python to make HTTP GET requests. - Fetch a list of all posts from the
/postsendpoint. - Given a specific
postId, fetch the comments associated with that post from the/posts/{postId}/commentsendpoint. - Your program should accept the
postIdas a command-line argument. - Print the title of the post and the body of each of its comments.
- Handle potential errors, such as invalid
postIdor network issues.
Expected Behavior: The program should:
- Receive a
postIdfrom the command line. - If the
postIdis not provided or is invalid, print an error message and exit. - Make a GET request to
https://jsonplaceholder.typicode.com/posts/{postId}. - If the post is found, print its title.
- Make a GET request to
https://jsonplaceholder.typicode.com/posts/{postId}/comments. - If comments are found, iterate through the list of comments and print the
bodyof each comment. - 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
postIdwill be an integer. - The program should handle
postIdvalues 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
requestslibrary.
Notes
- The
requestslibrary needs to be installed if you don't have it already (pip install requests). - You can use
sys.argvto access command-line arguments. - Pay attention to the structure of the JSON responses from the API to extract the correct fields (e.g.,
titlefor posts,bodyfor 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.