|
| 1 | +# HTTP Client User Guide |
| 2 | + |
| 3 | +This module provides a general-purpose HTTP client for calling target systems described by BTP Destinations. It handles auth-header injection, typed exceptions, and telemetry automatically. |
| 4 | + |
| 5 | +```python |
| 6 | +from sap_cloud_sdk.core.http_client import ( |
| 7 | + HttpClient, |
| 8 | + http_client_for_destination, |
| 9 | + HttpClientError, |
| 10 | + HttpConnectionError, |
| 11 | + HttpNotFoundError, |
| 12 | + HttpResponseError, |
| 13 | + HttpUnauthorizedError, |
| 14 | +) |
| 15 | +``` |
| 16 | + |
| 17 | +## Destination integration |
| 18 | + |
| 19 | +`http_client_for_destination` builds an `HttpClient` from a resolved BTP `Destination`. The destination's auth tokens, ERP headers, and `URL.headers.*` properties are pre-baked into the underlying `requests.Session`, so no manual header management is needed. |
| 20 | + |
| 21 | +```python |
| 22 | +from sap_cloud_sdk.destination import create_client |
| 23 | +from sap_cloud_sdk.core.http_client import http_client_for_destination |
| 24 | + |
| 25 | +dest_client = create_client() |
| 26 | +dest = dest_client.get_destination("MY_API") |
| 27 | + |
| 28 | +http = http_client_for_destination(dest) |
| 29 | +response = http.get("/api/v1/resources") |
| 30 | +data = response.json() |
| 31 | +``` |
| 32 | + |
| 33 | +> **Note:** Use a destination fetched via `get_destination()` (v2 API), which populates `auth_tokens`. Destinations from the deprecated v1 methods do not carry pre-fetched tokens. |
| 34 | +
|
| 35 | +When the destination URL points to a host root rather than the service root, pass `sub_path`: |
| 36 | + |
| 37 | +```python |
| 38 | +http = http_client_for_destination(dest, sub_path="api/v1") |
| 39 | +response = http.get("/resources") # calls https://host/api/v1/resources |
| 40 | +``` |
| 41 | + |
| 42 | +## Direct construction |
| 43 | + |
| 44 | +Inject any `requests.Session` directly when not using BTP Destinations: |
| 45 | + |
| 46 | +```python |
| 47 | +import requests |
| 48 | +from sap_cloud_sdk.core.http_client import HttpClient |
| 49 | + |
| 50 | +session = requests.Session() |
| 51 | +session.headers["Authorization"] = "Bearer <token>" |
| 52 | + |
| 53 | +http = HttpClient(base_url="https://api.example.com", session=session) |
| 54 | +``` |
| 55 | + |
| 56 | +## HTTP methods |
| 57 | + |
| 58 | +All convenience methods raise typed exceptions on non-2xx responses and record telemetry automatically. |
| 59 | + |
| 60 | +```python |
| 61 | +# GET — with optional query parameters |
| 62 | +response = http.get("/items", params={"$top": "10", "status": "active"}) |
| 63 | + |
| 64 | +# POST — JSON body |
| 65 | +response = http.post("/items", json={"name": "new item", "type": "A"}) |
| 66 | + |
| 67 | +# PUT — full replacement |
| 68 | +response = http.put("/items/1", json={"name": "updated item"}) |
| 69 | + |
| 70 | +# PATCH — partial update |
| 71 | +response = http.patch("/items/1", json={"status": "inactive"}) |
| 72 | + |
| 73 | +# DELETE |
| 74 | +response = http.delete("/items/1") |
| 75 | + |
| 76 | +# Low-level request — does NOT raise on non-2xx; caller inspects the response |
| 77 | +response = http.request("GET", "/items") |
| 78 | +if not response.ok: |
| 79 | + ... |
| 80 | +``` |
| 81 | + |
| 82 | +### Extra headers per request |
| 83 | + |
| 84 | +Pass `headers=` to add or override headers for a single call: |
| 85 | + |
| 86 | +```python |
| 87 | +response = http.get("/items", headers={"X-Correlation-ID": "abc123"}) |
| 88 | +``` |
| 89 | + |
| 90 | +Per-request headers are merged on top of the session defaults. |
| 91 | + |
| 92 | +### Raw body |
| 93 | + |
| 94 | +Use `data=` for non-JSON payloads: |
| 95 | + |
| 96 | +```python |
| 97 | +response = http.post("/upload", data=b"raw bytes", headers={"Content-Type": "application/octet-stream"}) |
| 98 | +``` |
| 99 | + |
| 100 | +## What headers are pre-baked |
| 101 | + |
| 102 | +When `http_client_for_destination` builds the client it calls `dest.get_headers()`, which injects the following into the session: |
| 103 | + |
| 104 | +1. **ERP headers** — `sap-client` and `sap-language` from destination properties (if present) |
| 105 | +2. **`URL.headers.*` properties** — any destination property prefixed with `URL.headers.` becomes a header (e.g. `URL.headers.apiKey = secret` → `apiKey: secret`) |
| 106 | +3. **Auth tokens** — pre-fetched by BTP and returned in `dest.auth_tokens`; each token's `http_header` dict is injected directly (e.g. `Authorization: Bearer eyJ...`) |
| 107 | + |
| 108 | +Auth tokens take precedence over `URL.headers.*` when both set the same key. |
| 109 | + |
| 110 | +## Error handling |
| 111 | + |
| 112 | +```python |
| 113 | +from sap_cloud_sdk.core.http_client import ( |
| 114 | + http_client_for_destination, |
| 115 | + HttpNotFoundError, |
| 116 | + HttpUnauthorizedError, |
| 117 | + HttpResponseError, |
| 118 | + HttpConnectionError, |
| 119 | +) |
| 120 | + |
| 121 | +http = http_client_for_destination(dest) |
| 122 | + |
| 123 | +try: |
| 124 | + response = http.get("/items/123") |
| 125 | +except HttpNotFoundError: |
| 126 | + print("item not found") |
| 127 | +except HttpUnauthorizedError as e: |
| 128 | + print(f"auth failure (HTTP {e.status_code}): check destination configuration") |
| 129 | +except HttpResponseError as e: |
| 130 | + print(f"service error (HTTP {e.status_code})") |
| 131 | +except HttpConnectionError: |
| 132 | + print("network unreachable — no response received") |
| 133 | +``` |
| 134 | + |
| 135 | +Exception hierarchy: |
| 136 | + |
| 137 | +``` |
| 138 | +HttpClientError |
| 139 | +├── HttpResponseError # non-2xx HTTP response (carries .status_code and .response) |
| 140 | +│ ├── HttpNotFoundError # 404 |
| 141 | +│ └── HttpUnauthorizedError # 401 / 403 |
| 142 | +└── HttpConnectionError # network failure — no HTTP response received |
| 143 | +``` |
| 144 | + |
| 145 | +`HttpResponseError` exposes: |
| 146 | +- `.status_code: int` — the HTTP status code |
| 147 | +- `.response` — the raw `requests.Response` object |
0 commit comments