pypi_json

PyPI JSON API client library.

Classes:

PyPIJSON([endpoint, auth, session])

A client for fetching package information from a Python JSON API.

ProjectMetadata(info, last_serial[, …])

Represents a project’s metadata from the PyPI JSON API.

Data:

USER_AGENT

The User-Agent header used for requests; not used when the user provides their own session object.

class PyPIJSON(endpoint='https://pypi.org/pypi', auth=None, session=None)[source]

A client for fetching package information from a Python JSON API.

If necessary, login/authentication details for the repository can be specified at initialization by setting the auth parameter to either a (username, password) pair or another authentication object accepted by requests.

If more complicated session configuration is desired (e.g., setting up caching), the user must create and configure a requests.Session object appropriately and pass it to the constructor as the session parameter.

A PyPIJSON instance can be used as a context manager that will automatically close its session on exit, regardless of where the session object came from.

Parameters

Attributes:

timeout

The timeout for HTTP requests, in seconds.

endpoint

The apeye.requests_url.TrailingRequestsURL object representing the PyPI JSON API, with an authenticated requests session.

endpoint_url

The URL of the JSON API endpoint.

Methods:

__repr__()

Returns a string representation of the PyPIJSON object.

get_metadata(project[, version])

Returns metadata for the given project on PyPI.

download_file(url)

Download the file with the given URL from PyPI.

timeout = 10

Type:    ClassVar[int]

The timeout for HTTP requests, in seconds.

New in version 0.1.1.

endpoint

Type:    TrailingRequestsURL

The apeye.requests_url.TrailingRequestsURL object representing the PyPI JSON API, with an authenticated requests session.

property endpoint_url

The URL of the JSON API endpoint.

Return type

str

__repr__()[source]

Returns a string representation of the PyPIJSON object.

Return type

str

get_metadata(project, version=None)[source]

Returns metadata for the given project on PyPI.

Parameters
Raises
Return type

ProjectMetadata

download_file(url)[source]

Download the file with the given URL from PyPI.

Parameters

url (Union[str, URL])

Return type

Response

namedtuple ProjectMetadata(info, last_serial, releases=None, urls=(), vulnerabilities=[])[source]

Bases: NamedTuple

Represents a project’s metadata from the PyPI JSON API.

Fields
  1.  info (ProjectInfoDict) – Generic information about a specific version of a project.

  2.  last_serial (int) – Monotonically increasing integer sequence that changes every time the project is updated.

  3.  releases (Optional[Dict[str, List[DistributionPackageDict]]]) – A mapping of version numbers to a list of artifacts associated with a version. .. versionchanged:: 0.3.0 Can now be None, as the JSON API no longer includes this key in queries for a specific version.

  4.  urls (List[DistributionPackageDict]) – A list of release artifacts associated with this version.

  5.  vulnerabilities (List[VulnerabilityInfoDict]) – Details of vulnerabilities from the Open Source Vulnerabilities project. (New in version 0.2.0)

Attributes:

name

Return the normalized project name.

version

Return the release version.

Methods:

get_latest_version()

Returns the version number of the latest release on PyPI for this project.

get_releases_with_digests()

Returns a dictionary mapping PyPI release versions to download URLs and the sha256sum of the file contents.

get_releases()

Returns a dictionary mapping PyPI release versions to download URLs.

get_wheel_tag_mapping([version])

Constructs a mapping of wheel tags to the download URL of the wheel with relevant tag.

property name

Return the normalized project name.

Return type

str

property version

Return the release version.

Return type

Version

get_latest_version()[source]

Returns the version number of the latest release on PyPI for this project.

Version numbers are sorted using the rules in PEP 386.

Return type

Version

get_releases_with_digests()[source]

Returns a dictionary mapping PyPI release versions to download URLs and the sha256sum of the file contents.

Return type

Dict[str, List[FileURL]]

get_releases()[source]

Returns a dictionary mapping PyPI release versions to download URLs.

Return type

Dict[str, List[str]]

get_wheel_tag_mapping(version=None)[source]

Constructs a mapping of wheel tags to the download URL of the wheel with relevant tag.

This can be used alongside packaging.tags.sys_tags() to select the best wheel for the current platform.

Parameters

version (Union[str, int, Version, None]) – The version to return the mapping for. If None the current version is used. Default None.

Return type

Tuple[Dict[Tag, URL], List[URL]]

Returns

A tuple containing the tag: url mapping, and a list of download URLs for non-wheel artifacts (e.g. sdists).

USER_AGENT

Type:    str

The User-Agent header used for requests; not used when the user provides their own session object.

The structure of the User-Agent is:

' '.join([
        f"pypi-json/{__version__} (https://github.com/repo-helper/pypi-json)",
        f"requests/{requests.__version__}",
        f"{platform.python_implementation()}/{platform.python_version()}",
        #  ^^^ e.g. CPython                   ^^^ e.g. 3.8.10
        ])

This global attribute should not be changed by other code.