पायथन उदाहरण – हेल्थ फिटनेस एपीआई¶
पायथन, हेल्थ फिटनेस एपीआई जैसे RESTful एपीआई के साथ इंटरैक्ट करना आसान बनाता है। यहां, सिंक्रोनस और एसिंक्रोनस अनुरोधों दोनों के लिए कई पायथन 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)
urllib का उपयोग करके 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को अपनी वास्तविक एपीआई कुंजी से बदलें। - बहु-भाषा समर्थन के लिए
langपैरामीटर का उपयोग करें (en,fr,de,es, आदि)। - प्रदर्शन के लिए, उत्पादन के लिए कनेक्शन पूलिंग (
httpxक्लाइंट) और एसिंक्रोनस अनुरोधों पर विचार करें। - त्रुटियों को कुशलता से संभालें: HTTP स्थिति कोड, समय सीमा, और अपवाद।
- उपलब्ध होने पर प्रतिक्रिया में जोखिम संकेत, कार्य योजनाएं, और एंटरप्राइज़ मेटाडेटा शामिल हैं।
आगे के कदम¶
अन्य भाषा-विशिष्ट उदाहरण देखें:
- PHP
- JavaScript
- NodeJs
- Kotlin / Android
- Flutter / Dart
- React Native / Expo
- Bash Shell
- Go / Golang
- Ruby / Rails
आज ही अपने पायथन एप्लिकेशन में हेल्थ फिटनेस एपीआई को एकीकृत करना शुरू करें!
हेल्थ फिटनेस एपीआई आधुनिक डिजिटल स्वास्थ्य बुनियादी ढांचे को शक्ति प्रदान करना