mamantoha / crest

HTTP and REST client for Crystal
https://mamantoha.github.io/crest/
MIT License
235 stars 14 forks source link

Repeating request on failure #202

Closed marcstein closed 1 year ago

marcstein commented 1 year ago

The application that I'm working on submits an authentication request and receives a token and then uses that token to submit a request for data. Sometimes, the auth succeeds but the main request times out. I want to trigger a repeat of the main request at that point.

I can catch the error and repeat the request, but that seems to cause an issue with the subsequent processes. Is there a recommended way to repeat on failure?

Thanks! Marc

mamantoha commented 1 year ago

Hi @marcstein .

I don't have recommended method of how to re-run a request if a timeout error occurs.

But you can use my solution which is used in one of my projects:

def make_request(url : String, params = {} of String => String)
  Retriable.retry(on: {Crest::InternalServerError, Crest::ServiceUnavailable}) do
    client[url].get(params: params)
  end
ensure
  client.http_client.close
end

It is used retriable.cr shard.

marcstein commented 1 year ago

Thanks so much!