Skip to content

Login

Login

Bases: LoginModel

class to log in via the ClashOfClans login API

to execute the login use Login(...).login() or await Login(...).login() depending on the context

Source code in pyclasher/utils/login.py
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
class Login(LoginModel):
    login_url = "https://developer.clashofclans.com/api/login"
    __response = None

    def __new__(cls, *args, **kwargs):
        return super().__new__(cls)

    def __init__(self, email, password):
        super().__init__(data=None)
        self.email = email
        self.__password = password

        return

    def _get_data(self, item):
        if self._data is None:
            return None
        if self._data is MISSING:
            raise LoginNotDone
        if item in self._data:
            return self._data[item]
        else:
            return MISSING

    def login(self):
        async def async_login():
            async with request("post", self.login_url, json={
                "email": self.email,
                "password": self.__password
            }) as response:
                if response.status == 200:
                    self._data = await response.json()
                    return self
                else:
                    raise InvalidLoginData

        try:
            get_running_loop()
        except RuntimeError:
            return run(async_login())
        else:
            return async_login()

login()

method to execute the login process

This method can be called in an asynchronous context using the await keyword in an asynchronous definition or used as a traditional method without awaiting it.

:return: the login :rtype: Login | Coroutine[Any, Any, Login]

Source code in pyclasher/utils/login.py
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
def login(self):
    async def async_login():
        async with request("post", self.login_url, json={
            "email": self.email,
            "password": self.__password
        }) as response:
            if response.status == 200:
                self._data = await response.json()
                return self
            else:
                raise InvalidLoginData

    try:
        get_running_loop()
    except RuntimeError:
        return run(async_login())
    else:
        return async_login()