pub struct RequestParser { /* private fields */ }
Expand description
HTTP POSTリクエストのパーサー
Implementations§
Source§impl RequestParser
impl RequestParser
Sourcepub fn parse_request<B>(&self, request: Request<B>) -> ParseRequest<B> ⓘ
Available on crate feature http
only.
pub fn parse_request<B>(&self, request: Request<B>) -> ParseRequest<B> ⓘ
http
only.http::Request
をパースします。
Note: この関数はhttp
featureが有効になっている時のみ有効です。
§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
が以下のものを返す可能性があります。
parse
で返されるものErrorKind::ReadBodyFailed
: リクエストボディの読み込みに失敗した
Source§impl RequestParser
impl RequestParser
Sourcepub fn parse_headers<'a, H, K, V>(&self, headers: H) -> Result<EventKind>
pub fn parse_headers<'a, H, K, V>(&self, headers: H) -> Result<EventKind>
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
が以下のものを返す可能性があります。
ErrorKind::ReadContentTypeFailed
: ヘッダーContent-Type
の値をUTF8の文字列として解釈できなかったErrorKind::ContentTypeNotFound
: ヘッダーContent-Type
が見つからなかったErrorKind::ContentTypeMismatch
: ヘッダーContent-Type
の値がapplication/json
で始まらないErrorKind::ReadBotTokenFailed
: ヘッダーX-TRAQ-BOT-TOKEN
の値に関して、以下のいずれかの場合- 値をUTF8の文字列として解釈できなかった
- 値が
visible US-ASCII octets (VCHAR)
,SP
,HTAB
以外の文字を含む (RFC9110 5.5)
ErrorKind::BotTokenNotFound
: ヘッダーX-TRAQ-BOT-TOKEN
が見つからなかったErrorKind::BotTokenMismatch
: ヘッダーX-TRAQ-BOT-TOKEN
の値がnew
で与えられたVerification Tokenと合わないErrorKind::ReadBotEventFailed
: ヘッダーX-TRAQ-BOT-EVENT
の値に関して、以下のいずれかの場合- 値をUTF8の文字列として解釈できなかった
- 値が
visible US-ASCII octets (VCHAR)
,SP
,HTAB
以外の文字を含む (RFC9110 5.5)
ErrorKind::BotEventNotFound
: ヘッダーX-TRAQ-BOT-EVENT
が見つからなかったErrorKind::BotEventMismatch
: ヘッダーX-TRAQ-BOT-EVENT
の値がEventKind
のstd::str::FromStr
でパースできなかった
Sourcepub fn parse<'a, H, K, V>(&self, headers: H, body: &[u8]) -> Result<Event>
pub fn parse<'a, H, K, V>(&self, headers: H, body: &[u8]) -> Result<Event>
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
が以下のものを返す可能性があります。
parse_headers
で返されるものErrorKind::ReadBodyFailed
:body
をUTF8の文字列として解釈できなかったErrorKind::ParseBodyFailed
:body
をparse_headers
で返されるEventKind
に対応するEvent
のペイロードJSONとしてデシリアライズできなかった。
Source§impl RequestParser
impl RequestParser
Sourcepub fn into_handler(self) -> Handler<Sink>
Available on crate feature tower
only.
pub fn into_handler(self) -> Handler<Sink>
tower
only.イベントハンドラに変換します。
Note: この関数はtower
featureが有効になっている時のみ提供されます。
Trait Implementations§
Source§impl Clone for RequestParser
impl Clone for RequestParser
Source§fn clone(&self) -> RequestParser
fn clone(&self) -> RequestParser
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source
. Read moreAuto Trait Implementations§
impl Freeze for RequestParser
impl RefUnwindSafe for RequestParser
impl Send for RequestParser
impl Sync for RequestParser
impl Unpin for RequestParser
impl UnwindSafe for RequestParser
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more