This repository has been archived on 2023-10-01. You can view files and clone it, but cannot push or open issues or pull requests.
PyNyaaTa/pynyaata/flarerequests.py

40 lines
1.2 KiB
Python
Raw Normal View History

2022-12-21 14:53:50 +00:00
from urllib import parse
2022-12-21 16:46:50 +00:00
from requests import RequestException, Response, Session, post
2022-12-21 15:14:43 +00:00
from .config import CLOUDPROXY_ENDPOINT, REQUESTS_TIMEOUT
2022-12-21 14:53:50 +00:00
class FlareRequests(Session):
2022-12-21 15:23:06 +00:00
def request(self, method, url, params=None, timeout=REQUESTS_TIMEOUT, **kwargs):
2022-12-21 14:53:50 +00:00
if not CLOUDPROXY_ENDPOINT:
2022-12-21 15:14:43 +00:00
return super().request(method, url, params, timeout=timeout, **kwargs)
2022-12-21 14:53:50 +00:00
2022-12-21 17:20:16 +00:00
post_data = {
"cmd": f"request.{method.lower()}",
"url": url,
"maxTimeout": timeout * 1000,
}
if params:
post_data["postData"] = parse.urlencode(params)
2022-12-21 17:12:18 +00:00
response = post(
CLOUDPROXY_ENDPOINT,
2022-12-21 17:20:16 +00:00
json=post_data,
2022-12-21 17:12:18 +00:00
)
2022-12-21 17:20:16 +00:00
2022-12-21 17:12:18 +00:00
solution = response.json()
if "solution" in solution:
resolved = Response()
resolved.raw = solution["solution"]["response"]
resolved.status_code = solution["solution"]["status"]
resolved.headers = solution["solution"]["headers"]
resolved.url = url
resolved.reason = solution["status"]
resolved.cookies = solution["solution"]["cookies"]
return resolved
raise RequestException(response)