-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add CmrQuery storage class as a convenience around HttpRequest
- Loading branch information
Showing
5 changed files
with
112 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import urllib.parse | ||
from dataclasses import InitVar, dataclass | ||
from typing import Optional | ||
|
||
from mandible.metadata_mapper.context import Context | ||
|
||
from .http_request import HttpRequest | ||
|
||
|
||
@dataclass | ||
class CmrQuery(HttpRequest): | ||
"""A convenience class for setting neccessary CMR parameters""" | ||
|
||
url: InitVar[None] = None | ||
|
||
base_url: str = "" | ||
path: str = "" | ||
format: str = "" | ||
token: Optional[str] = None | ||
|
||
def __post_init__(self, url: str): | ||
if url: | ||
raise ValueError( | ||
"do not set 'url' directly, use 'base_url' and 'path' instead", | ||
) | ||
|
||
def _get_override_request_args(self, context: Context) -> dict: | ||
return { | ||
"headers": self._get_headers(), | ||
"url": self._get_url(), | ||
} | ||
|
||
def _get_headers(self) -> Optional[dict]: | ||
if self.token is None: | ||
return self.headers | ||
|
||
return { | ||
**(self.headers or {}), | ||
"Authorization": self.token, | ||
} | ||
|
||
def _get_url(self) -> str: | ||
path = self.path | ||
if self.format: | ||
path = f"{self.path}.{self.format.lower()}" | ||
return urllib.parse.urljoin(self.base_url, path) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters