Skip to main content

Request

Struct Request 

Source
pub struct Request {
    pub body: Value,
    pub headers: HashMap<String, String>,
    pub query: HashMap<String, String>,
    pub path_params: HashMap<String, String>,
    /* private fields */
}
Expand description

Action 请求

封装 HTTP 请求信息,用于 Action 执行

§字段

  • body: 请求体(JSON 格式)
  • headers: 请求头
  • query: 查询参数
  • path_params: 路径参数

§示例

ⓘ
use yang_base::action::Request;
use serde_json::json;
use std::collections::HashMap;

// 创建请求
let mut request = Request::new(json!({
    "username": "alice",
    "email": "alice@example.com"
}));

// 添加请求头
request = request.header("Content-Type", "application/json");
request = request.header("Authorization", "Bearer token123");

// 添加查询参数
request = request.query("page", "1");
request = request.query("limit", "10");

// 添加路径参数
request = request.path_param("id", "123");

// 提取 Token
if let Some(token) = request.token() {
    println!("Token: {}", token);
}

Fields§

§body: Value

请求体(JSON)

§headers: HashMap<String, String>

请求头

§query: HashMap<String, String>

查询参数

§path_params: HashMap<String, String>

路径参数

Implementations§

Source§

impl Request

Source

pub fn new(body: Value) -> Self

创建新请求

§参数
  • body: 请求体(JSON 格式)
§返回
  • 新的 Request 实例
§示例
ⓘ
use yang_base::action::Request;
use serde_json::json;

let request = Request::new(json!({
    "name": "Alice",
    "age": 30
}));
Source

pub fn header(self, name: impl Into<String>, value: impl Into<String>) -> Self

添加请求头

§参数
  • name: 请求头名称
  • value: 请求头值
§返回
  • 修改后的 Request 实例(支持链式调用)
§示例
ⓘ
use yang_base::action::Request;
use serde_json::json;

let request = Request::new(json!({}))
    .header("Content-Type", "application/json")
    .header("Authorization", "Bearer token123");
Source

pub fn headers(self, headers: HashMap<String, String>) -> Self

批量添加请求头

§参数
  • headers: 请求头映射
§返回
  • 修改后的 Request 实例(支持链式调用)
§示例
ⓘ
use yang_base::action::Request;
use serde_json::json;
use std::collections::HashMap;

let mut headers = HashMap::new();
headers.insert("Content-Type".to_string(), "application/json".to_string());
headers.insert("Authorization".to_string(), "Bearer token123".to_string());

let request = Request::new(json!({}))
    .headers(headers);
Source

pub fn query(self, key: impl Into<String>, value: impl Into<String>) -> Self

添加查询参数

§参数
  • key: 参数名
  • value: 参数值
§返回
  • 修改后的 Request 实例(支持链式调用)
§示例
ⓘ
use yang_base::action::Request;
use serde_json::json;

let request = Request::new(json!({}))
    .query("page", "1")
    .query("limit", "10");
Source

pub fn queries(self, query: HashMap<String, String>) -> Self

批量添加查询参数

§参数
  • query: 查询参数映射
§返回
  • 修改后的 Request 实例(支持链式调用)
§示例
ⓘ
use yang_base::action::Request;
use serde_json::json;
use std::collections::HashMap;

let mut query = HashMap::new();
query.insert("page".to_string(), "1".to_string());
query.insert("limit".to_string(), "10".to_string());

let request = Request::new(json!({}))
    .queries(query);
Source

pub fn path_param( self, key: impl Into<String>, value: impl Into<String>, ) -> Self

添加路径参数

§参数
  • key: 参数名
  • value: 参数值
§返回
  • 修改后的 Request 实例(支持链式调用)
§示例
ⓘ
use yang_base::action::Request;
use serde_json::json;

let request = Request::new(json!({}))
    .path_param("id", "123")
    .path_param("action", "update");
Source

pub fn path_params(self, path_params: HashMap<String, String>) -> Self

批量添加路径参数

§参数
  • path_params: 路径参数映射
§返回
  • 修改后的 Request 实例(支持链式调用)
§示例
ⓘ
use yang_base::action::Request;
use serde_json::json;
use std::collections::HashMap;

let mut path_params = HashMap::new();
path_params.insert("id".to_string(), "123".to_string());
path_params.insert("action".to_string(), "update".to_string());

let request = Request::new(json!({}))
    .path_params(path_params);
Source

pub fn token(&self) -> Option<&str>

从 Authorization 头提取 Token

支持 Bearer Token 格式:Authorization: Bearer <token>

§返回
  • Some(&str): Token 字符串
  • None: 未找到 Token 或格式不正确
§示例
ⓘ
use yang_base::action::Request;
use serde_json::json;

let request = Request::new(json!({}))
    .header("Authorization", "Bearer my_secret_token");

assert_eq!(request.token(), Some("my_secret_token"));
Source

pub fn user_agent(&self) -> Option<&str>

获取规范化 headers 中的 User-Agent。

Source

pub fn cookie(&self) -> Option<&str>

获取规范化 headers 中的 Cookie。

Source

pub fn get_header(&self, name: &str) -> Option<&str>

获取请求头值

§参数
  • name: 请求头名称
§返回
  • Some(&str): 请求头值
  • None: 请求头不存在
§示例
ⓘ
use yang_base::action::Request;
use serde_json::json;

let request = Request::new(json!({}))
    .header("Content-Type", "application/json");

assert_eq!(request.get_header("Content-Type"), Some("application/json"));
Source

pub fn get_query(&self, key: &str) -> Option<&str>

获取查询参数值

§参数
  • key: 参数名
§返回
  • Some(&str): 参数值
  • None: 参数不存在
§示例
ⓘ
use yang_base::action::Request;
use serde_json::json;

let request = Request::new(json!({}))
    .query("page", "1");

assert_eq!(request.get_query("page"), Some("1"));
Source

pub fn get_path_param(&self, key: &str) -> Option<&str>

获取路径参数值

§参数
  • key: 参数名
§返回
  • Some(&str): 参数值
  • None: 参数不存在
§示例
ⓘ
use yang_base::action::Request;
use serde_json::json;

let request = Request::new(json!({}))
    .path_param("id", "123");

assert_eq!(request.get_path_param("id"), Some("123"));

Trait Implementations§

Source§

impl Clone for Request

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for Request

Source§

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

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

impl Default for Request

Source§

fn default() -> Self

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> DynClone for T
where T: Clone,

Source§

fn __clone_box(&self, _: Private) -> *mut ()

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

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

Source§

fn from_ref(input: &T) -> T

Converts to this type from a reference to the input type.
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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

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

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + 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: Sized + 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> Same for T

Source§

type Output = T

Should always be Self
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 = !

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

fn try_from(value: U) -> Result<T, !>

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<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

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