-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: API endpoints to query historical pair data
- Loading branch information
1 parent
15e15af
commit 0413635
Showing
17 changed files
with
782 additions
and
20 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
use axum::body::Body; | ||
use axum::extract::Request; | ||
use axum::http::header::CONTENT_TYPE; | ||
use axum::http::StatusCode; | ||
use axum::middleware::Next; | ||
use axum::response::{IntoResponse, Response}; | ||
use axum::Json; | ||
use serde::Serialize; | ||
|
||
#[derive(Serialize)] | ||
pub struct ApiError { | ||
pub error: String, | ||
} | ||
|
||
pub struct AxumError(anyhow::Error); | ||
|
||
impl IntoResponse for AxumError { | ||
fn into_response(self) -> Response { | ||
( | ||
StatusCode::INTERNAL_SERVER_ERROR, | ||
Json(ApiError { | ||
error: format!("{}", self.0), | ||
}), | ||
) | ||
.into_response() | ||
} | ||
} | ||
|
||
impl<E> From<E> for AxumError | ||
where | ||
E: Into<anyhow::Error>, | ||
{ | ||
fn from(err: E) -> Self { | ||
Self(err.into()) | ||
} | ||
} | ||
|
||
pub async fn error_middleware(request: Request<Body>, next: Next) -> Response<Body> { | ||
let response = next.run(request).await; | ||
|
||
if response.status().is_server_error() || response.status().is_client_error() { | ||
if let Some(content_type) = response.headers().get(CONTENT_TYPE) { | ||
// If the Content-Type is not JSON, make it JSON | ||
if content_type != "application/json" { | ||
let (parts, body) = response.into_parts(); | ||
let body_str = match axum::body::to_bytes(body, 8192).await { | ||
Ok(bytes) => match std::str::from_utf8(&bytes) { | ||
Ok(str) => str.to_string(), | ||
Err(_) => return Response::from_parts(parts, Body::from(bytes)), | ||
}, | ||
Err(err) => { | ||
return ( | ||
StatusCode::INTERNAL_SERVER_ERROR, | ||
Json(ApiError { | ||
error: format!("could not handle body: {}", err), | ||
}), | ||
) | ||
.into_response() | ||
} | ||
}; | ||
|
||
return (parts.status, Json(ApiError { error: body_str })).into_response(); | ||
} | ||
} | ||
} | ||
|
||
response | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
use axum_extra::headers::{Error, HeaderName, HeaderValue}; | ||
|
||
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] | ||
pub struct Referral(String); | ||
|
||
impl Referral { | ||
pub fn inner(&self) -> &str { | ||
&self.0 | ||
} | ||
} | ||
|
||
impl axum_extra::headers::Header for Referral { | ||
fn name() -> &'static HeaderName { | ||
static NAME: HeaderName = HeaderName::from_static("referral"); | ||
&NAME | ||
} | ||
|
||
fn decode<'i, I>(values: &mut I) -> Result<Self, Error> | ||
where | ||
Self: Sized, | ||
I: Iterator<Item = &'i HeaderValue>, | ||
{ | ||
let value = values.next().ok_or_else(Error::invalid)?; | ||
value | ||
.to_str() | ||
.map_err(|_| Error::invalid()) | ||
.map(|value| Self(value.to_owned())) | ||
} | ||
|
||
fn encode<E: Extend<HeaderValue>>(&self, values: &mut E) { | ||
values.extend(std::iter::once( | ||
HeaderValue::from_str(&self.0).expect("invalid header value"), | ||
)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.