|
| 1 | +from typing import List, Optional, Dict |
| 2 | +import httpx |
| 3 | + |
| 4 | +from .models import Proxy |
| 5 | +from .exceptions import APIError, InvalidAPIKey, NetworkError, TimeoutError, ParseError |
| 6 | +from ._utils import build_headers, extract_error_message |
| 7 | + |
| 8 | + |
| 9 | +class AsyncFreeProxyClient: |
| 10 | + def __init__(self, api_key: str, *, base_url: str = "https://api.getfreeproxy.com", timeout: float = 30.0, user_agent: Optional[str] = None, session: Optional[httpx.AsyncClient] = None): |
| 11 | + self.api_key = api_key |
| 12 | + self.base_url = base_url.rstrip("/") |
| 13 | + self.timeout = timeout |
| 14 | + self.user_agent = user_agent |
| 15 | + self._own_session = session is None |
| 16 | + self.session = session or httpx.AsyncClient(timeout=timeout) |
| 17 | + |
| 18 | + async def _handle_error_response(self, response: httpx.Response): |
| 19 | + status = response.status_code |
| 20 | + text = response.text |
| 21 | + try: |
| 22 | + j = response.json() |
| 23 | + except Exception: |
| 24 | + j = None |
| 25 | + |
| 26 | + msg = extract_error_message(text, j) |
| 27 | + if status == 401: |
| 28 | + raise InvalidAPIKey(status, msg, raw_body=text) |
| 29 | + raise APIError(status, msg, raw_body=text) |
| 30 | + |
| 31 | + async def query(self, country: Optional[str] = None, protocol: Optional[str] = None, page: Optional[int] = None) -> List[Proxy]: |
| 32 | + url = f"{self.base_url}/v1/proxies" |
| 33 | + params: Dict[str, str] = {} |
| 34 | + if country: |
| 35 | + params["country"] = country |
| 36 | + if protocol: |
| 37 | + params["protocol"] = protocol |
| 38 | + if page is not None: |
| 39 | + params["page"] = str(page) |
| 40 | + |
| 41 | + headers = build_headers(self.api_key, self.user_agent) |
| 42 | + try: |
| 43 | + resp = await self.session.get(url, headers=headers, params=params) |
| 44 | + except httpx.ReadTimeout: |
| 45 | + raise TimeoutError("request timed out") |
| 46 | + except httpx.RequestError as e: |
| 47 | + raise NetworkError(str(e)) |
| 48 | + |
| 49 | + if resp.status_code < 200 or resp.status_code >= 300: |
| 50 | + await self._handle_error_response(resp) |
| 51 | + |
| 52 | + try: |
| 53 | + data = resp.json() |
| 54 | + except Exception as e: |
| 55 | + raise ParseError(f"failed to parse JSON response: {e}") |
| 56 | + |
| 57 | + if not isinstance(data, list): |
| 58 | + raise ParseError("expected JSON array of proxies") |
| 59 | + |
| 60 | + proxies = [Proxy.from_dict(item) for item in data] |
| 61 | + return proxies |
| 62 | + |
| 63 | + async def query_country(self, country: str) -> List[Proxy]: |
| 64 | + return await self.query(country=country) |
| 65 | + |
| 66 | + async def query_protocol(self, protocol: str) -> List[Proxy]: |
| 67 | + return await self.query(protocol=protocol) |
| 68 | + |
| 69 | + async def query_page(self, page: int) -> List[Proxy]: |
| 70 | + return await self.query(page=page) |
| 71 | + |
| 72 | + async def iter_pages(self, *, start: int = 1): |
| 73 | + page = start |
| 74 | + while True: |
| 75 | + items = await self.query(page=page) |
| 76 | + yield items |
| 77 | + if not items: |
| 78 | + break |
| 79 | + page += 1 |
| 80 | + |
| 81 | + async def raw_request(self, method: str, path: str, **kwargs) -> httpx.Response: |
| 82 | + url = f"{self.base_url.rstrip('/')}/{path.lstrip('/')}" |
| 83 | + headers = kwargs.pop("headers", None) or build_headers(self.api_key, self.user_agent) |
| 84 | + return await self.session.request(method, url, headers=headers, timeout=self.timeout, **kwargs) |
| 85 | + |
| 86 | + async def aclose(self) -> None: |
| 87 | + if self._own_session: |
| 88 | + await self.session.aclose() |
0 commit comments