Passed
Pull Request — main (#403)
by Yohann
01:53
created

pincer.utils.api_data   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 6
eloc 22
dl 0
loc 37
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A APIDataGen.__async() 0 3 1
A APIDataGen.__await__() 0 2 1
A APIDataGen.__init__() 0 8 2
A APIDataGen.__aiter__() 0 3 2
1
# Copyright Pincer 2021-Present
0 ignored issues
show
introduced by
Missing module docstring
Loading history...
2
# Full MIT License can be found in `LICENSE` at the project root.
3
4
from __future__ import annotations
5
6
from typing import Generic, TypeVar, TYPE_CHECKING
7
8
from pincer.utils import APIObject
9
10
if TYPE_CHECKING:
11
    from typing import Any, Coroutine, List, Type, Generator, AsyncIterator
12
13
T = TypeVar('T')
14
15
16
class APIDataGen(Generic[T]):
0 ignored issues
show
introduced by
Missing class docstring
Loading history...
17
18
    def __init__(
19
        self,
0 ignored issues
show
Coding Style introduced by
Wrong hanging indentation before block (add 4 spaces).
Loading history...
20
        factory: Type[T],
0 ignored issues
show
Coding Style introduced by
Wrong hanging indentation before block (add 4 spaces).
Loading history...
21
        request_func: Coroutine[Any, None, Any]
0 ignored issues
show
Coding Style introduced by
Wrong hanging indentation before block (add 4 spaces).
Loading history...
22
    ):
23
24
        self.fac = factory if isinstance(factory, APIObject) else factory.from_dict
25
        self.request_func = request_func
26
27
    async def __async(self) -> List[T]:
28
        data = await self.request_func
29
        return [self.fac(i) for i in data]
30
31
    def __await__(self) -> Generator[Any, None, Any]:
32
        return self.__async().__await__()
33
34
    async def __aiter__(self) -> AsyncIterator[List[T]]:
35
        for item in await self:
36
            yield self.fac(item)
37