From 0e1c4f4ae2c89a367e4fa31c4fc28ae9de00eef4 Mon Sep 17 00:00:00 2001 From: korelstar Date: Sat, 13 Mar 2021 09:44:33 +0100 Subject: [PATCH] API: ETag/If-None-Match for Get single note --- docs/api/v1.md | 2 ++ lib/Controller/NotesApiController.php | 5 ++++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/docs/api/v1.md b/docs/api/v1.md index 07b88131e..c59f43bfb 100644 --- a/docs/api/v1.md +++ b/docs/api/v1.md @@ -81,9 +81,11 @@ No valid authentication credentials supplied. | Parameter | Type | Description | |:------|:-----|:-----| | `id` | integer, required (path) | ID of the note to query. | +| `If-None-Match` | HTTP header, optional | Use this in order to reduce transferred data size (see [HTTP ETag](https://en.wikipedia.org/wiki/HTTP_ETag)). You should use the value from the note's attribute `etag` or from the last request's HTTP response header `ETag`. | 1.2 | #### Response ##### 200 OK +- **HTTP Header**: `ETag` (see [HTTP ETag](https://en.wikipedia.org/wiki/HTTP_ETag)). The value is identical to the note's attribute `etag` (see section [Note attributes](#note-attributes)). - **Body**: note (see section [Note attributes](#note-attributes)), example: ```js { diff --git a/lib/Controller/NotesApiController.php b/lib/Controller/NotesApiController.php index 63f6846be..569a531bd 100644 --- a/lib/Controller/NotesApiController.php +++ b/lib/Controller/NotesApiController.php @@ -63,7 +63,10 @@ public function get(int $id, string $exclude = '') : JSONResponse { return $this->helper->handleErrorResponse(function () use ($id, $exclude) { $exclude = explode(',', $exclude); $note = $this->service->get($this->helper->getUID(), $id); - return $this->helper->getNoteData($note, $exclude); + $noteData = $this->helper->getNoteData($note, $exclude); + return (new JSONResponse($noteData)) + ->setETag($noteData['etag']) + ; }); }