From 8b7f90d514fee0a244c6a1fc219b09053e97d659 Mon Sep 17 00:00:00 2001 From: Alex Chan Date: Wed, 12 Jun 2024 11:00:19 +0100 Subject: [PATCH] Add some notes on Retry-After and ETag --- .../2024-02-28-get-netlify-usage-from-api.md | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/src/_til/2024/2024-02-28-get-netlify-usage-from-api.md b/src/_til/2024/2024-02-28-get-netlify-usage-from-api.md index bae29eb12..7b620c7dd 100644 --- a/src/_til/2024/2024-02-28-get-netlify-usage-from-api.md +++ b/src/_til/2024/2024-02-28-get-netlify-usage-from-api.md @@ -1,6 +1,7 @@ --- layout: til date: 2024-02-28 10:15:24 +0000 +date_updated: 2024-06-12 10:58:07 +0100 title: Get my Netlify bandwidth usage from the API tags: - netlify @@ -43,3 +44,34 @@ A couple of observations on the data you get back: * This value gets cached for a while -- in the example above, I called the API at 15:22 but the `last_updated_at` value is 15:03. I'm not sure how often the value is updated. + +## Being a responsible user of the API + +* The API returns a `Retry-After` value, which is a minute after your request. + This is a clue about how often the value gets updated -- it's definitely not second-level granularity. + + ```console + $ curl -v -H 'Authorization: Bearer ' 'https://api.netlify.com/api/v1/accounts//bandwidth' + … + < HTTP/2 200 + < date: Wed, 12 Jun 2024 09:52:37 GMT + … + < retry-after: 2024-06-12 09:53:37 UTC + … + ``` + +* The responses include an `ETag` header, which you can use to check if the value has changed since the last request: + + ```console + $ curl -v -H 'Authorization: Bearer ' 'https://api.netlify.com/api/v1/accounts//bandwidth' + … + < HTTP/2 200 + < etag: W/"190b2e3e6d8c239bd546819b0f0b1eb9" + … + + $ curl -v -H 'If-None-Match: W/"190b2e3e6d8c239bd546819b0f0b1eb9"' -H 'Authorization: Bearer ' 'https://api.netlify.com/api/v1/accounts//bandwidth' + < HTTP/2 304 + … + ``` + + Annoyingly, this doesn't make the API any faster -- responses still take about half a second, even if nothing has changed.