-
-
Notifications
You must be signed in to change notification settings - Fork 7.5k
Expand file tree
/
Copy pathlib.rs
More file actions
114 lines (86 loc) · 2.94 KB
/
lib.rs
File metadata and controls
114 lines (86 loc) · 2.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#![allow(missing_docs, trivial_casts, unused_variables, unused_mut, unused_imports, unused_extern_crates, unused_attributes, non_camel_case_types)]
#![allow(clippy::derive_partial_eq_without_eq, clippy::disallowed_names)]
use async_trait::async_trait;
use futures::Stream;
use std::error::Error;
use std::collections::BTreeSet;
use std::task::{Poll, Context};
use swagger::{ApiError, ContextWrapper};
use serde::{Serialize, Deserialize};
use crate::server::Authorization;
type ServiceError = Box<dyn Error + Send + Sync + 'static>;
pub const BASE_PATH: &str = "";
pub const API_VERSION: &str = "0.0.1";
mod auth;
pub use auth::{AuthenticationApi, Claims};
#[derive(Debug, PartialEq, Serialize, Deserialize)]
pub enum OpGetResponse {
/// OK
OK
}
/// API
#[async_trait]
#[allow(clippy::too_many_arguments, clippy::ptr_arg)]
pub trait Api<C: Send + Sync> {
fn poll_ready(&self, _cx: &mut Context) -> Poll<Result<(), Box<dyn Error + Send + Sync + 'static>>> {
Poll::Ready(Ok(()))
}
async fn op_get(
&self,
op_get_request: models::OpGetRequest,
context: &C) -> Result<OpGetResponse, ApiError>;
}
/// API where `Context` isn't passed on every API call
#[async_trait]
#[allow(clippy::too_many_arguments, clippy::ptr_arg)]
pub trait ApiNoContext<C: Send + Sync> {
fn poll_ready(&self, _cx: &mut Context) -> Poll<Result<(), Box<dyn Error + Send + Sync + 'static>>>;
fn context(&self) -> &C;
async fn op_get(
&self,
op_get_request: models::OpGetRequest,
) -> Result<OpGetResponse, ApiError>;
}
/// Trait to extend an API to make it easy to bind it to a context.
pub trait ContextWrapperExt<C: Send + Sync> where Self: Sized
{
/// Binds this API to a context.
fn with_context(self, context: C) -> ContextWrapper<Self, C>;
}
impl<T: Api<C> + Send + Sync, C: Clone + Send + Sync> ContextWrapperExt<C> for T {
fn with_context(self: T, context: C) -> ContextWrapper<T, C> {
ContextWrapper::<T, C>::new(self, context)
}
}
#[async_trait]
impl<T: Api<C> + Send + Sync, C: Clone + Send + Sync> ApiNoContext<C> for ContextWrapper<T, C> {
fn poll_ready(&self, cx: &mut Context) -> Poll<Result<(), ServiceError>> {
self.api().poll_ready(cx)
}
fn context(&self) -> &C {
ContextWrapper::context(self)
}
async fn op_get(
&self,
op_get_request: models::OpGetRequest,
) -> Result<OpGetResponse, ApiError>
{
let context = self.context().clone();
self.api().op_get(op_get_request, &context).await
}
}
#[cfg(feature = "client")]
pub mod client;
// Re-export Client as a top-level name
#[cfg(feature = "client")]
pub use client::Client;
#[cfg(feature = "server")]
pub mod server;
// Re-export router() as a top-level name
#[cfg(feature = "server")]
pub use self::server::Service;
#[cfg(feature = "server")]
pub mod context;
pub mod models;
#[cfg(any(feature = "client", feature = "server"))]
pub(crate) mod header;