-
Notifications
You must be signed in to change notification settings - Fork 3.7k
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
[API] Add Poem as an alternative backend #1906
Changes from all commits
7a0b10e
1b60317
2a79877
1c7af6c
3c5fcb1
80ce552
5a9e00d
6932152
104ba5e
4814a9c
c4fabdd
ff2e509
3028d9c
933569a
fbee6a4
e201c91
e25e250
a7675d5
32f11f6
78be793
ca5c73b
bbca085
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[package] | ||
name = "aptos-api" | ||
version = "0.1.0" | ||
version = "0.2.0" | ||
authors = ["Aptos Labs <[email protected]>"] | ||
description = "Aptos REST API" | ||
repository = "https://github.com/aptos-labs/aptos-core" | ||
|
@@ -17,12 +17,17 @@ fail = "0.5.0" | |
futures = "0.3.21" | ||
hex = "0.4.3" | ||
hyper = "0.14.18" | ||
mime = "0.3.16" | ||
once_cell = "1.10.0" | ||
percent-encoding = "2.1.0" | ||
poem = { version = "1.3.35", features = ["anyhow", "rustls"] } | ||
poem-openapi = { version = "2.0.5", features = ["swagger-ui", "url"] } | ||
serde = { version = "1.0.137", features = ["derive"], default-features = false } | ||
serde_json = { version = "1.0.81", features = ["preserve_order"] } | ||
tokio = { version = "1.18.2", features = ["full"] } | ||
url = "2.2.2" | ||
warp = { version = "0.3.2", features = ["default", "tls"] } | ||
warp-reverse-proxy = "0.5.0" | ||
|
||
aptos-api-types = { path = "./types", package = "aptos-api-types" } | ||
aptos-config = { path = "../config" } | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,28 @@ | ||
// Copyright (c) Aptos | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
#[allow(unused_imports)] | ||
#![allow(unused_imports)] | ||
|
||
use anyhow::{format_err, Result}; | ||
use aptos_api_types::Error; | ||
|
||
use crate::poem_backend::{AptosError, AptosErrorResponse}; | ||
use poem_openapi::payload::Json; | ||
|
||
#[allow(unused_variables)] | ||
#[inline] | ||
pub fn fail_point(name: &str) -> Result<(), Error> { | ||
Ok(fail::fail_point!(format!("api::{}", name).as_str(), |_| { | ||
Err(format_err!("unexpected internal error for {}", name).into()) | ||
})) | ||
} | ||
|
||
#[allow(unused_variables)] | ||
#[inline] | ||
pub fn fail_point_poem(name: &str) -> Result<(), AptosErrorResponse> { | ||
Ok(fail::fail_point!(format!("api::{}", name).as_str(), |_| { | ||
Err(AptosErrorResponse::InternalServerError(Json( | ||
AptosError::new(format!("unexpected internal error for {}", name)), | ||
))) | ||
})) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// Copyright (c) Aptos | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
use std::convert::TryFrom; | ||
|
||
use poem::web::Accept; | ||
use poem_openapi::payload::Json; | ||
|
||
use super::{AptosError, AptosErrorCode, AptosErrorResponse}; | ||
|
||
#[derive(PartialEq)] | ||
pub enum AcceptType { | ||
Json, | ||
Bcs, | ||
} | ||
|
||
impl TryFrom<&Accept> for AcceptType { | ||
type Error = AptosErrorResponse; | ||
|
||
fn try_from(accept: &Accept) -> Result<Self, Self::Error> { | ||
for mime in &accept.0 { | ||
match mime.as_ref() { | ||
"application/json" => return Ok(AcceptType::Json), | ||
"application/x-bcs" => return Ok(AcceptType::Bcs), | ||
"*/*" => {} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm, does it default to this? or would "application/*" be valid? Basically, if I curl the endpoint, do I need to specify the header? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You do not need to specify the header, it'll be json by default. You need to not error on |
||
wildcard => { | ||
return Err(AptosErrorResponse::BadRequest(Json( | ||
AptosError::new(format!("Invalid Accept type: {:?}", wildcard)) | ||
.error_code(AptosErrorCode::UnsupportedAcceptType), | ||
))); | ||
} | ||
} | ||
} | ||
|
||
// Default to returning content as JSON. | ||
Ok(AcceptType::Json) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can probably just make these
format!
if you're converting them to a string anyways.I do like this adding error codes though super slick.
If you're adding
Json(
to everything, I'd just make it a functionAptosErrorResponse::internal_server_error(err: AptosError)
, orAptosErrorResponse::internal_server_error(msg: String, error_code: AptosErrorCode)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The response / error stuff definitely needs love, I'm just deferring it to later as less critical. Agreed it's too verbose right now.