Skip to content

SSRequest SSResponse

Ivan William Harsono edited this page Nov 17, 2026 · 2 revisions

SSRequest & SSResponse

Bahasa / Language: English · Bahasa Indonesia


English

SSRequest is the HTTP layer; SSResponse wraps what comes back. Both are synchronous.

Constructing SSRequest

from satusehat_integration import OAuth2Client, SSRequest

oauth2 = OAuth2Client(satusehat_env="DEV")     # credentials from CLIENTID_DEV etc.
ss = SSRequest(oauth2)

The base URL comes from oauth2.fhir_url — the FHIR endpoint of the environment you chose, not the OAuth2 host. To point somewhere else, pass it explicitly; a token with no oauth2 object also works when you already have one:

ss = SSRequest(base_url="https://api-satusehat-stg.dto.kemkes.go.id/fhir-r4/v1", token="…")

Tuning goes through config, not keyword arguments:

ss = SSRequest(
    oauth2,
    config={
        "timeout": 30,          # seconds per request
        "maxRetries": 5,        # retries on retryable failures
        "verify": True,         # TLS verification — do not disable in production
        "log_enabled": True,    # keep the request log in ss.logs
        "headers": {"X-Request-Id": "…"},
    },
)

max_retries is accepted as an alias for maxRetries. session= and sleep_fn= are there for tests: a fake session and a no-op sleep make retry behaviour testable without waiting.

Methods

ss.get("Patient", {"name": "Sitorus"})        # search
ss.get("Patient/abc-123")                     # read by id
ss.post("Patient", patient_payload)           # create
ss.put("Patient/abc-123", patient_payload)    # replace
ss.delete("Encounter/xyz-789")                # delete
ss.patch("Patient/abc-123", patch_bundle)     # JSON Patch
ss.patch_fhir_path("Patient/abc-123", bundle) # FHIRPath Patch
ss.post_bundle("", bundle_payload)            # transaction bundle

The first argument is the path after the base URL; "Patient" becomes …/fhir-r4/v1/Patient. A full https:// URL is used as-is, which is how you follow a next link from a search Bundle.

SSResponse

class SSResponse:
    def __init__(self, http_code: int, body: dict | None = None, content: str = ""): ...

    http_code: int;  body: dict;  content: str          # properties
    def get_http_code(self) -> int
    def get_body(self) -> dict
    def get_content(self) -> str
    def is_success(self) -> bool          # 2xx
    def is_error(self) -> bool
    def get_error_messages(self) -> list[str]
    def get_resource_id(self) -> str | None
    def get(self, resource_type: str = "") -> str | None   # alias of get_resource_id
resp = ss.post("Patient", patient_payload)

if resp.is_success():
    print("id:", resp.get_resource_id())
else:
    print(resp.get_http_code(), resp.get_error_messages())

get_resource_id() looks in the obvious places — id on the resource, resource_id/patient_id in a non-FHIR envelope — and returns None rather than raising when the body has no id. It never guesses.

Authentication and retry, inside

  • The token is taken from oauth2.token() (50-minute cache) unless you passed token=.
  • A 401 triggers one refresh and one replay before the error is returned; infinite refresh loops are not possible.
  • 429, 5xx, 408 and network errors are retried up to maxRetries, honouring Retry-After when present.
  • Every attempt is recorded in ss.logs when log_enabled is on — that list is what to dump into a bug report.

Bahasa Indonesia

SSRequest adalah lapisan HTTP; SSResponse membungkus jawabannya. Keduanya sinkron.

Membuat SSRequest

from satusehat_integration import OAuth2Client, SSRequest

oauth2 = OAuth2Client(satusehat_env="DEV")     # kredensial dari CLIENTID_DEV dsb.
ss = SSRequest(oauth2)

Base URL diambil dari oauth2.fhir_url — endpoint FHIR environment yang dipilih, bukan host OAuth2. Untuk mengarahkan ke tempat lain, berikan eksplisit; token tanpa objek oauth2 juga bisa dipakai bila Anda sudah punya tokennya:

ss = SSRequest(base_url="https://api-satusehat-stg.dto.kemkes.go.id/fhir-r4/v1", token="…")

Penyetelan lewat config, bukan argumen kata kunci:

ss = SSRequest(
    oauth2,
    config={
        "timeout": 30,          # detik per permintaan
        "maxRetries": 5,        # ulangan untuk kegagalan yang bisa diulang
        "verify": True,         # verifikasi TLS — jangan dimatikan di produksi
        "log_enabled": True,    # simpan log permintaan di ss.logs
        "headers": {"X-Request-Id": "…"},
    },
)

max_retries diterima sebagai alias maxRetries. session= dan sleep_fn= disediakan untuk pengujian: session palsu dan sleep tanpa jeda membuat perilaku ulang bisa diuji tanpa menunggu.

Metode

ss.get("Patient", {"name": "Sitorus"})        # pencarian
ss.get("Patient/abc-123")                     # baca per id
ss.post("Patient", patient_payload)           # buat
ss.put("Patient/abc-123", patient_payload)    # ganti
ss.delete("Encounter/xyz-789")                # hapus
ss.patch("Patient/abc-123", patch_bundle)     # JSON Patch
ss.patch_fhir_path("Patient/abc-123", bundle) # FHIRPath Patch
ss.post_bundle("", bundle_payload)            # transaction bundle

Argumen pertama adalah path setelah base URL; "Patient" menjadi …/fhir-r4/v1/Patient. URL https:// penuh dipakai apa adanya — itulah cara mengikuti tautan next dari Bundle hasil pencarian.

SSResponse

class SSResponse:
    def __init__(self, http_code: int, body: dict | None = None, content: str = ""): ...

    http_code: int;  body: dict;  content: str          # properti
    def get_http_code(self) -> int
    def get_body(self) -> dict
    def get_content(self) -> str
    def is_success(self) -> bool          # 2xx
    def is_error(self) -> bool
    def get_error_messages(self) -> list[str]
    def get_resource_id(self) -> str | None
    def get(self, resource_type: str = "") -> str | None   # alias get_resource_id
resp = ss.post("Patient", patient_payload)

if resp.is_success():
    print("id:", resp.get_resource_id())
else:
    print(resp.get_http_code(), resp.get_error_messages())

get_resource_id() mencari di tempat yang wajar — id pada resource, resource_id/patient_id pada amplop non-FHIR — dan mengembalikan None, bukan melempar galat, bila body tidak punya id. Ia tidak pernah menebak.

Autentikasi dan ulangan, dari dalam

  • Token diambil dari oauth2.token() (cache 50 menit) kecuali Anda memberi token=.
  • 401 memicu satu penyegaran token dan satu pengiriman ulang sebelum galat dikembalikan; perulangan penyegaran tanpa akhir tidak mungkin terjadi.
  • 429, 5xx, 408, dan galat jaringan diulang sampai maxRetries, menghormati Retry-After bila ada.
  • Setiap percobaan dicatat di ss.logs bila log_enabled menyala — daftar itulah yang dilampirkan saat melaporkan masalah.

Clone this wiki locally