-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttp.py
More file actions
103 lines (80 loc) · 2.38 KB
/
http.py
File metadata and controls
103 lines (80 loc) · 2.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
"""HTTP helper abstraction layer."""
from __future__ import annotations
from abc import ABC, abstractmethod
from typing import Any, Literal
from astroapi.utils.http_client import HttpClient
class HttpHelper(ABC):
"""Abstract HTTP helper interface.
This abstraction allows for easier testing and potential alternative
HTTP client implementations.
"""
@abstractmethod
def get(
self,
url: str,
params: dict[str, Any] | None = None,
response_type: Literal["json", "text"] = "json",
) -> Any:
"""Make GET request.
Args:
url: Request URL
params: Query parameters
response_type: Expected response type
Returns:
Response data
"""
@abstractmethod
def post(
self,
url: str,
json: dict[str, Any] | None = None,
response_type: Literal["json", "text"] = "json",
) -> Any:
"""Make POST request.
Args:
url: Request URL
json: JSON request body
response_type: Expected response type
Returns:
Response data
"""
class HttpxHttpHelper(HttpHelper):
"""Concrete HTTP helper implementation using httpx.
Wraps HttpClient to provide the HttpHelper interface.
"""
def __init__(self, client: HttpClient) -> None:
"""Initialize helper with HTTP client.
Args:
client: HttpClient instance
"""
self._client = client
def get(
self,
url: str,
params: dict[str, Any] | None = None,
response_type: Literal["json", "text"] = "json",
) -> Any:
"""Make GET request.
Args:
url: Request URL
params: Query parameters
response_type: Expected response type
Returns:
Response data
"""
return self._client.get(url, params=params, response_type=response_type)
def post(
self,
url: str,
json: dict[str, Any] | None = None,
response_type: Literal["json", "text"] = "json",
) -> Any:
"""Make POST request.
Args:
url: Request URL
json: JSON request body
response_type: Expected response type
Returns:
Response data
"""
return self._client.post(url, json=json, response_type=response_type)