summaryrefslogtreecommitdiff
path: root/lib/license_finder/package_utils/pypi.rb
blob: 30856bd9eb03f3e373fbbddfa01aff8a47758335 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# frozen_string_literal: true

require 'net/hippie'

module LicenseFinder
  class PyPI
    class << self
      def definition(name, version)
        response = request("https://pypi.org/pypi/#{name}/#{version}/json")
        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