Allow use of either rest-client or faraday#307
Conversation
rest-client is no longer actively maintained. faraday is popular, still active, and the code changes to make the switch should be minimal.
| def parse_json(data, encoding = 'UTF-8') | ||
| case data | ||
| when Faraday::Response | ||
| JSON.parse(data.body.force_encoding(encoding)) | ||
| else | ||
| JSON.parse(data.force_encoding(encoding)) | ||
| end |
There was a problem hiding this comment.
I realize this might not be the most ideal place to parse out a Faraday response body, but it's the place that had the least impact on the rest of the code. I'm open to moving this if we'd rather it not be here.
96a4d61 to
bb55f43
Compare
bb55f43 to
9cd1be4
Compare
|
I tested this in my own rails app and it works without any changes in the app, beyond updating the |
|
Thank your @armilam for this PR. Any thoughts on this, @runlevel5 ? Should we support faraday and rest-client at the same time for a while in case it breaks those existing apps? Maybe we can let the user choose which HTTP client to use? |
|
If you'd prefer to support both in a minor version update, I'd be happy to update this PR to do that. Personally I think it's worth a major version update to drop rest-client, but I understand if you'd want to reduct the impact of the change. |
|
Actually, I would like to make multiple HTTP clients support as a feature in the long term. And I think the faraday is good one for the default. But we still have to make rest-client as the default for a while. How do you think? |
|
That does make sense. I'll take a shot at adding the ability to configure which client to use, with Faraday as the default. |
|
Look forward to your good news! |
And move rest-client and faraday dependencies to dev_dependencies
This raises an error if the proper gem is not present
|
Well, I have an implementation that I think is good. I'm just trying to figure out how to run all the tests with each of the two http clients without having to go into each spec file and iterate over the clients. I've pushed my updates without an attempt to test with both clients. Working on trying to do it with matrixeval. |
| begin | ||
| require 'faraday' | ||
| rescue LoadError | ||
| end |
There was a problem hiding this comment.
We rescue this in case the project does not have faraday installed and is using rest-client instead. We do the same in the rest-client version of this class in case the opposite is true.
| s.add_dependency 'rest-client', '>= 1.8.0' | ||
|
|
||
| s.add_development_dependency 'rest-client', '>= 1.8.0' | ||
| s.add_development_dependency 'faraday', '>= 2.0.0' |
There was a problem hiding this comment.
We don't want to require both of these gems in everyone's projects, so we make them dev dependencies. We'll need to update documentation to say that one of these is required.
| Trello::HTTP_CLIENTS.each do |key, _client| | ||
| context "using #{key}" do | ||
| include_context "using #{key}" do |
There was a problem hiding this comment.
This is the only reliable way I've been able to get the tests to run with each of the http clients. I have not copied this pattern to the other test files yet in case someone has a better idea.
Ideally, we'd run all tests with Trello.http_client = 'faraday' and again with Trello.http_client = 'rest-client', except for a few that specifically test the configuration.
If this is the best pattern, I can copy it to the other test files, too.
|
I'm also having a bit of trouble getting matrixeval to run this - I could use some help there, please. |
Just fetched your code, the MatrixEval is working on my side. Any details about the issue you are meeting? BTW, I haven't checked the code details, I will find time to review it this week. |
|
It works when I run it on the master branch, but on this branch every variant fails with this error:
If it's working for you, maybe I'll try adding a variant for |
|
Okay, I added a matrixeval variant for the client. Let me know if it runs correctly. If it does, then it should cover both faraday and rest-client for all the tests that don't explicitly set the client. |
|
I got matrixeval working, and everything passes with ruby 2.6 and up. I think this is ready for review now, with the only work left being updating documentation. I'd be happy to take a shot at that if you're good with the code changes. |
|
Hi @armilam, thanks for your work. I just check your code. All looks good. But we still get something on hand:
# Gemfile
if RUBY_VERSION < '2.6.0'
gem 'faraday', '~> 1.0'
else
gem 'faraday', '~> 2.0'
endYou can run all tests with
Thanks again @armilam. |
|
Absolutely. Thanks for the feedback. I'll make those changes. |
- Update CI to run the tests against each http client - Use older Faraday for older ruby versions - Update the README to talk about the new http_client configuration
|
@hoppergee I've made the updates you requested. Please let me know if there's anything else to do. |
|
Thank you for developing so quickly! I will do a last check today and may release it in two days. |
|
Thank you! |
What does this PR do?
This adds a configuration option to allow apps to choose between
rest-clientandfaradayfor the http client.Highlights
config.http_clientis not set, the gem tries to usefaraday, thenrest-client. If neither is present, it raises and error.Trello::Client#get,#post,#put, and#deletefor consistency. It returns theTrello::Responseobject every time, and it stores the response body as a string on theTrello::Response#bodyso that the caller can access the response's status code, body, and headers reliably regardless of the http client used.Why are we doing this? Any context or related work?
The
rest-clientgem is no longer actively maintained and its dependencies are starting to fall behind. See this PR on the rest-client repo for context.Faraday is fairly commonly used, actively maintained, and has few dependencies, making it a good candidate for a replacement http library.
In order to continue supporting those using
rest-clientwithout forcing them to change, we allow either as an option, with the option to support more in the future.Where should a reviewer start?
Start by looking at:
The rest are just updating the tests.
Manual testing steps?
Since this impacts http calls, I would check that successful responses, error responses, and network-level errors (like timeouts) are still handled correctly, for each of
faradayandrest-client.