diff options
| author | mo khan <mo.khan@gmail.com> | 2020-01-13 17:04:13 -0700 |
|---|---|---|
| committer | mo khan <mo.khan@gmail.com> | 2020-01-15 15:03:07 -0700 |
| commit | 1cc6c02f2f34252a85d354741db0a80d0f199286 (patch) | |
| tree | bcca242328ba09fbf3d6d5ff53b63df9af4c3b18 | |
| parent | 8ec78f9142577f9f6ed73d4dd5ac0a3e5f02a5c6 (diff) | |
Use net/hippie to follow redirects
| -rw-r--r-- | Gemfile.lock | 2 | ||||
| -rw-r--r-- | lib/license_finder/package_utils/pypi.rb | 12 |
2 files changed, 6 insertions, 8 deletions
diff --git a/Gemfile.lock b/Gemfile.lock index 0a91a04..3c700a8 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -16,7 +16,7 @@ GEM toml (= 0.2.0) with_env (= 1.1.0) xml-simple - net-hippie (0.2.7) + net-hippie (0.3.0) parslet (1.8.2) rspec (3.9.0) rspec-core (~> 3.9.0) diff --git a/lib/license_finder/package_utils/pypi.rb b/lib/license_finder/package_utils/pypi.rb index 30856bd..cfe3d66 100644 --- a/lib/license_finder/package_utils/pypi.rb +++ b/lib/license_finder/package_utils/pypi.rb @@ -6,17 +6,15 @@ module LicenseFinder class PyPI class << self def definition(name, version) - response = request("https://pypi.org/pypi/#{name}/#{version}/json") + url = "https://pypi.org/pypi/#{name}/#{version}/json" + response = Net::Hippie::Client.new.tap do |client| + client.follow_redirects = 3 + client.with_retry { |x| x.get(url) } + end response.is_a?(Net::HTTPSuccess) ? JSON.parse(response.body).fetch('info', {}) : {} rescue *Net::Hippie::CONNECTION_ERRORS {} end - - def request(url, limit = 10) - client = Net::Hippie::Client.new - response = client.with_retry { client.get(url) } - response.is_a?(Net::HTTPRedirection) && limit.positive? ? request(response['location'], limit - 1) : response - end end end end |
