コンテンツにスキップ

Python の例 – ヘルスフィットネス API

Python は、ヘルスフィットネス API のような RESTful API とのやり取りを簡単にする。以下に、複数の Python HTTP ライブラリを使用して、同期および非同期の両方のリクエストを行う例を示します。


1. requests (同期、最も一般的な方法)

import requests

API_KEY = "YOUR_API_KEY"
url = "https://api.hefitapi.com/api/v1/bmi"
params = {
    "height": 178,
    "weight": 82,
    "units": "metric",
    "lang": "en"
}
headers = {"X-API-Key": API_KEY}

response = requests.get(url, headers=headers, params=params)
data = response.json()

print(data)

POST リクエストの例:

import requests

API_KEY = "YOUR_API_KEY"
url = "https://api.hefitapi.com/api/v1/bmi/post"
payload = {
    "height": 178,
    "weight": 82,
    "units": "metric"
}
headers = {
    "X-API-Key": API_KEY,
    "Content-Type": "application/json"
}

response = requests.post(url, headers=headers, json=payload)
data = response.json()

print(data)

2. httpx (同期および非同期)

同期の GET リクエストの例:

import httpx

API_KEY = "YOUR_API_KEY"
url = "https://api.hefitapi.com/api/v1/bmi"
params = {"height": 178, "weight": 82, "units": "metric", "lang": "en"}
headers = {"X-API-Key": API_KEY}

with httpx.Client() as client:
    response = client.get(url, headers=headers, params=params)
    data = response.json()
    print(data)

非同期の GET リクエストの例:

import httpx
import asyncio

API_KEY = "YOUR_API_KEY"
url = "https://api.hefitapi.com/api/v1/bmi"
params = {"height": 178, "weight": 82, "units": "metric", "lang": "en"}
headers = {"X-API-Key": API_KEY}

async def fetch_bmi():
    async with httpx.AsyncClient() as client:
        response = await client.get(url, headers=headers, params=params)
        data = response.json()
        print(data)

asyncio.run(fetch_bmi())

非同期の POST リクエスト:

async def post_bmi():
    payload = {"height": 178, "weight": 82, "units": "metric"}
    async with httpx.AsyncClient() as client:
        response = await client.post(url + "/post", headers=headers, json=payload)
        data = response.json()
        print(data)

asyncio.run(post_bmi())

3. urllib (標準ライブラリ)

import json
from urllib import request, parse

API_KEY = "YOUR_API_KEY"
url = "https://api.hefitapi.com/api/v1/bmi"
params = {"height": 178, "weight": 82, "units": "metric", "lang": "en"}
query_string = parse.urlencode(params)
full_url = f"{url}?{query_string}"

req = request.Request(full_url, headers={"X-API-Key": API_KEY})
with request.urlopen(req) as response:
    data = json.load(response)
    print(data)

標準ライブラリを使用した POST リクエスト:

payload = json.dumps({"height": 178, "weight": 82, "units": "metric"}).encode("utf-8")
req = request.Request(url + "/post", data=payload, method="POST")
req.add_header("Content-Type", "application/json")
req.add_header("X-API-Key", API_KEY)

with request.urlopen(req) as response:
    data = json.load(response)
    print(data)

4. aiohttp (非同期、非同期フレームワークで人気)

import aiohttp
import asyncio

API_KEY = "YOUR_API_KEY"
url = "https://api.hefitapi.com/api/v1/bmi"
params = {"height": 178, "weight": 82, "units": "metric", "lang": "en"}
headers = {"X-API-Key": API_KEY}

async def fetch_bmi():
    async with aiohttp.ClientSession() as session:
        async with session.get(url, headers=headers, params=params) as resp:
            data = await resp.json()
            print(data)

asyncio.run(fetch_bmi())

aiohttp を使用した POST リクエストの例:

async def post_bmi():
    payload = {"height": 178, "weight": 82, "units": "metric"}
    async with aiohttp.ClientSession() as session:
        async with session.post(url + "/post", headers=headers, json=payload) as resp:
            data = await resp.json()
            print(data)

asyncio.run(post_bmi())

5. 備考とベストプラクティス

  • 必ず YOUR_API_KEY を実際の API キーで置き換えてください。
  • lang パラメータを使用して多言語をサポートします (en, fr, de, es, など)。
  • 性能向上のために、本番環境では 接続プーリング (クライアント) と 非同期 リクエストを検討してください。
  • エラーを適切に処理してください: HTTP ステータスコード、タイムアウト、および例外。
  • 可能な場合は、応答には リスク信号、アクションプラン、およびエンタープライズメタデータ が含まれます。

次のステップ

その他の言語固有の例を確認してください:

今日からヘルスフィットネス API を Python アプリケーションに統合しましょう!


ヘルスフィットネス API モダンなデジタルヘルスインフラを可能に