glance icon indicating copy to clipboard operation
glance copied to clipboard

add wait-last-request for custom-api

Open ralphocdol opened this issue 7 months ago • 2 comments

For https://github.com/glanceapp/glance/issues/568

Config example:

- type: custom-api
  url: https:${WEBSERVER_URL}/data          # Replaces the value of the key timestamp inside file data.json with the current timestamp
  method: post                              # Notice post
  skip-timeout-error: true                  # Example in the sub2, this should skip timeout error whenever it reaches it as the data from this request isn't needed
  subrequests:
    sub1:
      url: https:${WEBSERVER_URL}/data          # A get request the fetches a file data.json that has a timestamp key
      wait-last-request: true                   # The feature added, this waits for the previous request
    sub2:
      url: https:${WEBSERVER_URL}/slow          # Just a request that finishes up after 10s to simulate timeout
      skip-timeout-error: true                  # Just an example
  cache: 1s
  template: |
    <div><pre>POST:     {{ (.Subrequest "sub1").JSON.String "timestamp" }}</pre></div>
    <div><pre>GET:      {{ .JSON.String "timestamp" }}</pre></div>
    <div><pre>Matched?: {{ eq (.JSON.String "timestamp") ((.Subrequest "sub1").JSON.String "timestamp") }}</pre></div>
    <div><pre>SUB2:     {{ (.Subrequest "sub2").Response.Status }}</pre></div>

Output:

POST:     1744133075172
GET:      1744133075172
Matched?: true
SUB2:     Get "https://<redacted>/slow": context deadline exceeded (Client.Timeout exceeded while awaiting headers)

Setting wait-last-request: false or not setting it at all would make the Matched?: value randomized (GET would get the previous data before the POST happened or GET the latest POST data) since requests are concurrent and would it be unpredictable.

[!CAUTION]

Multiple SUCCESSIVE usage of this property in the same widget can slow your page load time depending on the API's response by another 1~5 seconds (timeout limit) each wait-last-request.

ralphocdol avatar Apr 08 '25 03:04 ralphocdol

Hi @svilenmarkov are there any plans to merge this PR in the near future? Thanks

anant-j avatar Apr 29 '25 00:04 anant-j

@anant-j I'll add something that would allow you to achieve the same result in a more imperative way in v0.8.

svilenmarkov avatar Apr 30 '25 07:04 svilenmarkov

Released: https://github.com/glanceapp/glance/releases/tag/v0.8.0

ralphocdol avatar May 14 '25 14:05 ralphocdol