Patching
SanteDB's REST API supports patching of resources. Patching follows IETF RFC 5789 pattern and has the following rules:
  • The patch must be applied to a specific instance of a resource
  • The patch must include the If-Match header with the etag of the last version the client has (and wishes to patch)

Constructing a Patch

The format of a patch in SanteDB is illustrated below:
1
<Patch xmlns="http://santedb.org/model">
2
<appliesTo type="Patient|Place|Entity...">
3
<id>9fdb557d-2e2f-4fe5-a193-e1cf8cf49bc4</id>
4
</appliesTo>
5
<change op="Test|Replace|Remove|Add" path="pathInObject">
6
<value>
7
object
8
</value>
9
</change>
10
</Patch>
Copied!
For example, if we wanted to replace the Legal name of a patient, the patch would be as follows:
1
<Patch xmlns="http://santedb.org/model">
2
<appliesTo type="Patient">
3
<id>1c8c9aa3-abb8-48ee-992d-8f0c524ce40c</id>
4
</appliesTo>
5
<!-- Remove existing Legal Name -->
6
<change op="Remove" path="name">
7
<value>
8
<EntityName>
9
<use>effe122d-8d30-491d-805d-addcb4466c35</use>
10
</EntityName>
11
</value>
12
</change>
13
<change op="Add" path="name">
14
<value>
15
<EntityName>
16
<use>effe122d-8d30-491d-805d-addcb4466c35</use>
17
<component>
18
<value>Jim</value>
19
</component>
20
</EntityName>
21
</change>
22
</Patch>
Copied!
Alternately you can simply identify a specific instance to be removed by substituting the use qualifier with a UUID:
1
<change op="Remove" path="name">
2
<value>9fdb557d-2e2f-4fe5-a193-e1cf8cf49bc4</value>
3
</change>
Copied!
Which removes a name with ID : 9fdb557d-2e2f-4fe5-a193-e1cf8cf49bc4 from the patient's name list then replaces it.
You can also patch using JSON, for example, the following patch will remove the tag "favoriteColour" and replace it:
1
{
2
"$type": "Patch",
3
"appliesTo": {
4
"type": "Patient",
5
"id": "9fdb557d-2e2f-4fe5-a193-e1cf8cf49bc4"
6
},
7
"change": [
8
{
9
"$type": "PatchOperation",
10
"path": "tag",
11
"op": "Remove",
12
"value": {
13
"$type": "EntityTag",
14
"key": "favoriteColor"
15
}
16
},
17
{
18
"$type": "PatchOperation",
19
"path": "tag",
20
"op": "Add",
21
"value": {
22
"$type": "EntityTag",
23
"key": "favoriteColor",
24
"value": "Blue"
25
}
26
}
27
]
28
}
Copied!

Dealing with Conflicts

You can assert that particular values are present or true prior to performing your patch, additionally the SanteDB service will assert on the etag of the If-Match header. For example, this patch will change the gender of the patient from MALE to FEMALE, but will only succeed if the etag matches e15be5a372b742ccac35518df7c9a784 and the gender is currently MALE.
1
PATCH /hdsi/Patient/9fdb557d-2e2f-4fe5-a193-e1cf8cf49bc4 HTTP/1.1
2
If-Match: e15be5a372b742ccac35518df7c9a784
3
Content-Type: application/json
4
5
{
6
"$type": "Patch",
7
"appliesTo": {
8
"type": "Patient",
9
"id": "9fdb557d-2e2f-4fe5-a193-e1cf8cf49bc4"
10
},
11
"change": [
12
{
13
"$type": "PatchOperation",
14
"op": "Test",
15
"path": "genderConcept",
16
"value": "f4e3a6bb-612e-46b2-9f77-ff844d971198"
17
},
18
{
19
"$type": "PatchOperation",
20
"path": "genderConcept",
21
"op": "Replace",
22
"value": "094941e9-a3db-48b5-862c-bc289bd7f86c"
23
}
24
]
25
}
Copied!
If the server does not have the same copy of the patient, then an HTTP 409 (CONFLICT) is returned to the caller.
You may override (or force) the patch to occur by using these sets of headers:
1
PATCH /hdsi/Patient/9fdb557d-2e2f-4fe5-a193-e1cf8cf49bc4
2
If-Match: e15be5a372b742ccac35518df7c9a784
3
Content-Type: application/json
4
X-Patch-Force: true
Copied!
Which will bypass the outcomes of checks.
It is best practice to not append the X-Patch-Force header unless a human is indicating that they would like to force the the patch to succeed.

Difference Operation

You can submit an object to the SanteDB API to get a "diff" of two objects. This operation is invoked using the $diff operation on the desired instance of the type. For example, to see the instructions it would take to turn patent 9fdb557d-2e2f-4fe5-a193-e1cf8cf49bc4 to 1c8c9aa3-abb8-48ee-992d-8f0c524ce40c submitting the following request.
1
POST /hdsi/Patient/9fdb557d-2e2f-4fe5-a193-e1cf8cf49bc4/$diff HTTP/1.1
2
Content-Type: application/json
3
4
{
5
"$type":"ApiOperationParameterCollection",
6
"parameter": [
7
{
8
"name":"other",
9
"value": "1c8c9aa3-abb8-48ee-992d-8f0c524ce40c"
10
}
11
]
12
}
Copied!
Last modified 4mo ago