Code ReviewIntermediate~20 min

Review: API Client Design

pythonapihttprequestserror-handling

Code Review Task

Please review the following Python code.

Background

This code is a client library for interacting with an external API. It requires authentication, error handling, retry logic, and more.

Review Criteria

  • Timeout configuration
  • Error handling
  • Handling of authentication credentials
  • Resource management

Your Answer

api_client.pypython
import requests
class APIClient:
def __init__(self):
self.api_key = "sk-1234567890abcdef"
self.base_url = "https://api.example.com"
def get_user(self, user_id):
"""Get user information"""
url = f"{self.base_url}/users/{user_id}"
headers = {"Authorization": f"Bearer {self.api_key}"}
response = requests.get(url, headers=headers)
return response.json()
def create_user(self, user_data):
"""Create new user"""
url = f"{self.base_url}/users"
headers = {"Authorization": f"Bearer {self.api_key}"}
response = requests.post(url, json=user_data, headers=headers)
print(f"Created user: {user_data}")
return response.json()
行番号をクリックしてコメントを追加(Shift+クリックで範囲選択)

Minimum 100 characters required

0

Add comments and enter at least 100 characters in the summary

Model Answer

Submit your answer or click "Show" to view the model answer.