-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[API] Add support for index with Poem backend
- Loading branch information
Showing
12 changed files
with
114 additions
and
26 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
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" | ||
|
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 |
---|---|---|
@@ -1,9 +1,70 @@ | ||
// Copyright (c) Aptos | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
use poem_openapi::OpenApi; | ||
use crate::context::Context; | ||
use aptos_api_types::IndexResponse; | ||
use poem::{http::StatusCode, Error as PoemError, Result as PoemResult}; | ||
use poem_openapi::{ | ||
payload::{Html, Json}, | ||
OpenApi, Tags, | ||
}; | ||
|
||
pub struct Api {} | ||
const OPEN_API_HTML: &str = include_str!("../../doc/spec.html"); | ||
|
||
pub struct Api { | ||
context: Context, | ||
} | ||
|
||
impl Api { | ||
pub fn new(context: Context) -> Self { | ||
Self { context } | ||
} | ||
} | ||
|
||
// TODO: Move these impls throughout each of the files in the parent directory. | ||
// The only reason I do it here right now is the existing handler functions return | ||
// opaque reply objects and therefore I can't re-use them, so I'd have to pollute | ||
// those files with these impls below. | ||
|
||
// TODO: Consider using swagger UI here instead since it's built in, though | ||
// the UI is much worse. I could look into adding the Elements UI to Poem. | ||
|
||
#[derive(Tags)] | ||
enum ApiTags { | ||
/// General information. | ||
General, | ||
} | ||
|
||
#[OpenApi] | ||
impl Api {} | ||
impl Api { | ||
/// get_ledger_info | ||
/// | ||
/// Get the latest ledger information, including data such as chain ID, role type, ledger versions, epoch, etc. | ||
#[oai( | ||
path = "/", | ||
method = "get", | ||
operation_id = "get_ledger_info", | ||
tag = "ApiTags::General" | ||
)] | ||
async fn get_ledger_info(&self) -> PoemResult<Json<IndexResponse>> { | ||
let ledger_info = self.context.get_latest_ledger_info().map_err(|e| { | ||
PoemError::from((StatusCode::INTERNAL_SERVER_ERROR, anyhow::anyhow!(e))) | ||
})?; | ||
let node_role = self.context.node_role(); | ||
let index_response = IndexResponse::new(ledger_info, node_role); | ||
Ok(Json(index_response)) | ||
} | ||
|
||
/// openapi | ||
/// | ||
/// Provides a UI that you can use to explore the API. You can also retrieve the API directly at `/openapi.yaml` and `/openapi.json`. | ||
#[oai( | ||
path = "/openapi", | ||
method = "get", | ||
operation_id = "openapi", | ||
tag = "ApiTags::General" | ||
)] | ||
async fn openapi(&self) -> Html<String> { | ||
Html(OPEN_API_HTML.to_string()) | ||
} | ||
} |
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 |
---|---|---|
@@ -1,2 +1,5 @@ | ||
// Copyright (c) Aptos | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
pub mod api; | ||
pub mod runtime; |
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
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
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
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