diff options
| author | Can Eldem <celdem@gitlab.com> | 2020-01-16 10:24:11 +0000 |
|---|---|---|
| committer | Can Eldem <celdem@gitlab.com> | 2020-01-16 10:24:11 +0000 |
| commit | 2074e7e5ea3012be6f3a72bd4af934a42b7202ca (patch) | |
| tree | ce9ec33580f0e6e3f9cf3df1aab2f2cf7b5601cb /lib/license/management/python/pypi.rb | |
| parent | f4f59927f87944a4d73be26416a7334521875f40 (diff) | |
| parent | 34f162a4903d852d47bd5440839f7519eb6fa8f0 (diff) | |
Merge branch '12012-pipfile-lock' into 'master'v2.4.0
Add support for Pipfile.lock
See merge request gitlab-org/security-products/license-management!103
Diffstat (limited to 'lib/license/management/python/pypi.rb')
| -rw-r--r-- | lib/license/management/python/pypi.rb | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/lib/license/management/python/pypi.rb b/lib/license/management/python/pypi.rb new file mode 100644 index 0000000..0397532 --- /dev/null +++ b/lib/license/management/python/pypi.rb @@ -0,0 +1,44 @@ +# frozen_string_literal: true + +require 'net/hippie' + +module License + module Management + class PyPI + include Loggable + + def initialize(http) + @http = http + end + + def definition_for(name, version) + uri = "https://pypi.org/pypi/#{name}/#{version}/json" + process(@http.with_retry { |client| client.get(uri) }).tap do |definition| + log_info([name, version, definition["license"]].inspect) + end + rescue *Net::Hippie::CONNECTION_ERRORS + {} + end + + class << self + def definition(name, version) + @pypi ||= new(License::Management.http) + @pypi.definition_for(name, version) + end + end + + private + + def process(response) + return JSON.parse(response.body).fetch('info', {}) if ok?(response) + + log_error([response.class, response.code, response.body].inspect) + {} + end + + def ok?(response) + response.is_a?(Net::HTTPSuccess) + end + end + end +end |
