-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a9a99ad
commit 3ac1a1e
Showing
38 changed files
with
1,468 additions
and
136 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 |
---|---|---|
@@ -1 +1,2 @@ | ||
from .domain import * | ||
from .models import * | ||
from .client import Client |
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,66 @@ | ||
import abc | ||
import typing | ||
|
||
|
||
class Auth(metaclass=abc.ABCMeta): | ||
|
||
@abc.abstractmethod | ||
def apply( | ||
self, | ||
headers: dict, | ||
params: dict, | ||
data: typing.Optional[dict], | ||
): | ||
... | ||
|
||
|
||
class NoneAuth(Auth): | ||
|
||
def apply( | ||
self, | ||
headers: dict, | ||
params: dict, | ||
data: typing.Optional[dict], | ||
): | ||
pass | ||
|
||
|
||
class ApiKeyAuth(Auth): | ||
|
||
def __init__( | ||
self, | ||
key: str | ||
): | ||
super().__init__() | ||
|
||
self._key = key | ||
|
||
def apply( | ||
self, | ||
headers: dict, | ||
params: dict, | ||
data: typing.Optional[dict], | ||
): | ||
headers["Authorization"] = f"API-Key {self._key}" | ||
|
||
|
||
class PushTokenAuth(Auth): | ||
|
||
def __init__( | ||
self, | ||
token: str | ||
): | ||
super().__init__() | ||
|
||
self._token = token | ||
|
||
def apply( | ||
self, | ||
headers: dict, | ||
params: dict, | ||
data: typing.Optional[dict], | ||
): | ||
if data: | ||
data["pushToken"] = self._token | ||
else: | ||
params["pushToken"] = self._token |
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,32 @@ | ||
import os | ||
|
||
from .. import store, constants | ||
|
||
from .auth import Auth, ApiKeyAuth, NoneAuth | ||
from .endpoints import EndpointClient | ||
from .models.competitions import CompetitionCollection | ||
|
||
|
||
class Client: | ||
|
||
def __init__( | ||
self, | ||
base_url: str, | ||
auth: Auth | ||
): | ||
self.api = EndpointClient(base_url, auth) | ||
|
||
@property | ||
def competitions(self): | ||
return CompetitionCollection(client=self) | ||
|
||
def from_env(): | ||
store.load_from_env() | ||
|
||
api_key = os.getenv(constants.API_KEY_ENV_VAR) | ||
if api_key: | ||
auth = ApiKeyAuth(api_key) | ||
else: | ||
auth = NoneAuth() | ||
|
||
return Client(store.api_base_url, auth) |
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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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 @@ | ||
from .client import EndpointClient |
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,12 @@ | ||
class CheckEndpointMixin: | ||
|
||
def list_checks( | ||
self, | ||
competition_identifier | ||
): | ||
return self._result( | ||
self.get( | ||
f"/v1/competitions/{competition_identifier}/checks" | ||
), | ||
json=True | ||
) |
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,89 @@ | ||
import urllib.parse | ||
|
||
import requests | ||
|
||
from ..auth import Auth | ||
from ..errors import ApiException | ||
from .check import CheckEndpointMixin | ||
from .competition import CompetitionEndpointMixin | ||
from .crunch import CrunchEndpointMixin | ||
from .data_release import DataReleaseEndpointMixin | ||
from .phase import PhaseEndpointMixin | ||
from .prediction import PredictionEndpointMixin | ||
from .project import ProjectEndpointMixin | ||
from .round import RoundEndpointMixin | ||
from .score import ScoreEndpointMixin | ||
|
||
|
||
class EndpointClient( | ||
requests.Session, | ||
CheckEndpointMixin, | ||
CompetitionEndpointMixin, | ||
CrunchEndpointMixin, | ||
DataReleaseEndpointMixin, | ||
PhaseEndpointMixin, | ||
PredictionEndpointMixin, | ||
ProjectEndpointMixin, | ||
RoundEndpointMixin, | ||
ScoreEndpointMixin, | ||
): | ||
|
||
def __init__( | ||
self, | ||
base_url: str, | ||
auth: Auth | ||
): | ||
super().__init__() | ||
|
||
self.base_url = base_url | ||
self.auth_ = auth | ||
|
||
def request(self, method, url, *args, **kwargs): | ||
headers = kwargs.pop("headers", {}) | ||
params = kwargs.pop("params", {}) | ||
data = kwargs.pop("data", None) | ||
|
||
self.auth_.apply(headers, params, data) | ||
|
||
return super().request( | ||
method, | ||
urllib.parse.urljoin(self.base_url, url), | ||
*args, | ||
headers=headers, | ||
params=params, | ||
data=data, | ||
**kwargs, | ||
) | ||
|
||
def _raise_for_status( | ||
self, | ||
response: requests.Response, | ||
): | ||
try: | ||
response.raise_for_status() | ||
except requests.exceptions.HTTPError as error: | ||
content = error.response.json() | ||
|
||
code = content.pop("code", "") | ||
message = content.pop("message", "") | ||
|
||
raise ApiException( | ||
f"{code}: {message}" | ||
) | ||
|
||
def _result( | ||
self, | ||
response: requests.Response, | ||
json=False, | ||
binary=False | ||
): | ||
assert not (json and binary) | ||
self._raise_for_status(response) | ||
|
||
if json: | ||
return response.json() | ||
|
||
if binary: | ||
return response.content | ||
|
||
return response.text |
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,22 @@ | ||
class CompetitionEndpointMixin: | ||
|
||
def list_competitions( | ||
self | ||
): | ||
return self._result( | ||
self.get( | ||
"/v1/competitions" | ||
), | ||
json=True | ||
) | ||
|
||
def get_competition( | ||
self, | ||
identifier | ||
): | ||
return self._result( | ||
self.get( | ||
f"/v1/competitions/{identifier}" | ||
), | ||
json=True | ||
) |
Oops, something went wrong.