Introduction

https://img.shields.io/pypi/v/ahttp-client?style=flat https://img.shields.io/pypi/dm/ahttp-client?style=flat https://img.shields.io/pypi/l/ahttp-client?style=flat

Annotated Type@decorator 를 사용하여 간결하고 직관적인 비동기 HTTP 요청을 제공하는 파이썬 패키지입니다.

주요 특징

  • HTTP 요청을 데코레이터(decorator)로 정의할 수 있습니다.

  • Annotated Type으로 HTTP Component를 관리할 수 있습니다.

  • HTTP 요청이 있기 전과 후에 후킹 기능을 제공합니다.

시작하기

ahttp_client.Session 으로 확장한 GithubSerivce 클래스를 구현합니다.그리고, request decorator 를 이용하여 list_repositories 메소드를 만들어줍니다.

Annotated Type을 이용하여 user 인수를 HTTP-구성요소(Path)로 사용할 수 있도록 정의합니다.

class GithubService(Session):
    def __init__(self):
        super().__init__("https://api.github.com")

    @request("GET", "/users/{user}/repos")
    def list_repositories(
        self, user: Annotated[str, Path]
    ) -> dict[str, Any]:
        return await response.json()

Asynchronous Context Manage( async with )를 이용하여 GithubService 객체를 생성합니다.

async with GithubService() as service:
    result = await service.list_repoisitories(user = "gunyu1019")
    print(result)

GithubSerivce 객체에 있는 Client Session은 Asynchronous Context Manage를 벗어나면 자동으로 종료됩니다.