-
Notifications
You must be signed in to change notification settings - Fork 32
Description
Hi!
I was wondering how to make sure in SciCat that updates don't get lost when multiple clients access the same resource?
What I wanted to do uses the origdatablocks endpoint, but probably extends to the other PATCH endpoints.
I noticed the Etag header was set, and I tried supplying If-Match and If-Unmodified-Since, but as far as I now understand after having a look, the default behaviour in the etag library is to only hash the response value for caching, and you have to implement the logic for these headers yourself (or I'm doing something wrong).
Is there another way that I have missed? The search didn't turn up issues/pr's.
Best
cchndl
Here is a python test script, using the instruments endpoint since it's more compact than origdatablocks/datasets:
import requests
# setup
url = ""
instrument = {"uniqueName": "test-instrument", "name": "TBD"}
credentials = {"username": "", "password": ""}
r = requests.post(f"{url}/api/v3/auth/login", json=credentials)
headers = {"Authorization": "Bearer " + r.json().get("access_token")}
r = requests.post(f"{url}/api/v3/instruments/", headers=headers, json=instrument)
instr_id = r.json()["pid"]
# get the initial instrument state
r = requests.get(f"{url}/api/v3/instruments/{instr_id}", headers=headers)
not_modified_headers = headers.copy()
not_modified_headers["If-Match"] = r.headers.get("etag")
not_modified_headers["If-Unmodified-Since"] = r.headers.get("date")
intermediate_update = {"name": "intermediate"}
conditional_update = {"name": "conditional"}
# do an unconditional update
requests.patch(
f"{url}/api/v3/instruments/{instr_id}",
headers=headers,
json=intermediate_update,
)
# this should only happen if the resource has not changed since the GET request, no?
r = requests.patch(
f"{url}/api/v3/instruments/{instr_id}",
headers=not_modified_headers,
json=conditional_update,
)
r = requests.get(f"{url}/api/v3/instruments/{instr_id}", headers=headers)
print(r.json().get("name")) # will print "conditional"