MCPcopy Create free account
hub / github.com/apify/impit / request

Method request

impit-python/src/client.rs:369–443  ·  view source on GitHub ↗
(
        &self,
        py: Python<'_>,
        method: &str,
        url: String,
        content: Option<Vec<u8>>,
        mut data: Option<RequestBody>,
        headers: Option<HashMap<String, Str

Source from the content-addressed store, hash-verified

367
368 #[pyo3(signature = (method, url, content=None, data=None, headers=None, timeout=Some(Right(USE_CLIENT_DEFAULT_SENTINEL)), force_http3=false, stream=false))]
369 pub fn request(
370 &self,
371 py: Python<'_>,
372 method: &str,
373 url: String,
374 content: Option<Vec<u8>>,
375 mut data: Option<RequestBody>,
376 headers: Option<HashMap<String, String>>,
377 timeout: Option<Either<f64, &str>>,
378 force_http3: Option<bool>,
379 stream: Option<bool>,
380 ) -> Result<ImpitPyResponse, ImpitPyError> {
381 let mut headers = headers.clone();
382
383 if let Some(content) = content {
384 data = Some(RequestBody::Bytes(content));
385 }
386
387 let body: Vec<u8> = match data {
388 Some(data) => match data {
389 RequestBody::Bytes(bytes) => Ok(bytes),
390 RequestBody::Form(form) => {
391 headers.get_or_insert_with(HashMap::new).insert(
392 "Content-Type".to_string(),
393 "application/x-www-form-urlencoded".to_string(),
394 );
395 Ok(form_to_bytes(form))
396 }
397 RequestBody::CatchAll(e) => Err(ImpitPyError(ImpitError::BindingPassthroughError(
398 format!("Unsupported data type: {e:?}").to_string(),
399 ))),
400 },
401 None => Ok(Vec::new()),
402 }?;
403
404 let timeout = parse_timeout(timeout)
405 .map_err(|e| ImpitPyError(ImpitError::BindingPassthroughError(e.to_string())))?;
406
407 let options = RequestOptions {
408 headers: headers
409 .unwrap_or_default()
410 .iter()
411 .map(|(k, v)| (k.clone(), v.clone()))
412 .collect(),
413 timeout,
414 http3_prior_knowledge: force_http3.unwrap_or(false),
415 };
416
417 py.detach(|| {
418 pyo3_async_runtimes::tokio::get_runtime().block_on(async {
419 let response = match method.to_lowercase().as_str() {
420 "get" => self.impit.get(url, Some(body), Some(options)).await,
421 "post" => self.impit.post(url, Some(body), Some(options)).await,
422 "patch" => self.impit.patch(url, Some(body), Some(options)).await,
423 "put" => self.impit.put(url, Some(body), Some(options)).await,
424 "options" => self.impit.options(url, Some(body), Some(options)).await,
425 "trace" => self.impit.trace(url, Some(body), Some(options)).await,
426 "head" => self.impit.head(url, Some(body), Some(options)).await,

Callers 10

getMethod · 0.45
headMethod · 0.45
postMethod · 0.45
patchMethod · 0.45
putMethod · 0.45
deleteMethod · 0.45
optionsMethod · 0.45
traceMethod · 0.45
streamMethod · 0.45
execute_requestMethod · 0.45

Calls 13

form_to_bytesFunction · 0.85
ImpitPyErrorClass · 0.85
parse_timeoutFunction · 0.85
cloneMethod · 0.80
iterMethod · 0.80
getMethod · 0.45
postMethod · 0.45
patchMethod · 0.45
putMethod · 0.45
optionsMethod · 0.45
traceMethod · 0.45
headMethod · 0.45

Tested by

no test coverage detected