registry_tracker/src/upstream.py
Elia el Lazkani d8e7035369 chore(): Improves code features
* Upgrades python to 3.11.3
* Upgrade poetry configuration
* Upgrade project packages
* Introduces Upstream class
* Introduces an improved Registry class
* Introduces a common library for serialization
* Introduces Tags and Tag classes
* Introduces Images and Image classes
2023-06-03 21:53:37 +02:00

43 lines
1.4 KiB
Python

import json
from src.connection import Requests
class Upstream:
"""
Registry class.
"""
def __init__(
self,
url: str = "https://hub.docker.com",
username: str = None,
password: str = None,
) -> None:
"""
Initialize the upstream registry object.
"""
self.url = url
self.req = Requests()
if username and password:
self.req.get_token(url + "/v2/users/login", username, password)
def get_tags(self, image, namespace: str = "library", url_params: str = None) -> dict:
"""
Method which returns the tags page in a dictionary.
"""
if url_params:
url = "{registry_url}/v2/namespaces/{namespace}/repositories/{repository}/tags?{url_params}".format(
registry_url=self.url, namespace=namespace, repository=image, url_params=url_params
)
else:
url = "{registry_url}/v2/namespaces/{namespace}/repositories/{repository}/tags".format(
registry_url=self.url, namespace=namespace, repository=image
)
try:
result = self.req.get(url, headers=self.req.get_headers())
except Exception as e:
print(e)
if not result.status_code == 200:
raise Exception("Could not get tags from server")
return result.json()["results"]