NurCore API
SDKs & Examples

Python Client

Минимальный Python wrapper для server-to-server интеграций.

Готового PyPI-пакета пока нет. Используйте httpx для async или requests для sync кода.

Async (httpx)

import os
import httpx

class NurCoreClient:
    def __init__(
        self,
        api_key: str,
        base_url: str = "https://api.nurcore.kg/api/v1",
    ):
        self.base_url = base_url
        self._client = httpx.AsyncClient(
            timeout=30.0,
            headers={
                "X-API-Key": api_key,
                "Content-Type": "application/json",
            },
        )

    async def get(self, path: str, **params) -> dict:
        resp = await self._client.get(f"{self.base_url}{path}", params=params)
        resp.raise_for_status()
        return resp.json()

    async def post(self, path: str, body: dict | None = None) -> dict:
        resp = await self._client.post(
            f"{self.base_url}{path}", json=body or {}
        )
        resp.raise_for_status()
        return resp.json() if resp.status_code != 204 else {}

    async def close(self):
        await self._client.aclose()


# Использование
async def main():
    client = NurCoreClient(api_key=os.environ["NURCORE_API_KEY"])

    # Поиск рейсов
    flights = await client.get(
        "/schedules/flights/",
        route_code="BSZ-OSS",
        flight_date="2026-05-10",
    )
    print(f"Found {flights['total']} flights")

    # Создание брони
    booking = await client.post("/bookings/", body={
        "flight_id": "...",
        "fare_price_id": "...",
        "passengers": [{"first_name": "JOHN", "last_name": "DOE", ...}],
        "contact_email": "user@example.com",
    })
    print(f"Created PNR: {booking['booking_reference']}")

    await client.close()

Sync (requests)

import requests

class NurCoreClient:
    def __init__(self, api_key, base_url="https://api.nurcore.kg/api/v1"):
        self.base_url = base_url
        self.session = requests.Session()
        self.session.headers.update({
            "X-API-Key": api_key,
            "Content-Type": "application/json",
        })

    def get(self, path, **params):
        r = self.session.get(f"{self.base_url}{path}", params=params, timeout=30)
        r.raise_for_status()
        return r.json()

    def post(self, path, body=None):
        r = self.session.post(f"{self.base_url}{path}", json=body or {}, timeout=30)
        r.raise_for_status()
        return r.json() if r.status_code != 204 else {}
async def load_public_booking(booking_id: str, access_token: str):
    async with httpx.AsyncClient() as client:
        resp = await client.get(
            f"https://api.nurcore.kg/api/v1/bookings/{booking_id}/public",
            params={"access_token": access_token},
        )
        resp.raise_for_status()
        return resp.json()

Retry с backoff

from tenacity import retry, stop_after_attempt, wait_exponential

@retry(
    stop=stop_after_attempt(3),
    wait=wait_exponential(multiplier=1, min=1, max=10),
    retry_error_callback=lambda x: None,
)
async def get_with_retry(client: NurCoreClient, path: str, **params):
    return await client.get(path, **params)

Best practices

  • Один httpx.AsyncClient на app instance — connection pooling
  • Всегда явный timeout (30s default, дольше для bulk endpoints)
  • Не выводите API Key в логи / stack traces
  • Используйте os.environ или secret-manager для credentials

On this page