mirror of
https://github.com/OMGeeky/google-apis-rs.git
synced 2026-02-15 22:18:10 +01:00
Remove breaking changes, add TODOs
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
mako==1.2.2
|
||||
pyyaml<6
|
||||
mkdocs==0.11
|
||||
mkdocs==1.3.1
|
||||
pytest
|
||||
pytest-cov
|
||||
codecov
|
||||
|
||||
@@ -32,7 +32,9 @@ mime = "^ 0.2.0"
|
||||
serde = "^ 1.0"
|
||||
serde_json = "^ 1.0"
|
||||
serde_derive = "^ 1.0"
|
||||
yup-oauth2 = { version = "^ 7.0", optional = true }
|
||||
## TODO: Make yup-oauth2 optional
|
||||
## yup-oauth2 = { version = "^ 7.0", optional = true }
|
||||
yup-oauth2 = "^ 7.0"
|
||||
itertools = "^ 0.10"
|
||||
% for dep in cargo.get('dependencies', list()):
|
||||
${dep}
|
||||
@@ -54,5 +56,6 @@ path = "../${api_name}"
|
||||
version = "${util.crate_version()}"
|
||||
% endif
|
||||
|
||||
[features]
|
||||
default = ["yup-oauth2"]
|
||||
## TODO: Make yup-oauth2 optional
|
||||
# [features]
|
||||
# default = ["yup-oauth2"]
|
||||
|
||||
@@ -30,7 +30,7 @@ use http::Uri;
|
||||
use hyper::client::connect;
|
||||
use tokio::io::{AsyncRead, AsyncWrite};
|
||||
use tower_service;
|
||||
use crate::{client, client::Authy};
|
||||
use crate::{client, client::GetToken};
|
||||
|
||||
// ##############
|
||||
// UTILITIES ###
|
||||
@@ -55,7 +55,7 @@ ${lib.hub_usage_example(c)}\
|
||||
#[derive(Clone)]
|
||||
pub struct ${hub_type}${ht_params} {
|
||||
pub client: hyper::Client<S, hyper::body::Body>,
|
||||
pub auth: Box<dyn client::Authy>,
|
||||
pub auth: Box<dyn client::GetToken>,
|
||||
_user_agent: String,
|
||||
_base_url: String,
|
||||
_root_url: String,
|
||||
@@ -65,7 +65,7 @@ impl<'a, ${', '.join(HUB_TYPE_PARAMETERS)}> client::Hub for ${hub_type}${ht_para
|
||||
|
||||
impl<'a, ${', '.join(HUB_TYPE_PARAMETERS)}> ${hub_type}${ht_params} {
|
||||
|
||||
pub fn new<A: 'static + client::Authy>(client: hyper::Client<S, hyper::body::Body>, auth: A) -> ${hub_type}${ht_params} {
|
||||
pub fn new<A: 'static + client::GetToken>(client: hyper::Client<S, hyper::body::Body>, auth: A) -> ${hub_type}${ht_params} {
|
||||
${hub_type} {
|
||||
client,
|
||||
auth: Box::new(auth),
|
||||
|
||||
@@ -707,13 +707,24 @@ else {
|
||||
loop {
|
||||
% if default_scope:
|
||||
let token = match ${auth_call}.get_token(&self.${api.properties.scopes}.keys().map(String::as_str).collect::<Vec<_>>()[..]).await {
|
||||
Some(token) => token.clone(),
|
||||
None => {
|
||||
match dlg.token() {
|
||||
// TODO: remove Ok / Err branches
|
||||
Ok(Some(token)) => token.clone(),
|
||||
Ok(None) => {
|
||||
let err = oauth2::OtherError("unknown error occurred while generating oauth2 token".into());
|
||||
match dlg.token(&err) {
|
||||
Some(token) => token,
|
||||
None => {
|
||||
${delegate_finish}(false);
|
||||
return Err(client::Error::MissingToken)
|
||||
return Err(client::Error::MissingToken(err))
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(err) => {
|
||||
match dlg.token(&err) {
|
||||
Some(token) => token,
|
||||
None => {
|
||||
${delegate_finish}(false);
|
||||
return Err(client::Error::MissingToken(err))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -107,12 +107,14 @@ pub trait Delegate: Send {
|
||||
None
|
||||
}
|
||||
|
||||
// TODO: Remove oauth2::Error
|
||||
/// Called whenever the Authenticator didn't yield a token. The delegate
|
||||
/// may attempt to provide one, or just take it as a general information about the
|
||||
/// impending failure.
|
||||
/// The given Error provides information about why the token couldn't be acquired in the
|
||||
/// first place
|
||||
fn token(&mut self) -> Option<String> {
|
||||
fn token(&mut self, err: &oauth2::Error) -> Option<String> {
|
||||
let _ = err;
|
||||
None
|
||||
}
|
||||
|
||||
@@ -228,8 +230,9 @@ pub enum Error {
|
||||
/// Neither through the authenticator, nor through the Delegate.
|
||||
MissingAPIKey,
|
||||
|
||||
// TODO: Remove oauth2::Error
|
||||
/// We required a Token, but didn't get one from the Authenticator
|
||||
MissingToken,
|
||||
MissingToken(oauth2::Error),
|
||||
|
||||
/// The delgate instructed to cancel the operation
|
||||
Cancelled,
|
||||
@@ -273,8 +276,9 @@ impl Display for Error {
|
||||
writeln!(f, "Bad Request: {}", message)?;
|
||||
Ok(())
|
||||
}
|
||||
Error::MissingToken => {
|
||||
writeln!(f, "Token retrieval failed.")
|
||||
// TODO: Remove oauth2::Error
|
||||
Error::MissingToken(ref err) => {
|
||||
writeln!(f, "Token retrieval failed with error: {}", err)
|
||||
}
|
||||
Error::Cancelled => writeln!(f, "Operation cancelled by delegate"),
|
||||
Error::FieldClash(field) => writeln!(
|
||||
@@ -787,52 +791,52 @@ pub async fn get_body_as_string(res_body: &mut hyper::Body) -> String {
|
||||
res_body_string.to_string()
|
||||
}
|
||||
|
||||
pub trait Authy: AuthyClone {
|
||||
fn get_token<'a>(&'a self, _scopes: &'a [&str]) -> Pin<Box<dyn Future<Output=Option<String>> + 'a>> {
|
||||
async fn x() -> Option<String> {
|
||||
None
|
||||
}
|
||||
// TODO: Simplify this to Option<String>
|
||||
type TokenResult = std::result::Result<Option<String>, oauth2::Error>;
|
||||
|
||||
Box::pin(x())
|
||||
pub trait GetToken: GetTokenClone {
|
||||
/// Called whenever there is the need for your applications API key after
|
||||
/// the official authenticator implementation didn't provide one, for some reason.
|
||||
/// If this method returns None as well, the underlying operation will fail
|
||||
fn get_token<'a>(&'a self, _scopes: &'a [&str]) -> Pin<Box<dyn Future<Output=TokenResult> + 'a>> {
|
||||
Box::pin(async move { Ok(None) })
|
||||
}
|
||||
}
|
||||
|
||||
pub trait AuthyClone {
|
||||
fn clone_box(&self) -> Box<dyn Authy>;
|
||||
pub trait GetTokenClone {
|
||||
fn clone_box(&self) -> Box<dyn GetToken>;
|
||||
}
|
||||
|
||||
impl<T> AuthyClone for T
|
||||
impl<T> GetTokenClone for T
|
||||
where
|
||||
T: 'static + Authy + Clone,
|
||||
T: 'static + GetToken + Clone,
|
||||
{
|
||||
fn clone_box(&self) -> Box<dyn Authy> {
|
||||
fn clone_box(&self) -> Box<dyn GetToken> {
|
||||
Box::new(self.clone())
|
||||
}
|
||||
}
|
||||
|
||||
impl Clone for Box<dyn Authy> {
|
||||
fn clone(&self) -> Box<dyn Authy> {
|
||||
impl Clone for Box<dyn GetToken> {
|
||||
fn clone(&self) -> Box<dyn GetToken> {
|
||||
self.clone_box()
|
||||
}
|
||||
}
|
||||
|
||||
impl Authy for String {
|
||||
fn get_token<'a>(&'a self, _scopes: &'a [&str]) -> Pin<Box<dyn Future<Output=Option<String>> + 'a>> {
|
||||
async fn ident(s: &str) -> Option<String> {
|
||||
Some(s.to_string())
|
||||
}
|
||||
Box::pin(ident(self))
|
||||
impl GetToken for String {
|
||||
fn get_token<'a>(&'a self, _scopes: &'a [&str]) -> Pin<Box<dyn Future<Output=TokenResult> + 'a>> {
|
||||
Box::pin(async move { Ok(Some(self.clone())) })
|
||||
}
|
||||
}
|
||||
|
||||
impl Authy for () {}
|
||||
impl GetToken for () {}
|
||||
|
||||
#[cfg(feature = "yup-oauth2")]
|
||||
// TODO: Make this optional
|
||||
// #[cfg(feature = "yup-oauth2")]
|
||||
mod yup_oauth2_impl {
|
||||
use core::future::Future;
|
||||
use core::pin::Pin;
|
||||
|
||||
use super::Authy;
|
||||
use super::{GetToken, TokenResult};
|
||||
|
||||
use tower_service::Service;
|
||||
use yup_oauth2::authenticator::Authenticator;
|
||||
@@ -840,21 +844,16 @@ mod yup_oauth2_impl {
|
||||
use http::Uri;
|
||||
use hyper::client::connect::Connection;
|
||||
|
||||
async fn helper<S>(auth: &Authenticator<S>, scopes: &[&str]) -> Option<String> where
|
||||
S: Service<Uri> + Clone + Send + Sync + 'static,
|
||||
S::Response: Connection + AsyncRead + AsyncWrite + Send + Unpin + 'static,
|
||||
S::Future: Send + Unpin + 'static,
|
||||
S::Error: Into<Box<dyn std::error::Error + Send + Sync>> {
|
||||
auth.token(scopes).await.ok().map(|t| t.as_str().to_string())
|
||||
}
|
||||
|
||||
impl<S> Authy for Authenticator<S> where
|
||||
impl<S> GetToken for Authenticator<S> where
|
||||
S: Service<Uri> + Clone + Send + Sync + 'static,
|
||||
S::Response: Connection + AsyncRead + AsyncWrite + Send + Unpin + 'static,
|
||||
S::Future: Send + Unpin + 'static,
|
||||
S::Error: Into<Box<dyn std::error::Error + Send + Sync>> {
|
||||
fn get_token<'a>(&'a self, scopes: &'a [&str]) -> Pin<Box<dyn Future<Output=Option<String>> + 'a>> {
|
||||
Box::pin(helper(self, scopes))
|
||||
fn get_token<'a>(&'a self, scopes: &'a [&str]) -> Pin<Box<dyn Future<Output=TokenResult> + 'a>> {
|
||||
Box::pin(async move {
|
||||
self.token(scopes).await.map(|t| Some(t.as_str().to_owned()))
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user