

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 更新 FHIR 資源
<a name="managing-fhir-resources-update"></a>

FHIR `update`互動會為現有資源建立新的目前版本，如果指定的 尚不存在資源，則會建立初始版本`id`。如需詳細資訊，請參閱 **FHIR R4 RESTful API 文件**[https://hl7.org/fhir/R4/http.html#update](https://hl7.org/fhir/R4/http.html#update)中的 。

**更新 FHIR 資源**  


1. 收集 HealthLake `region`和 `datastoreId` 值。如需詳細資訊，請參閱[取得資料存放區屬性](managing-data-stores-describe.md)。

1. 決定`Resource`要更新並收集相關聯`id`值的 FHIR 類型。如需詳細資訊，請參閱[資源類型](reference-fhir-resource-types.md)。

1. 使用 HealthLake `region`和 的收集值來建構請求的 URL`datastoreId`。同時包含 FHIR `Resource`類型及其相關聯的 `id`。若要在下列範例中檢視整個 URL 路徑，請捲動至**複製**按鈕。

   ```
   PUT https://healthlake.region.amazonaws.com/datastore/datastoreId/r4/Resource/id
   ```

1. 為請求建構`JSON`內文，指定要進行的 FHIR 資料更新。基於此程序的目的，請將檔案儲存為 `update-patient.json`。

   ```
   {
       "id": "2de04858-ba65-44c1-8af1-f2fe69a977d9",
       "resourceType": "Patient",
       "active": true,
       "name": [
           {
               "use": "official",
               "family": "Doe",
               "given": [
                   "Jane"
               ]
           },
           {
               "use": "usual",
               "given": [
                   "Jane"
               ]
           }
       ],
       "gender": "female",
       "birthDate": "1985-12-31"
   }
   ```

1. 傳送 請求。FHIR `update`互動使用具有[AWS 簽章第 4 ](https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_sigv.html)版或 FHIR 授權上的 SMART 的`PUT`請求。下列`curl`範例會更新 HealthLake 中的`Patient`資源。若要檢視整個範例，請捲動至**複製**按鈕。

------
#### [ SigV4 ]

   SigV4 授權

   ```
   curl --request PUT \
     'https://healthlake.region.amazonaws.com/datastore/datastoreId/r4/Patient/id' \
     --aws-sigv4 'aws:amz:region:healthlake' \
     --user "$AWS_ACCESS_KEY_ID:$AWS_SECRET_ACCESS_KEY" \
     --header "x-amz-security-token:$AWS_SESSION_TOKEN" \
     --header 'Accept: application/json' \
     --data @update-patient.json
   ```

   如果現有資源*已更新*，您的請求將傳回 `200` HTTP 狀態碼，如果建立新資源，則傳回 `201` HTTP 狀態碼。

------
#### [ SMART on FHIR ]

   [https://docs.aws.amazon.com/healthlake/latest/APIReference/API_IdentityProviderConfiguration.html](https://docs.aws.amazon.com/healthlake/latest/APIReference/API_IdentityProviderConfiguration.html) 資料類型的 FHIR 上的 SMART 授權範例。

   ```
   {
       "AuthorizationStrategy": "SMART_ON_FHIR",
       "FineGrainedAuthorizationEnabled": true,
       "IdpLambdaArn": "arn:aws:lambda:your-region:your-account-id:function:your-lambda-name",
       "Metadata": "{\"issuer\":\"https://ehr.example.com\", \"jwks_uri\":\"https://ehr.example.com/.well-known/jwks.json\",\"authorization_endpoint\":\"https://ehr.example.com/auth/authorize\",\"token_endpoint\":\"https://ehr.token.com/auth/token\",\"token_endpoint_auth_methods_supported\":[\"client_secret_basic\",\"foo\"],\"grant_types_supported\":[\"client_credential\",\"foo\"],\"registration_endpoint\":\"https://ehr.example.com/auth/register\",\"scopes_supported\":[\"openId\",\"profile\",\"launch\"],\"response_types_supported\":[\"code\"],\"management_endpoint\":\"https://ehr.example.com/user/manage\",\"introspection_endpoint\":\"https://ehr.example.com/user/introspect\",\"revocation_endpoint\":\"https://ehr.example.com/user/revoke\",\"code_challenge_methods_supported\":[\"S256\"],\"capabilities\":[\"launch-ehr\",\"sso-openid-connect\",\"client-public\",\"permission-v2\"]}"
   }
   ```

   發起人可以在授權 lambda 中指派許可。如需詳細資訊，請參閱[OAuth 2.0 範圍](reference-smart-on-fhir-oauth-scopes.md)。

------
#### [ AWS Console ]

   1. 登入 HealthLake 主控台上的[執行查詢](https://console.aws.amazon.com/healthlake/home#/crud)頁面。

   2. 在**查詢設定**區段下，進行下列選擇。
   + **資料存放區 ID** — 選擇資料存放區 ID 以產生查詢字串。
   + **查詢類型** — 選擇 `Update (PUT)`。
   + **資源類型** — 選擇要更新或建立的 FHIR [資源類型](reference-fhir-resource-types.md)。
   + **請求內文** - 建構請求的 JSON 內文，指定要更新資源的 FHIR 資料。

   3. 選擇 **Run query** (執行查詢)。

------

## 根據條件更新 FHIR 資源
<a name="managing-fhir-resources-update-conditional"></a>

條件式更新可讓您根據某些識別搜尋條件更新現有資源，而不是透過邏輯 FHIR `id`。當伺服器處理更新時，它會針對 資源類型使用其標準搜尋功能執行搜尋，目標是解決請求`id`的單一邏輯。

伺服器採取的動作取決於找到多少相符項目：
+ **沒有相符項目，請求內文中未`id`提供**：伺服器會建立 FHIR 資源。
+ **不存在相符項目、`id`提供的和資源：`id`**伺服器會將互動視為更新為建立互動。
+ **沒有相符項目，`id`已提供且已存在**：伺服器拒絕更新時發生錯誤`409 Conflict`。
+ **One Match，未`id`提供資源 OR (`id`提供的資源，且符合找到的資源）**：伺服器會針對相符的資源執行更新，如上所示，如果資源已更新，伺服器 SHALL 會傳回 `200 OK`。
+ **One Match，`id`提供的資源，但不符合找到的資源**：伺服器傳回`409 Conflict`錯誤，指出用戶端 ID 規格是 的問題 `OperationOutcome`
+ **多個相符**項目：伺服器傳回`412 Precondition Failed`錯誤，指出用戶端的條件不夠選擇性，最好使用 OperationOutcome

下列範例會更新名稱為 peter `Patient`的資源，出生日期為 2000 年 1 月 1 日，電話號碼為 1234567890。

```
PUT https://healthlake.region.amazonaws.com/datastore/datastoreId/r4/Patient?name=peter&birthdate=2000-01-01&phone=1234567890
```

## 設定資源更新的驗證層級
<a name="validation-level-resource-updates"></a>

更新 FHIR 資源時，您可以選擇指定 `x-amzn-healthlake-fhir-validation-level` HTTP 標頭來設定資源的驗證層級。 AWS HealthLake 目前支援下列驗證層級：
+ `strict`：資源會根據資源的設定檔元素進行驗證，如果沒有設定檔，則為 R4 規格。這是 的預設驗證層級 AWS HealthLake。
+ `structure-only`：資源會根據 R4 驗證，忽略任何參考的設定檔。
+ `minimal`：最少驗證資源，忽略某些 R4 規則。搜尋/分析所需的結構檢查失敗的資源將更新為包含稽核警告。

使用最低驗證層級更新的資源可能會擷取至資料存放區，即使搜尋索引所需的驗證失敗。在這種情況下，資源將更新為包含 Healthlake 特定的延伸，以記錄上述失敗：

```
{
    "url": "http://healthlake.amazonaws.com/fhir/StructureDefinition/validation-issue",
    "valueString": "{\"resourceType\":\"OperationOutcome\",\"issue\":[{\"severity\":\"error\",\"code\":\"processing\",\"details\":{\"text\":\"FHIR resource in payload failed FHIR validation rules.\"},\"diagnostics\":\"FHIR resource in payload failed FHIR validation rules.\"}]}"
}
```

此外，以下 HTTP 回應標頭會包含在「true」的值中：

```
x-amzn-healthlake-validation-issues : true
```

**注意**  
請注意，如果出現這些錯誤，根據 R4 規格格式不正確擷取的資料可能無法如預期搜尋。