Skip to main content

RequestData

Struct RequestData 

Source
pub struct RequestData { /* private fields */ }
Expand description

Aggregates all RequestParameters for a single Bot API call.

§Relationship to Python source

Python attribute / propertyRust equivalent
contains_filesRequestData::contains_files()
parametersRequestData::parameters()
json_parametersRequestData::json_parameters()
json_payloadRequestData::json_payload()
multipart_dataRequestData::multipart_data()
url_encoded_parametersRequestData::url_encoded_parameters()
parametrized_urlRequestData::parametrized_url()

Implementations§

Source§

impl RequestData

Source

pub fn new() -> Self

Create an empty RequestData.

Source

pub fn from_parameters(parameters: Vec<RequestParameter>) -> Self

Create a RequestData from an existing list of parameters.

use rust_tg_bot_raw::request::request_data::RequestData;
use rust_tg_bot_raw::request::request_parameter::RequestParameter;
use serde_json::json;

let params = vec![RequestParameter::new("chat_id", json!(42))];
let data = RequestData::from_parameters(params);
assert!(!data.contains_files());
Source

pub fn push(&mut self, param: RequestParameter)

Append a single parameter.

Source

pub fn iter(&self) -> impl Iterator<Item = &RequestParameter>

Iterate over all parameters.

Source

pub fn contains_files(&self) -> bool

Returns true when at least one parameter carries attached files.

Mirrors RequestData.contains_files in Python.

use rust_tg_bot_raw::request::request_data::RequestData;
use rust_tg_bot_raw::request::request_parameter::{InputFileRef, RequestParameter};

let file = InputFileRef::direct(vec![0u8]);
let p = RequestParameter::file_only("photo", file);
let data = RequestData::from_parameters(vec![p]);
assert!(data.contains_files());
Source

pub fn parameters(&self) -> HashMap<&str, &Value>

All parameters as a HashMap<name, Value>, excluding those whose value is None.

Mirrors RequestData.parameters in Python.

Source

pub fn json_parameters(&self) -> HashMap<String, String>

All parameters as HashMap<name, json_encoded_string>, excluding those whose JSON value is None.

Mirrors RequestData.json_parameters in Python.

use rust_tg_bot_raw::request::request_data::RequestData;
use rust_tg_bot_raw::request::request_parameter::RequestParameter;
use serde_json::json;

let data = RequestData::from_parameters(vec![
    RequestParameter::new("chat_id", json!(99)),
    RequestParameter::new("text", json!("hello")),
]);
let jp = data.json_parameters();
assert_eq!(jp.get("chat_id").map(String::as_str), Some("99"));
assert_eq!(jp.get("text").map(String::as_str), Some("hello"));
Source

pub fn json_payload(&self) -> Vec<u8>

Serialize the JSON parameters to a UTF-8 byte payload.

Mirrors RequestData.json_payload in Python.

use rust_tg_bot_raw::request::request_data::RequestData;
use rust_tg_bot_raw::request::request_parameter::RequestParameter;
use serde_json::json;

let data = RequestData::from_parameters(vec![
    RequestParameter::new("chat_id", json!(1)),
]);
let payload = data.json_payload();
let parsed: serde_json::Value = serde_json::from_slice(&payload).unwrap();
assert_eq!(parsed["chat_id"], json!("1"));
Source

pub fn url_encoded_parameters(&self) -> String

URL-encode all JSON parameters.

Mirrors RequestData.url_encoded_parameters in Python (without the encode_kwargs variant — use [Self::url_encoded_parameters_with] for that).

use rust_tg_bot_raw::request::request_data::RequestData;
use rust_tg_bot_raw::request::request_parameter::RequestParameter;
use serde_json::json;

let data = RequestData::from_parameters(vec![
    RequestParameter::new("a", json!("1")),
]);
let encoded = data.url_encoded_parameters();
assert!(encoded.contains("a=1"));
Source

pub fn parametrized_url(&self, url: &str) -> String

Attach the URL-encoded parameters to a base URL with a ? separator.

Mirrors RequestData.parametrized_url in Python.

use rust_tg_bot_raw::request::request_data::RequestData;
use rust_tg_bot_raw::request::request_parameter::RequestParameter;
use serde_json::json;

let data = RequestData::from_parameters(vec![
    RequestParameter::new("offset", json!("0")),
]);
let url = data.parametrized_url("https://api.telegram.org/bot<token>/getUpdates");
assert!(url.starts_with("https://api.telegram.org/bot<token>/getUpdates?"));
Source

pub fn multipart_data(&self) -> Option<HashMap<String, MultipartPart>>

Collect multipart form parts contributed by all parameters.

Returns None when Self::contains_files is false.

The outer HashMap maps multipart part names to (bytes, mime_type, file_name) triples so that crate::request::reqwest_impl can assemble a reqwest::multipart::Form without needing to reach back into this module.

Trait Implementations§

Source§

impl Clone for RequestData

Source§

fn clone(&self) -> RequestData

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for RequestData

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for RequestData

Source§

fn default() -> RequestData

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more