|
| 1 | +import logging |
| 2 | +from typing import Tuple |
| 3 | + |
| 4 | +import requests |
| 5 | + |
| 6 | +logger = logging.getLogger(__name__) |
| 7 | + |
| 8 | + |
| 9 | +class DockerHubService: |
| 10 | + """ |
| 11 | + Handles read and write DockerHub API operations |
| 12 | + """ |
| 13 | + |
| 14 | + def __init__(self, user_name: str, image_name: str) -> None: |
| 15 | + self.user_name = user_name |
| 16 | + self.image_name = image_name |
| 17 | + |
| 18 | + def get_dockerhub_image_stats(self) -> Tuple[int, int]: |
| 19 | + api_url = f"https://hub.docker.com/v2/repositories/{self.user_name}/{self.image_name}/" |
| 20 | + |
| 21 | + try: |
| 22 | + response = requests.get(api_url) |
| 23 | + except Exception as e: |
| 24 | + logger.error(f"Error: Unable to fetch data. Exception: {e}") |
| 25 | + raise |
| 26 | + |
| 27 | + if response.status_code == 200: |
| 28 | + image_data = response.json() |
| 29 | + pull_count = image_data["pull_count"] |
| 30 | + star_count = image_data["star_count"] |
| 31 | + return pull_count, star_count |
| 32 | + else: |
| 33 | + logger.error( |
| 34 | + f"Error: Unable to fetch data. Status Code: {response.status_code}" |
| 35 | + ) |
| 36 | + raise |
| 37 | + |
| 38 | + |
| 39 | +if __name__ == "__main__": |
| 40 | + user_name = "localstack" |
| 41 | + image_name = "localstack" |
| 42 | + |
| 43 | + LocalStackDockerHubService = DockerHubService(user_name, image_name) |
| 44 | + pull_count, star_count = LocalStackDockerHubService.get_dockerhub_image_stats() |
| 45 | + |
| 46 | + if pull_count is not None and star_count is not None: |
| 47 | + print(f"Pull Count: {pull_count}") |
| 48 | + print(f"Star Count: {star_count}") |
0 commit comments