Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: choose client in RequestModel.request() #31

Merged
merged 4 commits into from
Aug 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions pyclasher/api/requests/abc.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
from ..models import Paging
from ...client import Client
from ...utils.request_methods import RequestMethods
from ...exceptions import NoClient, ClientIsNotRunning, RequestNotDone, MISSING
from ...exceptions import (NoClient, ClientIsNotRunning, RequestNotDone,
MISSING, InvalidClientId)

request_id = 0

Expand Down Expand Up @@ -90,11 +91,16 @@ def _get_data(self, item):
else:
return MISSING

async def request(self):
async def request(self, client_id=None):
"""
makes a request to the ClashOfClans API
"""
self.client = Client.get_instance()
self.client = Client.get_instance(client_id)
if self.client is None:
raise NoClient
if self.client is MISSING:
raise InvalidClientId(f"Cannot find a client with the client_id "
f"{client_id}.")

if not self.client.is_running:
raise ClientIsNotRunning
Expand All @@ -118,6 +124,8 @@ async def request(self):
raise req_error.value

self.client.logger.debug(f"request {self._request_id} done")

self.client = None
return self

async def __aenter__(self):
Expand Down
2 changes: 1 addition & 1 deletion pyclasher/api/requests/abc.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class RequestModel(ABC):
def _get_data(self, item) -> dict:
...

async def request(self) -> RequestModel:
async def request(self, client_id: int | str = None) -> RequestModel:
...

async def __aenter__(self):
Expand Down
5 changes: 4 additions & 1 deletion pyclasher/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,10 @@ def get_instance(cls, client_id=None):
if len(clients):
if client_id is None:
return clients[0]
return clients[client_id]
for client in clients:
if client.client_id == client_id:
return client
return MISSING
return None

@classmethod
Expand Down
4 changes: 4 additions & 0 deletions pyclasher/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,3 +122,7 @@ def __str__(self):
class RequestTimeout(PyClasherException):
def __str__(self):
return "The request took to much time and was cancelled."


class InvalidClientId(PyClasherException):
pass
4 changes: 4 additions & 0 deletions pyclasher/exceptions.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -224,3 +224,7 @@ class InvalidSeasonFormat(Exception):
class RequestTimeout(Exception):
def __str__(self) -> str:
...


class InvalidClientId(PyClasherException):
pass