Skip to content

Commit

Permalink
chore: add type hints
Browse files Browse the repository at this point in the history
Because they make me feel better even though pyld doesn't care lol

Signed-off-by: Daniel Bluhm <dbluhm@pm.me>
  • Loading branch information
dbluhm committed Nov 8, 2023
1 parent 2b64428 commit 814e40f
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions aries_cloudagent/vc/ld_proofs/document_downloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import string
from typing import Dict, Optional
import urllib.parse as urllib_parse
import importlib
from importlib import resources

import requests
from pyld import jsonld
Expand All @@ -28,7 +28,7 @@ def _load_jsonld_file(
"contentType": "application/ld+json",
"contextUrl": None,
"documentUrl": original_url,
"document": (importlib.resources.files(resource_path) / filename).read_text(),
"document": (resources.files(resource_path) / filename).read_text(),
}


Expand All @@ -44,7 +44,11 @@ class StaticCacheJsonLdDownloader:
"https://w3id.org/security/suites/ed25519-2020/v1": "ed25519-2020-context.jsonld",
}

def __init__(self, document_downloader=None, document_parser=None):
def __init__(
self,
document_downloader: Optional["JsonLdDocumentDownloader"] = None,
document_parser: Optional["JsonLdDocumentParser"] = None,
):
"""Load static document on initialization."""
self.documents_downloader = document_downloader or JsonLdDocumentDownloader()
self.document_parser = document_parser or JsonLdDocumentParser()
Expand All @@ -54,7 +58,7 @@ def __init__(self, document_downloader=None, document_parser=None):
for url, filename in StaticCacheJsonLdDownloader.CONTEXT_FILE_MAPPING.items()
}

def load(self, url, options=None):
def load(self, url: str, options: Optional[Dict] = None):
"""Load a jsonld document from URL.
Prioritize local static cache before attempting to download from the URL.
Expand All @@ -68,15 +72,15 @@ def load(self, url, options=None):
logger.debug("Context %s not in static cache, resolving from URL.", url)
return self._live_load(url, options)

def _live_load(self, url, options=None):
def _live_load(self, url: str, options: Optional[Dict] = None):
doc, link_header = self.documents_downloader.download(url, options)
return self.document_parser.parse(doc, link_header)


class JsonLdDocumentDownloader:
"""JsonLd documents downloader."""

def download(self, url: str, options: Dict, **kwargs):
def download(self, url: str, options: Optional[Dict], **kwargs):
"""Retrieves JSON-LD at the given URL.
This was lifted from pyld.documentloader.requests.
Expand Down

0 comments on commit 814e40f

Please sign in to comment.