1- from typing import TypeVar , Dict , Optional , Union , List
1+ from typing import Dict , Optional , Union , List , Generic , TypeVar
22from aiohttp import ClientSession , ClientResponse
3- from .async_threads import ThreadTask
3+ from asyncio import Future
4+
45from .response import Response
56
67async def parse_response (resp : ClientResponse ) -> Union [str , Dict , List ]:
@@ -14,7 +15,9 @@ async def parse_response(resp: ClientResponse) -> Union[str, Dict, List]:
1415 return await resp .json ()
1516 return await resp .text ()
1617
17- class Request (ThreadTask [Response ]):
18+ RT = TypeVar ('RT' )
19+
20+ class Request (Generic [RT ]):
1821 """
1922 This class encapsulates a request to be executed inside of a
2023 thread pool.
@@ -26,6 +29,8 @@ class Request(ThreadTask[Response]):
2629 params : Optional [Dict ]
2730 body : Optional [Dict ]
2831 headers : Optional [Dict ]
32+ result : Optional [Response ]
33+ __future : Optional [Future ]
2934
3035 def __init__ (self , client : ClientSession , method : str , url : str , params : Optional [Dict ] = None , body : Optional [Dict ] = None , headers : Optional [Dict ] = None ) -> None :
3136 super ().__init__ ()
@@ -35,16 +40,18 @@ def __init__(self, client: ClientSession, method: str, url: str, params: Optiona
3540 self .params = params
3641 self .body = body
3742 self .headers = headers
43+ self .response = None
44+ self .__future = None
3845
39- async def run (self ) -> Response :
46+ def send (self ) -> Response :
4047 """
4148 This function is to be executed within a thread. It makes a
4249 request, and parses the response into a custom response object.
4350
4451 @return: A response object containing the fetched data.
4552 """
4653 self .attempts = 0
47- return await self .make_request ()
54+ self . __future = self .make_request ()
4855
4956 async def make_request (self ) -> Response :
5057 """
@@ -58,17 +65,22 @@ async def make_request(self) -> Response:
5865 try :
5966 async with self .client .request (self .method , self .url , data = self .body , params = self .params , headers = self .headers ) as response :
6067 data = await parse_response (response )
61- return Response (response .status , response .ok , data )
68+ return Response (self . url , response .status , response .ok , response . headers , data )
6269 except Exception as e :
6370 self .attempts += 1
6471 if self .attempts <= 3 : return await self .make_request ()
6572 raise e
66-
67- @property
68- def bucket (self ) -> str :
73+
74+ async def get_result (self ):
6975 """
70- Represents the request and its componets as astring.
76+ It returns the result of the execution. Blocks if
77+ the tasks underlying lock isn't freed, or returns
78+ immediately. May cause unexpected behaviour if not
79+ submitted to a pool.
7180
72- @return: The request as a string.
81+ @return: Returns the value returned from the run method.
7382 """
74- return f"{ self .url } :{ self .params } :{ self .body } "
83+ if self .__future :
84+ self .result = await self .__future
85+ self .__future = None
86+ return self .result
0 commit comments