Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import asyncio
- import dataclasses
- import typing
- import httpx
- @dataclasses.dataclass
- class HttpRequest:
- url: str
- @dataclasses.dataclass
- class HttpResponse:
- code: int
- body: bytes
- type HttpGenerator[R] = typing.Generator[HttpRequest, HttpResponse, R]
- def http_once(
- url: str,
- ) -> HttpGenerator[HttpResponse]:
- print("requesting", url)
- result = yield HttpRequest(url=url)
- print("response was", result)
- return result
- def do_multiple_requests() -> HttpGenerator[bool]:
- # The idea is to allow sending multiple requests, for example during auth sequence.
- # My original task was something like grabbing the XSRF token from the first response and using it during the second.
- first = yield from http_once("https://google.com")
- second = yield from http_once("https://baidu.com")
- return len(first.body) > len(second.body)
- def execute_sync[R](task: HttpGenerator[R]) -> R:
- with httpx.Client() as client:
- current_request = next(task)
- while True:
- resp = client.get(url=current_request.url)
- prepared = HttpResponse(code=resp.status_code, body=resp.content)
- try:
- current_request = task.send(prepared)
- except StopIteration as e:
- return e.value
- async def execute_async[R](task: HttpGenerator[R]) -> R:
- async with httpx.AsyncClient() as client:
- current_request = next(task)
- while True:
- resp = await client.get(url=current_request.url)
- prepared = HttpResponse(code=resp.status_code, body=resp.content)
- try:
- current_request = task.send(prepared)
- except StopIteration as e:
- return e.value
- print("sync version")
- execute_sync(do_multiple_requests())
- print("async version")
- asyncio.run(execute_async(do_multiple_requests()))
Advertisement
Add Comment
Please, Sign In to add comment