Skip to content

Commit

Permalink
feat: choose client in RequestModel.request()
Browse files Browse the repository at this point in the history
  • Loading branch information
201st-Luka committed Aug 23, 2023
1 parent bae4821 commit 3089f63
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 4 deletions.
12 changes: 10 additions & 2 deletions pyclasher/api/requests/abc.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from typing import Any
from urllib.parse import quote, urlencode

from exceptions import InvalidClientId
from ..models import Paging
from ...client import Client
from ...utils.request_methods import RequestMethods
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

0 comments on commit 3089f63

Please sign in to comment.