-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp_client.py
149 lines (126 loc) · 5.39 KB
/
http_client.py
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import json
import random
from abc import ABC, abstractmethod
from typing import Dict, List, Optional
import httpx
from loguru import logger
class IHTTPClient(ABC):
@abstractmethod
def get(self, path: str, params: Optional[dict] = None) -> httpx.Response:
pass
@abstractmethod
def post(
self, path: str, data: dict, params: Optional[dict] = None
) -> httpx.Response:
pass
class SessionManager:
"""Handles session creation, closing, and management."""
def __init__(self, base_url: str, timeout: httpx.Timeout):
self.base_url = base_url
self.timeout = timeout
self.session: Optional[httpx.Client] = None
def __enter__(self) -> "SessionManager":
self.session = httpx.Client(
base_url=self.base_url, follow_redirects=True, timeout=self.timeout
)
logger.info("HTTP session started.")
return self
def __exit__(self, exc_type, exc_value, traceback):
if self.session:
self.session.close()
logger.info("HTTP session closed.")
class HeaderManager:
"""Handles loading, saving, and selecting headers."""
def __init__(self, headers_list: List[Dict[str, str]]):
self.headers_list = headers_list
self.recent_headers = self.get_recent_headers()
self.random_headers = self.get_random_headers()
def get_recent_headers(self) -> List[Dict[str, str]]:
"""Load the most recent headers from a file."""
if Path("recent_headers.json").exists():
with open("recent_headers.json", "r", encoding="utf-8") as f:
headers = json.load(f)
logger.debug("Recent headers loaded from file.")
return headers
logger.warning("No file with recent headers found.")
return []
def save_recent_headers(self, headers: Dict[str, str]):
"""Save the current headers to the recent headers list."""
self.recent_headers.insert(0, headers)
self.recent_headers = self.recent_headers[
:3
] # Keep only the latest three headers
logger.debug(f"Updated recent headers: {self.recent_headers}")
def get_random_headers(self) -> Dict[str, str]:
"""Select random headers from the available list."""
candidate = None
while not candidate or candidate in self.recent_headers:
candidate = random.choice(self.headers_list)
self.save_recent_headers(candidate)
logger.debug(f"Selected headers: {candidate}")
return candidate
def save_headers_to_file(self):
"""Save recent headers to a file."""
with open("recent_headers.json", "w", encoding="utf-8") as f:
json.dump(self.recent_headers, f)
logger.debug("Recent headers saved to file.")
class HTTPClient(IHTTPClient):
"""HTTP client for handling cookies and session operations."""
def __init__(
self,
base_url: str,
headers_list: List[Dict[str, str]],
timeout: httpx.Timeout = httpx.Timeout(30.0, connect=15.0, read=60.0),
):
self.base_url = base_url
self.headers_list = headers_list
self.timeout = timeout
self.session_manager = SessionManager(
base_url=self.base_url, timeout=self.timeout
)
self.header_manager = HeaderManager(headers_list=self.headers_list)
logger.info(f"HTTPClient initialized with base_url: {self.base_url}")
def __enter__(self) -> "HTTPClient":
self.session_manager.__enter__()
self.session = self.session_manager.session
self.session.headers.update(self.header_manager.random_headers)
return self
def __exit__(self, exc_type, exc_value, traceback):
self.session_manager.__exit__(exc_type, exc_value, traceback)
self.header_manager.save_headers_to_file()
def get(self, path: str, params: Optional[dict] = None) -> httpx.Response:
"""Send a GET request to the specified path."""
try:
logger.info(f"Sending GET request to {path} with params {params}")
response = self.session.get(path, params=params)
logger.debug(
f"GET response: {response.status_code}, content preview: {response.text[:100]}..."
)
response.raise_for_status()
return response
except httpx.HTTPStatusError as e:
logger.error(f"HTTP error during GET request: {e.response.status_code}")
raise
except httpx.RequestError as e:
logger.error(f"Request error during GET request: {e}")
raise
def post(
self, path: str, data: dict, params: Optional[dict] = None
) -> httpx.Response:
"""Send a POST request to the specified path."""
try:
logger.info(
f"Sending POST request to {path} with data {data} and params {params}"
)
response = self.session.post(path, data=data, params=params)
logger.debug(
f"POST response: {response.status_code}, content preview: {response.text[:100]}..."
)
response.raise_for_status()
return response
except httpx.HTTPStatusError as e:
logger.error(f"HTTP error during POST request: {e.response.status_code}")
raise
except httpx.RequestError as e:
logger.error(f"Request error during POST request: {e}")
raise