Skip to content

Allow use of either rest-client or faraday#307

Merged
hoppergee merged 16 commits into
jeremytregunna:masterfrom
armilam:switch-to-faraday
May 30, 2022
Merged

Allow use of either rest-client or faraday#307
hoppergee merged 16 commits into
jeremytregunna:masterfrom
armilam:switch-to-faraday

Conversation

@armilam
Copy link
Copy Markdown
Contributor

@armilam armilam commented May 13, 2022

What does this PR do?

This adds a configuration option to allow apps to choose between rest-client and faraday for the http client.

Trello.configure do |config|
  config.http_client = 'faraday'
  # or
  config.http_client = 'rest-client'
end

Highlights

  • It's written in a way to allow for supporting more http clients in the future.
  • If config.http_client is not set, the gem tries to use faraday, then rest-client. If neither is present, it raises and error.
  • It changes the return value of Trello::Client#get, #post, #put, and #delete for consistency. It returns the Trello::Response object every time, and it stores the response body as a string on the Trello::Response#body so 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-client gem 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-client without 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 faraday and rest-client.

armilam added 3 commits May 13, 2022 15:41
rest-client is no longer actively maintained. faraday is popular,
still active, and the code changes to make the switch should be minimal.
Comment thread lib/trello/json_utils.rb
Comment on lines +51 to +57
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
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread lib/trello/net.rb Outdated
@armilam armilam force-pushed the switch-to-faraday branch from 96a4d61 to bb55f43 Compare May 13, 2022 20:05
@armilam armilam force-pushed the switch-to-faraday branch from bb55f43 to 9cd1be4 Compare May 13, 2022 20:07
@armilam
Copy link
Copy Markdown
Contributor Author

armilam commented May 13, 2022

I tested this in my own rails app and it works without any changes in the app, beyond updating the ruby-trello gem.

@hoppergee
Copy link
Copy Markdown
Collaborator

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?

@armilam
Copy link
Copy Markdown
Contributor Author

armilam commented May 16, 2022

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.

@hoppergee
Copy link
Copy Markdown
Collaborator

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?

@armilam
Copy link
Copy Markdown
Contributor Author

armilam commented May 17, 2022

That does make sense. I'll take a shot at adding the ability to configure which client to use, with Faraday as the default.

@hoppergee
Copy link
Copy Markdown
Collaborator

Look forward to your good news!

@armilam
Copy link
Copy Markdown
Contributor Author

armilam commented May 17, 2022

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.

Comment thread lib/trello/net/faraday.rb
Comment on lines +5 to +8
begin
require 'faraday'
rescue LoadError
end
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread ruby-trello.gemspec Outdated
Comment on lines +27 to +29
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'
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines +8 to +10
Trello::HTTP_CLIENTS.each do |key, _client|
context "using #{key}" do
include_context "using #{key}" do
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@armilam
Copy link
Copy Markdown
Contributor Author

armilam commented May 18, 2022

I'm also having a bit of trouble getting matrixeval to run this - I could use some help there, please.

@hoppergee
Copy link
Copy Markdown
Collaborator

hoppergee commented May 18, 2022

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.

@armilam
Copy link
Copy Markdown
Contributor Author

armilam commented May 18, 2022

It works when I run it on the master branch, but on this branch every variant fails with this error:

Bundler::GemNotFound: Could not find gem 'rest-client (>= 1.8.0)' in locally installed gems.

If it's working for you, maybe I'll try adding a variant for http_client_gem.

@armilam
Copy link
Copy Markdown
Contributor Author

armilam commented May 18, 2022

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.

@armilam
Copy link
Copy Markdown
Contributor Author

armilam commented May 18, 2022

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.

@armilam armilam changed the title Switch from RestClient to Faraday Allow use of either rest-client or faraday May 19, 2022
@hoppergee
Copy link
Copy Markdown
Collaborator

Hi @armilam, thanks for your work. I just check your code. All looks good. But we still get something on hand:

  • Update the .github/workflows/main.yml to include faraday and reset-client
  • Can you help to pass the tests under Ruby 2.5 with something like this in Gemfile.
# Gemfile
if RUBY_VERSION  < '2.6.0'
  gem 'faraday', '~> 1.0'
else
  gem 'faraday', '~> 2.0'
end

You can run all tests with meval -a bundle install and meval -a rspec. Or run test for a specific circumstance like this meval --ruby 2.5 --active_model 6.0 --http_client_gem faraday rspec

  • Can you help to bring the rest-client back as the default in both codebase and gemspec? I want to release this update with a minor version. Then set faraday as the default in next major version.
  • Update the README about this new feature.

Thanks again @armilam.

@armilam
Copy link
Copy Markdown
Contributor Author

armilam commented May 27, 2022

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
@armilam
Copy link
Copy Markdown
Contributor Author

armilam commented May 27, 2022

@hoppergee I've made the updates you requested. Please let me know if there's anything else to do.

@hoppergee
Copy link
Copy Markdown
Collaborator

Thank you for developing so quickly! I will do a last check today and may release it in two days.

@hoppergee hoppergee merged commit 9312d24 into jeremytregunna:master May 30, 2022
@armilam
Copy link
Copy Markdown
Contributor Author

armilam commented May 31, 2022

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants