Struct RequestParser

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

HTTP POSTリクエストのパーサー

Implementations§

Source§

impl RequestParser

Source

pub fn parse_request<B>(&self, request: Request<B>) -> ParseRequest<B>
where B: Body, B::Error: Into<Box<dyn Error + Send + Sync + 'static>>,

Available on crate feature http only.

http::Requestをパースします。

Note: この関数はhttpfeatureが有効になっている時のみ有効です。

§Arguments
  • request: リクエスト全体
§Example
use traq_bot_http::{EventKind, RequestParser};

let verification_token = "verification_token";
let body = r#"{"eventTime": "2019-05-07T04:50:48.582586882Z"}"#.to_string();
let request = http::Request::builder()
    .method(http::Method::POST)
    .header(http::header::CONTENT_TYPE, "application/json")
    .header("X-TRAQ-BOT-TOKEN", verification_token)
    .header("X-TRAQ-BOT-EVENT", "PING")
    .body(body)?;
let parser = RequestParser::new(verification_token);
let event = parser.parse_request(request).await?;
assert_eq!(event.kind(), EventKind::Ping);
§Errors

Errorのうち、Error::kindが以下のものを返す可能性があります。

Source§

impl RequestParser

Source

pub fn new(verification_token: &str) -> Self

新しいRequestParserを作成します。

§Arguments
  • verification_token - ボットのVerification Token
§Example
use traq_bot_http::RequestParser;
let parser = RequestParser::new("verification_token");
Source

pub fn parse_headers<'a, H, K, V>(&self, headers: H) -> Result<EventKind>
where H: IntoIterator<Item = (&'a K, &'a V)>, K: AsRef<[u8]> + ?Sized + 'static, V: AsRef<[u8]> + ?Sized + 'static,

POSTリクエストのヘッダーからイベント名を取得します。

§Arguments
  • headers - リクエストのヘッダー
§Example
use http::HeaderMap;
use traq_bot_http::RequestParser;

let parser = RequestParser::new("verification_token");
let headers = HeaderMap::new();
let kind = parser.parse_headers(&headers);
assert!(kind.is_err());
§Errors

Errorのうち、Error::kindが以下のものを返す可能性があります。

Source

pub fn parse<'a, H, K, V>(&self, headers: H, body: &[u8]) -> Result<Event>
where H: IntoIterator<Item = (&'a K, &'a V)>, K: AsRef<[u8]> + ?Sized + 'static, V: AsRef<[u8]> + ?Sized + 'static,

HTTP POSTリクエストをパースします。

§Arguments
  • headers - リクエストのヘッダー
  • body - リクエストのボディ
§Example
use traq_bot_http::{RequestParser, Event};
let headers = [
    ("Content-Type", "application/json"),
    ("X-TRAQ-BOT-TOKEN", "verification_token"),
    ("X-TRAQ-BOT-EVENT", "PING"),
];
let body = br#"{"eventTime": "2019-05-07T04:50:48.582586882Z"}"#;
let verification_token = "verification_token";
let parser = RequestParser::new(verification_token);
let event = parser.parse(headers, body);
assert!(matches!(event, Ok(Event::Ping(_))));
§Errors

Errorのうち、Error::kindが以下のものを返す可能性があります。

Source§

impl RequestParser

Source

pub fn into_handler(self) -> Handler<Sink>

Available on crate feature tower only.

イベントハンドラに変換します。

Note: この関数はtowerfeatureが有効になっている時のみ提供されます。

Trait Implementations§

Source§

impl Clone for RequestParser

Source§

fn clone(&self) -> RequestParser

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 RequestParser

Source§

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

Formats the value using the given formatter. 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, 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> 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.