Faraday - Using Backup Host for Remote Requests
Nowadays almost every Rails application interact with the remote service or API. The problem is that you can’t fully trust those services and you forced to add a lot of test coverage and exception handling for this part of code. Once i worked with the API that had mirror server for case when the main one is unavailable. I write a middleware for Faraday, which will use backup host(provided by user) if the original one is unresponsive.
First of all, what is Faraday? It’s an awesome HTTP client library that provides a common interface over many adapters (such as Net::HTTP) and embraces the concept of Rack middleware when processing the request/response cycle. Middleware are classes that implement a call instance method. They hook into the request/response cycle. Our middleware will be hooked to the request cycle.
Now to our middleware. All we need from it is simple checking for errors, such as
Faraday::ConnectionFailed. If some errors from the list were araised, than we should switch request to the backup server. So let’s jump to the code:
1 2 3 4 5 6 7 8 9 10 11 12
call is the main method for almost every faraday middleware. Here we just trying to catch any exception from the list, and switch the url of request if the exception was raised.
You can pass exception that you want to the
@errmatch list. Here is the list of defaults:
1 2 3 4
In the end you can use this middleware like this:
1 2 3 4 5
You can grab this middleware from my repository.