Skip to content

Commit

Permalink
feat: support proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
Dorbmon committed Oct 26, 2024
1 parent e15ed60 commit 239b851
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 7 deletions.
54 changes: 51 additions & 3 deletions batch_http_request.pyi
Original file line number Diff line number Diff line change
@@ -1,18 +1,66 @@
from typing import List, Tuple, Optional, Union

class Request:
"""
Represents an HTTP request.
Attributes:
url (str): The URL to which the request is sent.
method (str): HTTP method (e.g., GET, POST).
headers (List[Tuple[str, str]]): List of headers as key-value pairs.
body (Optional[bytes]): Optional body of the request, in bytes.
"""
url: str
method: str
headers: List[Tuple[str, str]]
body: Optional[bytes]

def __init__(self, url: str, method: str, headers: Optional[List[Tuple[str, str]]] = [], body: Optional[bytes] = None) -> None: ...
def __init__(self, url: str, method: str, headers: Optional[List[Tuple[str, str]]] = [], body: Optional[bytes] = None) -> None:
"""
Initializes a Request object.
Args:
url (str): The URL for the request.
method (str): The HTTP method to use.
headers (Optional[List[Tuple[str, str]]]): Optional list of headers.
body (Optional[bytes]): Optional request body.
"""
...

class Response:
"""
Represents an HTTP response.
Attributes:
status_code (int): HTTP status code of the response.
headers (List[Tuple[str, str]]): List of headers as key-value pairs.
body (bytes): Body of the response, in bytes.
"""
status_code: int
headers: List[Tuple[str, str]]
body: bytes

def __init__(self, status_code: int, headers: List[Tuple[str, str]], body: bytes) -> None: ...
def __init__(self, status_code: int, headers: List[Tuple[str, str]], body: bytes) -> None:
"""
Initializes a Response object.
Args:
status_code (int): The HTTP status code.
headers (List[Tuple[str, str]]): List of headers.
body (bytes): The response body.
"""
...

def batch_request(requests: List[Request], return_panic: bool = False, proxy: List[Tuple[str, str]] = []) -> List[Union[Response, RuntimeError]]:
"""
Handles a batch of HTTP requests.
Args:
requests (List[Request]): List of Request objects.
return_panic (bool): If True, include errors in the response list.
proxy (List[Tuple[str, str]]): Optional list of proxy settings as key-value pairs.
def batch_request(requests: List[Request], return_panic: bool = False) -> List[Union[Response, RuntimeError]]: ...
Returns:
List[Union[Response, RuntimeError]]: List of Response objects or errors.
"""
...
16 changes: 12 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::str::FromStr;
use std::{collections::HashMap, str::FromStr};

use pyo3::{exceptions::{PyRuntimeError, PyTypeError}, prelude::*, types::{PyList, PyType}, wrap_pyfunction};
mod request;
Expand All @@ -7,14 +7,22 @@ use request::{Request, Response};

use tokio;
#[pyfunction]
#[pyo3(signature = (requests, return_panic=false, /))]
fn batch_request<'a>(py: Python<'a>, requests: &Bound<'a, PyList>, return_panic: bool) -> PyResult<Bound<'a, PyAny>> {
#[pyo3(signature = (requests, return_panic=false, proxy=Vec::new(), /))]
fn batch_request<'a>(py: Python<'a>, requests: &Bound<'a, PyList>, return_panic: bool, proxy: Vec<(String, String)>) -> PyResult<Bound<'a, PyAny>> {
for request in requests.iter() {
if !request.is_instance_of::<Request>() {
return Err(PyTypeError::new_err("Invalid request type"));
}
}
let client = reqwest::Client::new();
let mut client = reqwest::Client::builder();
for (key, value) in proxy.iter() {
match key.as_str() {
"http" => client = client.proxy(reqwest::Proxy::http(value).unwrap()),
"https" => client = client.proxy(reqwest::Proxy::https(value).unwrap()),
_ => return Err(PyTypeError::new_err("Invalid proxy type")),
};
}
let client = client.build().unwrap();
let mut features = Vec::with_capacity(requests.len());
for request in requests.iter() {
let req = Request::extract_bound(&request).unwrap();
Expand Down

0 comments on commit 239b851

Please sign in to comment.