API リファレンス
クライアント
class Client(api_key=None, timeout=30, retry=True)
API と相互作用するためのメインクライアントクラス。
パラメータ:
api_key(str): 認証用の API キーtimeout(int): リクエストのタイムアウト(秒)retry(bool): 失敗時の自動再試行を有効にする
メソッド
get(endpoint, params=None)
GET リクエストを実行します。
パラメータ:
endpoint(str): API エンドポイント パスparams(dict): クエリ パラメータ
戻り値: レスポンス オブジェクト
例外: API がエラーを返す場合、APIError を発生させます
response = client.get('/users/123')
post(endpoint, data=None)
POST リクエストを実行します。
パラメータ:
endpoint(str): API エンドポイント パスdata(dict): リクエスト本文データ
戻り値: レスポンス オブジェクト
data = {'name': 'John', 'email': 'john@example.com'}
response = client.post('/users', data=data)
put(endpoint, data=None)
PUT リクエストを実行します。
パラメータ:
endpoint(str): API エンドポイント パスdata(dict): リクエスト本文データ
戻り値: レスポンス オブジェクト
updates = {'name': 'Jane'}
response = client.put('/users/123', data=updates)
delete(endpoint)
DELETE リクエストを実行します。
パラメータ:
endpoint(str): API エンドポイント パス
戻り値: レスポンス オブジェクト
response = client.delete('/users/123')
レスポンス
API レスポンスを表します。
属性:
status_code(int): HTTP ステータス コードdata(dict): レスポンス本文データheaders(dict): レスポンス ヘッダー
メソッド
json()
レスポンスを JSON として解析します。
戻り値: 解析された JSON データ
例外
APIError
API がエラーを返す場合に発生します。
NetworkError
ネットワーク エラーが発生した場合に発生します。
AuthenticationError
認証に失敗した場合に発生します。
RateLimitError
レート制限を超えた場合に発生します。
定数
API_BASE_URL
API リクエストのベース URL。
API_BASE_URL = 'https://api.example.com'
API_VERSION
現在の API バージョン。
API_VERSION = 'v1'
例
初期化
from your_package import Client
client = Client(api_key='your-key')
リソースの取得
response = client.get('/users/123')
if response.status_code == 200:
print(response.data)
リソースの作成
data = {'name': 'John', 'email': 'john@example.com'}
response = client.post('/users', data=data)
エラーハンドリング
try:
response = client.get('/users/123')
except APIError as e:
print(f"エラー:{e}")