Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[github] Fix compilation wiht httpcache feature enabled #63

Merged
merged 1 commit into from
Mar 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion generator/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2390,7 +2390,12 @@ pub enum ClientError {"#);
},
/// JWT errors from auth.rs
#[error(transparent)]
JsonWebTokenError(#[from] jsonwebtoken::errors::Error),"#);
JsonWebTokenError(#[from] jsonwebtoken::errors::Error),
/// IO Errors
#[cfg(feature = "httpcache")]
#[error(transparent)]
#[cfg(feature = "httpcache")]
IoError(#[from] std::io::Error),"#);
}
TemplateType::GenericApiKey | TemplateType::GenericClientCredentials => {
a(r#"/// utf8 convertion error
Expand Down
35 changes: 18 additions & 17 deletions github/src/http_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ use std::{
path::{Path, PathBuf},
};

use anyhow::{Error, Result};
use crate::ClientResult;
use crate::ClientError;
use http::Uri;

/// A type for an HTTP cache.
Expand All @@ -23,10 +24,10 @@ pub trait HttpCache: HttpCacheClone + Debug {
body: &[u8],
etag: &[u8],
next_link: &Option<String>,
) -> Result<()>;
fn lookup_etag(&self, uri: &str) -> Result<String>;
fn lookup_body(&self, uri: &str) -> Result<String>;
fn lookup_next_link(&self, uri: &str) -> Result<Option<String>>;
) -> ClientResult<()>;
fn lookup_etag(&self, uri: &str) -> ClientResult<String>;
fn lookup_body(&self, uri: &str) -> ClientResult<String>;
fn lookup_next_link(&self, uri: &str) -> ClientResult<Option<String>>;
}

impl dyn HttpCache {
Expand Down Expand Up @@ -60,16 +61,16 @@ impl Clone for BoxedHttpCache {
pub struct NoCache;

impl HttpCache for NoCache {
fn cache_response(&self, _: &str, _: &[u8], _: &[u8], _: &Option<String>) -> Result<()> {
fn cache_response(&self, _: &str, _: &[u8], _: &[u8], _: &Option<String>) -> ClientResult<()> {
Ok(())
}
fn lookup_etag(&self, _uri: &str) -> Result<String> {
fn lookup_etag(&self, _uri: &str) -> ClientResult<String> {
no_read("No etag cached")
}
fn lookup_body(&self, _uri: &str) -> Result<String> {
fn lookup_body(&self, _uri: &str) -> ClientResult<String> {
no_read("No body cached")
}
fn lookup_next_link(&self, _uri: &str) -> Result<Option<String>> {
fn lookup_next_link(&self, _uri: &str) -> ClientResult<Option<String>> {
no_read("No next link cached")
}
}
Expand All @@ -94,7 +95,7 @@ impl HttpCache for FileBasedCache {
body: &[u8],
etag: &[u8],
next_link: &Option<String>,
) -> Result<()> {
) -> ClientResult<()> {
let mut path = cache_path(&self.root, uri, "json");
//println!("caching body at path: {}", path.display());
if let Some(parent) = path.parent() {
Expand All @@ -110,15 +111,15 @@ impl HttpCache for FileBasedCache {
Ok(())
}

fn lookup_etag(&self, uri: &str) -> Result<String> {
fn lookup_etag(&self, uri: &str) -> ClientResult<String> {
read_to_string(cache_path(&self.root, uri, "etag"))
}

fn lookup_body(&self, uri: &str) -> Result<String> {
fn lookup_body(&self, uri: &str) -> ClientResult<String> {
read_to_string(cache_path(&self.root, uri, "json"))
}

fn lookup_next_link(&self, uri: &str) -> Result<Option<String>> {
fn lookup_next_link(&self, uri: &str) -> ClientResult<Option<String>> {
let path = cache_path(&self.root, uri, "next_link");
if path.exists() {
Ok(Some(read_to_string(path)?))
Expand Down Expand Up @@ -185,13 +186,13 @@ pub fn cache_path<S: AsRef<OsStr>>(dir: &Path, uri: &str, extension: S) -> PathB
path
}

fn read_to_string<P: AsRef<Path>>(path: P) -> Result<String> {
fn read_to_string<P: AsRef<Path>>(path: P) -> ClientResult<String> {
//println!("reading path: {}", path.as_ref().display());
fs::read_to_string(path).map_err(Error::from)
Ok(fs::read_to_string(path)?)
}

fn no_read<T, E: Into<Box<dyn std::error::Error + Send + Sync>>>(error: E) -> Result<T> {
Err(Error::from(io::Error::new(io::ErrorKind::NotFound, error)))
fn no_read<T, E: Into<Box<dyn std::error::Error + Send + Sync>>>(error: E) -> ClientResult<T> {
Err(ClientError::IoError(io::Error::new(io::ErrorKind::NotFound, error)))
}

// Separate to provide a blanket implementation for `T: HttpCache + Clone`
Expand Down