# ovok_sdk.resources.fhir

Canonical page: https://docs.ovok.com/python-sdk/reference/resources/fhir

Generic FHIR R4 reads, searches, writes and batches.

## FhirResource [#fhirresource]

```python
FhirResource(http: HttpClient)
```

Generic FHIR R4 on `{base_url}/{fhir_path}`.

Created by `Ovok`; access it as `ovok.fhir`. Resources are plain dicts in FHIR JSON shape.

**Examples**

```python
patient = ovok.fhir.read("Patient", "patient-id")
page = ovok.fhir.search("Observation", {"patient": "Patient/patient-id"})
```

### read() [#read]

```python
read(resource_type: str, resource_id: str) -> Resource
```

Read one resource by type and ID.

**Parameters**

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `resource_type` | `str` |  | FHIR resource type, such as `"Patient"`. |
| `resource_id` | `str` |  | The resource's logical ID. |

**Returns**

- `Resource`: The resource.

**Raises**

- `OvokNotFoundError`: The resource does not exist.

### search() [#search]

```python
search(resource_type: str, params: QueryParams | None = None) -> Page[Resource]
```

Search one resource type and return the first page of matches.

A list value repeats its parameter, which FHIR reads as AND.

**Parameters**

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `resource_type` | `str` |  | FHIR resource type to search. |
| `params` | `QueryParams \| None` | `None` | FHIR search parameters. |

**Returns**

- `Page[Resource]`: The first page; call `next_page()` or `auto_paging_iter()` for the rest.

### search_one() [#search_one]

```python
search_one(resource_type: str, params: QueryParams | None = None) -> Resource | None
```

Return the first resource matching a search, or None.

**Parameters**

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `resource_type` | `str` |  | FHIR resource type to search. |
| `params` | `QueryParams \| None` | `None` | FHIR search parameters; `_count` is set to 1. |

**Returns**

- `Resource | None`: The first match, or None when nothing matches.

### create() [#create]

```python
create(resource: Resource, *, if_none_exist: str | None = None) -> Resource
```

Create a resource of its `resourceType`.

With `if_none_exist`, a retry cannot create a duplicate, so the request is safe to repeat.

**Parameters**

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `resource` | `Resource` |  | The resource to create; it needs `resourceType`. |
| `if_none_exist` | `str \| None` | `None` | FHIR search query for a conditional create, such as `"identifier=x\|1"`. |

**Returns**

- `Resource`: The created (or matched) resource.

**Raises**

- `OvokValidationError`: The resource has no `resourceType`.

### update() [#update]

```python
update(resource: Resource) -> Resource
```

Replace a resource with a PUT to its type and ID.

**Parameters**

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `resource` | `Resource` |  | The full resource; it needs `resourceType` and `id`. |

**Returns**

- `Resource`: The updated resource.

**Raises**

- `OvokValidationError`: The resource has no `resourceType` or `id`.

### patch() [#patch]

```python
patch(
    resource_type: str,
    resource_id: str,
    operations: Sequence[PatchOperation],
) -> Resource
```

Apply JSON Patch operations to a resource.

**Parameters**

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `resource_type` | `str` |  | FHIR resource type. |
| `resource_id` | `str` |  | The resource's logical ID. |
| `operations` | `Sequence[PatchOperation]` |  | JSON Patch operations, such as `{"op": "replace", "path": "/active", "value": False}`. |

**Returns**

- `Resource`: The patched resource.

### delete() [#delete]

```python
delete(resource_type: str, resource_id: str) -> None
```

Delete a resource.

**Parameters**

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `resource_type` | `str` |  | FHIR resource type. |
| `resource_id` | `str` |  | The resource's logical ID. |

### batch() [#batch]

```python
batch(bundle: Resource) -> Resource
```

Post a `batch` or `transaction` Bundle to the FHIR base.

**Parameters**

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `bundle` | `Resource` |  | The Bundle resource. |

**Returns**

- `Resource`: The response Bundle.
