mirror of
https://github.com/actix/actix-extras.git
synced 2025-06-25 01:51:23 +02:00
add Item and Error to FromRequest trait
This commit is contained in:
@ -6,14 +6,14 @@ A request handler can by any object that implements
|
||||
By default actix provdes several `Handler` implementations:
|
||||
|
||||
* Simple function that accepts `HttpRequest` and returns any object that
|
||||
can be converted to `HttpResponse`
|
||||
implements `FromRequest` trait
|
||||
* Function that accepts `HttpRequest` and returns `Result<Reply, Into<Error>>` object.
|
||||
* Function that accepts `HttpRequest` and return actor that has `HttpContext<A>`as a context.
|
||||
|
||||
Actix provides response conversion into `HttpResponse` for some standard types,
|
||||
Actix provides response `FromRequest` implementation for some standard types,
|
||||
like `&'static str`, `String`, etc.
|
||||
For complete list of implementations check
|
||||
[HttpResponse documentation](../actix_web/struct.HttpResponse.html#implementations).
|
||||
[FromRequest documentation](../actix_web/trait.FromRequest.html#foreign-impls).
|
||||
|
||||
Examples:
|
||||
|
||||
@ -58,19 +58,18 @@ struct MyObj {
|
||||
name: String,
|
||||
}
|
||||
|
||||
/// we have to convert Error into HttpResponse as well, but with
|
||||
/// specialization this could be handled genericly.
|
||||
impl Into<HttpResponse> for MyObj {
|
||||
fn into(self) -> HttpResponse {
|
||||
let body = match serde_json::to_string(&self) {
|
||||
Err(err) => return Error::from(err).into(),
|
||||
Ok(body) => body,
|
||||
};
|
||||
/// we have to convert Error into HttpResponse as well
|
||||
impl FromRequest for MyObj {
|
||||
type Item = HttpResponse;
|
||||
type Error = Error;
|
||||
|
||||
fn from_request(self, req: HttpRequest) -> Result<HttpResponse> {
|
||||
let body = serde_json::to_string(&self)?;
|
||||
|
||||
// Create response and set content type
|
||||
HttpResponse::Ok()
|
||||
Ok(HttpResponse::Ok()
|
||||
.content_type("application/json")
|
||||
.body(body).unwrap()
|
||||
.body(body)?)
|
||||
}
|
||||
}
|
||||
|
||||
@ -90,20 +89,6 @@ fn main() {
|
||||
}
|
||||
```
|
||||
|
||||
If `specialization` is enabled, conversion could be simplier:
|
||||
|
||||
```rust,ignore
|
||||
impl Into<Result<HttpResponse>> for MyObj {
|
||||
fn into(self) -> Result<HttpResponse> {
|
||||
let body = serde_json::to_string(&self)?;
|
||||
|
||||
Ok(HttpResponse::Ok()
|
||||
.content_type("application/json")
|
||||
.body(body)?)
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## Async handlers
|
||||
|
||||
There are two different types of async handlers.
|
||||
|
@ -65,11 +65,11 @@ used later in a request handler to access the matched value for that part. This
|
||||
done by looking up the identifier in the `HttpRequest.match_info` object:
|
||||
|
||||
```rust
|
||||
extern crate actix;
|
||||
extern crate actix_web;
|
||||
use actix_web::*;
|
||||
|
||||
fn index(req: Httprequest) -> String {
|
||||
format!("Hello, {}", req.match_info["name"])
|
||||
fn index(req: HttpRequest) -> String {
|
||||
format!("Hello, {}", &req.match_info()["name"])
|
||||
}
|
||||
|
||||
fn main() {
|
||||
@ -96,13 +96,13 @@ implements `FromParam` trait. For example most of standard integer types
|
||||
implements `FromParam` trait. i.e.:
|
||||
|
||||
```rust
|
||||
extern crate actix;
|
||||
extern crate actix_web;
|
||||
use actix_web::*;
|
||||
|
||||
fn index(req: Httprequest) -> String {
|
||||
fn index(req: HttpRequest) -> Result<String> {
|
||||
let v1: u8 = req.match_info().query("v1")?;
|
||||
let v2: u8 = req.match_info().query("v2")?;
|
||||
format!("Values {} {}", v1, v2)
|
||||
Ok(format!("Values {} {}", v1, v2))
|
||||
}
|
||||
|
||||
fn main() {
|
||||
@ -146,18 +146,18 @@ safe to interpolate within, or use as a suffix of, a path without additional
|
||||
checks.
|
||||
|
||||
```rust
|
||||
extern crate actix;
|
||||
extern crate actix_web;
|
||||
use actix_web::*;
|
||||
use std::path::PathBuf;
|
||||
|
||||
fn index(req: Httprequest) -> String {
|
||||
fn index(req: HttpRequest) -> Result<String> {
|
||||
let path: PathBuf = req.match_info().query("tail")?;
|
||||
format!("Path {:?}", path)
|
||||
Ok(format!("Path {:?}", path))
|
||||
}
|
||||
|
||||
fn main() {
|
||||
Application::default("/")
|
||||
.resource(r"/a/{tail:**}", |r| r.get(index))
|
||||
.resource(r"/a/{tail:*}", |r| r.get(index))
|
||||
.finish();
|
||||
}
|
||||
```
|
||||
|
Reference in New Issue
Block a user