|
| 1 | +import logging |
| 2 | + |
| 3 | +from attrs import asdict |
| 4 | +from django.conf import settings |
| 5 | +from django.contrib.sites.models import Site |
| 6 | + |
| 7 | +from credentials.apps.badges.accredible.data import AccredibleBadgeData, AccredibleExpireBadgeData |
| 8 | +from credentials.apps.badges.accredible.exceptions import AccredibleError |
| 9 | +from credentials.apps.badges.accredible.utils import get_accredible_api_base_url |
| 10 | +from credentials.apps.badges.base_api_client import BaseBadgeProviderClient |
| 11 | +from credentials.apps.badges.models import AccredibleAPIConfig, AccredibleGroup |
| 12 | + |
| 13 | + |
| 14 | +logger = logging.getLogger(__name__) |
| 15 | + |
| 16 | + |
| 17 | +class AccredibleAPIClient(BaseBadgeProviderClient): |
| 18 | + """ |
| 19 | + A client for interacting with the Accredible API. |
| 20 | +
|
| 21 | + This class provides methods for performing various operations on the Accredible API. |
| 22 | + """ |
| 23 | + |
| 24 | + PROVIDER_NAME = "Accredible" |
| 25 | + |
| 26 | + def __init__(self, api_config_id: int): |
| 27 | + """ |
| 28 | + Initializes a AccredibleAPIClient object. |
| 29 | +
|
| 30 | + Args: |
| 31 | + api_config (AccredibleAPIConfig): Configuration object for the Accredible API. |
| 32 | + """ |
| 33 | + |
| 34 | + self.api_config_id = api_config_id |
| 35 | + self.api_config = self.get_api_config() |
| 36 | + |
| 37 | + def get_api_config(self) -> AccredibleAPIConfig: |
| 38 | + """ |
| 39 | + Returns the API configuration object for the Accredible API. |
| 40 | + """ |
| 41 | + try: |
| 42 | + return AccredibleAPIConfig.objects.get(id=self.api_config_id) |
| 43 | + except AccredibleAPIConfig.DoesNotExist: |
| 44 | + raise AccredibleError(f"AccredibleAPIConfig with the id {self.api_config_id} does not exist!") |
| 45 | + |
| 46 | + def _get_base_api_url(self) -> str: |
| 47 | + return get_accredible_api_base_url(settings) |
| 48 | + |
| 49 | + def _get_headers(self) -> dict: |
| 50 | + """ |
| 51 | + Returns the headers for making API requests to Accredible. |
| 52 | + """ |
| 53 | + return { |
| 54 | + "Accept": "application/json", |
| 55 | + "Content-Type": "application/json", |
| 56 | + "Authorization": f"Bearer {self.api_config.api_key}", |
| 57 | + } |
| 58 | + |
| 59 | + def fetch_all_groups(self) -> dict: |
| 60 | + """ |
| 61 | + Fetch all groups. |
| 62 | + """ |
| 63 | + return self.perform_request("get", "issuer/all_groups") |
| 64 | + |
| 65 | + def fetch_design_image(self, design_id: int) -> str: |
| 66 | + """ |
| 67 | + Fetches the design and return the URL of image. |
| 68 | + """ |
| 69 | + design_raw = self.perform_request("get", f"designs/{design_id}") |
| 70 | + return design_raw.get("design", {}).get("rasterized_content_url") |
| 71 | + |
| 72 | + def issue_badge(self, issue_badge_data: AccredibleBadgeData) -> dict: |
| 73 | + """ |
| 74 | + Issues a badge using the Accredible REST API. |
| 75 | +
|
| 76 | + Args: |
| 77 | + issue_badge_data (IssueBadgeData): Data required to issue the badge. |
| 78 | + """ |
| 79 | + return self.perform_request("post", "credentials", asdict(issue_badge_data)) |
| 80 | + |
| 81 | + def revoke_badge(self, badge_id, data: AccredibleExpireBadgeData) -> dict: |
| 82 | + """ |
| 83 | + Revoke a badge with the given badge ID. |
| 84 | +
|
| 85 | + Args: |
| 86 | + badge_id (str): ID of the badge to revoke. |
| 87 | + data (dict): Additional data for the revocation. |
| 88 | + """ |
| 89 | + return self.perform_request("patch", f"credentials/{badge_id}", asdict(data)) |
| 90 | + |
| 91 | + def sync_groups(self, site_id: int) -> int: |
| 92 | + """ |
| 93 | + Pull all groups for a given Accredible API config. |
| 94 | +
|
| 95 | + Args: |
| 96 | + site_id (int): ID of the site. |
| 97 | +
|
| 98 | + Returns: |
| 99 | + int | None: processed items. |
| 100 | + """ |
| 101 | + try: |
| 102 | + site = Site.objects.get(id=site_id) |
| 103 | + except Site.DoesNotExist: |
| 104 | + logger.error(f"Site with the id {site_id} does not exist!") |
| 105 | + raise |
| 106 | + |
| 107 | + groups_data = self.fetch_all_groups() |
| 108 | + raw_groups = groups_data.get("groups", []) |
| 109 | + |
| 110 | + all_group_ids = [group.get("id") for group in raw_groups] |
| 111 | + AccredibleGroup.objects.exclude(id__in=all_group_ids).delete() |
| 112 | + |
| 113 | + for raw_group in raw_groups: |
| 114 | + AccredibleGroup.objects.update_or_create( |
| 115 | + id=raw_group.get("id"), |
| 116 | + api_config=self.api_config, |
| 117 | + defaults={ |
| 118 | + "site": site, |
| 119 | + "name": raw_group.get("course_name"), |
| 120 | + "description": raw_group.get("course_description"), |
| 121 | + "icon": self.fetch_design_image(raw_group.get("primary_design_id")), |
| 122 | + "created": raw_group.get("created_at"), |
| 123 | + "state": AccredibleGroup.STATES.active, |
| 124 | + }, |
| 125 | + ) |
| 126 | + |
| 127 | + return len(raw_groups) |
0 commit comments