From 79869b50325acf9255670cb61d71d5a9959e263d Mon Sep 17 00:00:00 2001 From: Koxiaet <38139193+Koxiaet@users.noreply.github.com> Date: Wed, 23 Sep 2020 17:30:22 +0100 Subject: [PATCH] Small improvements to actix-web integration --- integrations/actix-web/src/batch_request.rs | 25 ++++++------- integrations/actix-web/src/request.rs | 39 +++++++++++---------- 2 files changed, 30 insertions(+), 34 deletions(-) diff --git a/integrations/actix-web/src/batch_request.rs b/integrations/actix-web/src/batch_request.rs index dbba8260..9c47a292 100644 --- a/integrations/actix-web/src/batch_request.rs +++ b/integrations/actix-web/src/batch_request.rs @@ -1,4 +1,4 @@ -use actix_web::dev::{HttpResponseBuilder, Payload, PayloadStream}; +use actix_web::dev::{Payload, PayloadStream}; use actix_web::http::StatusCode; use actix_web::{http, web, Error, FromRequest, HttpRequest, HttpResponse, Responder}; use async_graphql::http::MultipartOptions; @@ -10,14 +10,14 @@ use futures::{Future, SinkExt, StreamExt, TryFutureExt, TryStreamExt}; use std::io; use std::pin::Pin; -/// Extractor for GraphQL batch request +/// Extractor for GraphQL batch request. /// -/// It's a wrapper of `async_graphql::Request`, you can use `Request::into_inner` unwrap it to `async_graphql::Request`. /// `async_graphql::http::MultipartOptions` allows to configure extraction process. -pub struct BatchRequest(async_graphql::BatchRequest); +pub struct BatchRequest(pub async_graphql::BatchRequest); impl BatchRequest { /// Unwraps the value to `async_graphql::Request`. + #[must_use] pub fn into_inner(self) -> async_graphql::BatchRequest { self.0 } @@ -70,7 +70,7 @@ impl FromRequest for BatchRequest { } /// Responder for GraphQL batch response -pub struct BatchResponse(async_graphql::BatchResponse); +pub struct BatchResponse(pub async_graphql::BatchResponse); impl From for BatchResponse { fn from(resp: async_graphql::BatchResponse) -> Self { @@ -85,16 +85,11 @@ impl Responder for BatchResponse { fn respond_to(self, _req: &HttpRequest) -> Self::Future { let mut res = HttpResponse::build(StatusCode::OK); res.content_type("application/json"); - add_cache_control(&mut res, &self.0); - let res = res.body(serde_json::to_string(&self.0).unwrap()); - futures::future::ok(res) - } -} - -fn add_cache_control(builder: &mut HttpResponseBuilder, resp: &async_graphql::BatchResponse) { - if resp.is_ok() { - if let Some(cache_control) = resp.cache_control().value() { - builder.header("cache-control", cache_control); + if self.0.is_ok() { + if let Some(cache_control) = self.0.cache_control().value() { + res.header("cache-control", cache_control); + } } + futures::future::ok(res.body(serde_json::to_string(&self.0).unwrap())) } } diff --git a/integrations/actix-web/src/request.rs b/integrations/actix-web/src/request.rs index e2b2ce2a..c0e29472 100644 --- a/integrations/actix-web/src/request.rs +++ b/integrations/actix-web/src/request.rs @@ -1,4 +1,4 @@ -use actix_web::dev::{HttpResponseBuilder, Payload, PayloadStream}; +use actix_web::dev::{Payload, PayloadStream}; use actix_web::http::StatusCode; use actix_web::{http, web, Error, FromRequest, HttpRequest, HttpResponse, Responder}; use async_graphql::http::MultipartOptions; @@ -6,24 +6,30 @@ use async_graphql::ParseRequestError; use futures::channel::mpsc; use futures::future::Ready; use futures::io::ErrorKind; -use futures::{Future, SinkExt, StreamExt, TryFutureExt, TryStreamExt}; +use futures::{Future, SinkExt, StreamExt, TryStreamExt}; use http::Method; use std::io; use std::pin::Pin; -/// Extractor for GraphQL request +/// Extractor for GraphQL request. /// -/// It's a wrapper of `async_graphql::Request`, you can use `Request::into_inner` unwrap it to `async_graphql::Request`. /// `async_graphql::http::MultipartOptions` allows to configure extraction process. -pub struct Request(async_graphql::Request); +pub struct Request(pub async_graphql::Request); impl Request { /// Unwraps the value to `async_graphql::Request`. + #[must_use] pub fn into_inner(self) -> async_graphql::Request { self.0 } } +impl From for async_graphql::Request { + fn from(req: Request) -> Self { + req.0 + } +} + impl FromRequest for Request { type Error = Error; type Future = Pin>>>; @@ -65,21 +71,21 @@ impl FromRequest for Request { .into_async_read(), config, ) + .await .map_err(|err| match err { ParseRequestError::PayloadTooLarge => { actix_web::error::ErrorPayloadTooLarge(err) } _ => actix_web::error::ErrorBadRequest(err), - }) - .await?, + })?, )) }) } } } -/// Responder for GraphQL response -pub struct Response(async_graphql::Response); +/// Responder for GraphQL response. +pub struct Response(pub async_graphql::Response); impl From for Response { fn from(resp: async_graphql::Response) -> Self { @@ -94,16 +100,11 @@ impl Responder for Response { fn respond_to(self, _req: &HttpRequest) -> Self::Future { let mut res = HttpResponse::build(StatusCode::OK); res.content_type("application/json"); - add_cache_control(&mut res, &self.0); - let res = res.body(serde_json::to_string(&self.0).unwrap()); - futures::future::ok(res) - } -} - -fn add_cache_control(builder: &mut HttpResponseBuilder, resp: &async_graphql::Response) { - if resp.is_ok() { - if let Some(cache_control) = resp.cache_control.value() { - builder.header("cache-control", cache_control); + if self.0.is_ok() { + if let Some(cache_control) = self.0.cache_control.value() { + res.header("cache-control", cache_control); + } } + futures::future::ok(res.body(serde_json::to_string(&self.0).unwrap())) } }