use futures::{future, future::BoxFuture, Stream, stream, future::FutureExt, stream::TryStreamExt}; use hyper::{Request, Response, StatusCode, Body, HeaderMap}; use hyper::header::{HeaderName, HeaderValue, CONTENT_TYPE}; use log::warn; #[allow(unused_imports)] use std::convert::{TryFrom, TryInto}; use std::error::Error; use std::future::Future; use std::marker::PhantomData; use std::task::{Context, Poll}; use swagger::{ApiError, BodyExt, Has, RequestParser, XSpanIdString}; pub use swagger::auth::Authorization; use swagger::auth::Scopes; use url::form_urlencoded; #[allow(unused_imports)] use crate::models; use crate::header; pub use crate::context; type ServiceFuture = BoxFuture<'static, Result, crate::ServiceError>>; use crate::{Api }; use chrono::NaiveDate; mod paths { use lazy_static::lazy_static; lazy_static! { pub static ref GLOBAL_REGEX_SET: regex::RegexSet = regex::RegexSet::new(vec![ ]) .expect("Unable to create global regex set"); } } pub struct MakeService where T: Api + Clone + Send + 'static, C: Has + Send + Sync + 'static { api_impl: T, marker: PhantomData, } impl MakeService where T: Api + Clone + Send + 'static, C: Has + Send + Sync + 'static { pub fn new(api_impl: T) -> Self { MakeService { api_impl, marker: PhantomData } } } impl hyper::service::Service for MakeService where T: Api + Clone + Send + 'static, C: Has + Send + Sync + 'static { type Response = Service; type Error = crate::ServiceError; type Future = future::Ready>; fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll> { Poll::Ready(Ok(())) } fn call(&mut self, target: Target) -> Self::Future { futures::future::ok(Service::new( self.api_impl.clone(), )) } } fn method_not_allowed() -> Result, crate::ServiceError> { Ok( Response::builder().status(StatusCode::METHOD_NOT_ALLOWED) .body(Body::empty()) .expect("Unable to create Method Not Allowed response") ) } pub struct Service where T: Api + Clone + Send + 'static, C: Has + Send + Sync + 'static { api_impl: T, marker: PhantomData, } impl Service where T: Api + Clone + Send + 'static, C: Has + Send + Sync + 'static { pub fn new(api_impl: T) -> Self { Service { api_impl: api_impl, marker: PhantomData } } } impl Clone for Service where T: Api + Clone + Send + 'static, C: Has + Send + Sync + 'static { fn clone(&self) -> Self { Service { api_impl: self.api_impl.clone(), marker: self.marker.clone(), } } } impl hyper::service::Service<(Request, C)> for Service where T: Api + Clone + Send + Sync + 'static, C: Has + Send + Sync + 'static { type Response = Response; type Error = crate::ServiceError; type Future = ServiceFuture; fn poll_ready(&mut self, cx: &mut Context) -> Poll> { self.api_impl.poll_ready(cx) } fn call(&mut self, req: (Request, C)) -> Self::Future { async fn run(mut api_impl: T, req: (Request, C)) -> Result, crate::ServiceError> where T: Api + Clone + Send + 'static, C: Has + Send + Sync + 'static { let (request, context) = req; let (parts, body) = request.into_parts(); let (method, uri, headers) = (parts.method, parts.uri, parts.headers); let path = paths::GLOBAL_REGEX_SET.matches(uri.path()); match &method { _ => Ok(Response::builder().status(StatusCode::NOT_FOUND) .body(Body::empty()) .expect("Unable to create Not Found response")) } } Box::pin(run(self.api_impl.clone(), req)) } } /// Request parser for `Api`. pub struct ApiRequestParser; impl RequestParser for ApiRequestParser { fn parse_operation_id(request: &Request) -> Result<&'static str, ()> { let path = paths::GLOBAL_REGEX_SET.matches(request.uri().path()); match request.method() { _ => Err(()), } } }