Prettify API a bit
This commit is contained in:
parent
66e62d61e4
commit
8cb368e73c
2 changed files with 102 additions and 101 deletions
|
|
@ -1,5 +1,5 @@
|
||||||
use anyhow::Context;
|
use anyhow::Context;
|
||||||
use axum::extract::{Query, State};
|
use axum::extract::{Path, Query, State};
|
||||||
use axum::http::StatusCode;
|
use axum::http::StatusCode;
|
||||||
use axum::response::IntoResponse;
|
use axum::response::IntoResponse;
|
||||||
use buffers::ByteString;
|
use buffers::ByteString;
|
||||||
|
|
@ -19,6 +19,9 @@ use crate::session::{AddTorrentOptions, AddTorrentResponse, ListOnlyResponse, Se
|
||||||
use crate::torrent_manager::TorrentManagerHandle;
|
use crate::torrent_manager::TorrentManagerHandle;
|
||||||
use crate::torrent_state::StatsSnapshot;
|
use crate::torrent_state::StatsSnapshot;
|
||||||
|
|
||||||
|
type Result<T> = std::result::Result<T, ApiError>;
|
||||||
|
|
||||||
|
// Private HTTP API internals.
|
||||||
pub struct ApiInternal {
|
pub struct ApiInternal {
|
||||||
dht: Option<Dht>,
|
dht: Option<Dht>,
|
||||||
startup_time: Instant,
|
startup_time: Instant,
|
||||||
|
|
@ -26,23 +29,24 @@ pub struct ApiInternal {
|
||||||
session: Arc<Session>,
|
session: Arc<Session>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Convenience error type.
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
struct Error {
|
struct ApiError {
|
||||||
status: Option<StatusCode>,
|
status: Option<StatusCode>,
|
||||||
kind: ErrorKind,
|
kind: ApiErrorKind,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Error {
|
impl ApiError {
|
||||||
const fn torrent_not_found(torrent_id: usize) -> Self {
|
const fn torrent_not_found(torrent_id: usize) -> Self {
|
||||||
Self {
|
Self {
|
||||||
status: Some(StatusCode::NOT_FOUND),
|
status: Some(StatusCode::NOT_FOUND),
|
||||||
kind: ErrorKind::TorrentNotFound(torrent_id),
|
kind: ApiErrorKind::TorrentNotFound(torrent_id),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
const fn dht_disabled() -> Self {
|
const fn dht_disabled() -> Self {
|
||||||
Self {
|
Self {
|
||||||
status: Some(StatusCode::NOT_FOUND),
|
status: Some(StatusCode::NOT_FOUND),
|
||||||
kind: ErrorKind::DhtDisabled,
|
kind: ApiErrorKind::DhtDisabled,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fn with_status(self, status: StatusCode) -> Self {
|
fn with_status(self, status: StatusCode) -> Self {
|
||||||
|
|
@ -54,44 +58,49 @@ impl Error {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
enum ErrorKind {
|
enum ApiErrorKind {
|
||||||
TorrentNotFound(usize),
|
TorrentNotFound(usize),
|
||||||
DhtDisabled,
|
DhtDisabled,
|
||||||
Other(anyhow::Error),
|
Other(anyhow::Error),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<anyhow::Error> for Error {
|
impl From<anyhow::Error> for ApiError {
|
||||||
fn from(value: anyhow::Error) -> Self {
|
fn from(value: anyhow::Error) -> Self {
|
||||||
Self {
|
Self {
|
||||||
status: None,
|
status: None,
|
||||||
kind: ErrorKind::Other(value),
|
kind: ApiErrorKind::Other(value),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl std::error::Error for Error {
|
impl From<(StatusCode, anyhow::Error)> for ApiError {
|
||||||
|
fn from((code, err): (StatusCode, anyhow::Error)) -> Self {
|
||||||
|
ApiError::from(err).with_status(code)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl std::error::Error for ApiError {
|
||||||
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
|
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
|
||||||
match &self.kind {
|
match &self.kind {
|
||||||
ErrorKind::Other(err) => err.source(),
|
ApiErrorKind::Other(err) => err.source(),
|
||||||
_ => None,
|
_ => None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl std::fmt::Display for Error {
|
impl std::fmt::Display for ApiError {
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
match &self.kind {
|
match &self.kind {
|
||||||
ErrorKind::TorrentNotFound(idx) => write!(f, "torrent {idx} not found"),
|
ApiErrorKind::TorrentNotFound(idx) => write!(f, "torrent {idx} not found"),
|
||||||
ErrorKind::Other(err) => err.fmt(f),
|
ApiErrorKind::Other(err) => write!(f, "{err:?}"),
|
||||||
ErrorKind::DhtDisabled => write!(f, "DHT is disabled"),
|
ApiErrorKind::DhtDisabled => write!(f, "DHT is disabled"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl IntoResponse for Error {
|
impl IntoResponse for ApiError {
|
||||||
fn into_response(self) -> response::Response {
|
fn into_response(self) -> response::Response {
|
||||||
let response_body = format!("{self}");
|
let mut response = format!("{self}").into_response();
|
||||||
let mut response = response_body.into_response();
|
|
||||||
*response.status_mut() = match self.status {
|
*response.status_mut() = match self.status {
|
||||||
Some(s) => s,
|
Some(s) => s,
|
||||||
None => StatusCode::INTERNAL_SERVER_ERROR,
|
None => StatusCode::INTERNAL_SERVER_ERROR,
|
||||||
|
|
@ -110,7 +119,7 @@ impl ApiInternal {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn add_mgr(&self, handle: TorrentManagerHandle) -> usize {
|
fn add_torrent_handle(&self, handle: TorrentManagerHandle) -> usize {
|
||||||
let mut g = self.torrent_managers.write();
|
let mut g = self.torrent_managers.write();
|
||||||
let idx = g.len();
|
let idx = g.len();
|
||||||
g.push(handle);
|
g.push(handle);
|
||||||
|
|
@ -182,9 +191,10 @@ fn make_torrent_details(
|
||||||
info_hash: &Id20,
|
info_hash: &Id20,
|
||||||
info: &TorrentMetaV1Info<ByteString>,
|
info: &TorrentMetaV1Info<ByteString>,
|
||||||
only_files: Option<&[usize]>,
|
only_files: Option<&[usize]>,
|
||||||
) -> Result<TorrentDetailsResponse, Error> {
|
) -> Result<TorrentDetailsResponse> {
|
||||||
let files = info
|
let files = info
|
||||||
.iter_filenames_and_lengths()?
|
.iter_filenames_and_lengths()
|
||||||
|
.context("error iterating filenames and lengths")?
|
||||||
.enumerate()
|
.enumerate()
|
||||||
.map(|(idx, (filename_it, length))| {
|
.map(|(idx, (filename_it, length))| {
|
||||||
let name = match filename_it.to_string() {
|
let name = match filename_it.to_string() {
|
||||||
|
|
@ -209,12 +219,12 @@ fn make_torrent_details(
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ApiInternal {
|
impl ApiInternal {
|
||||||
fn mgr_handle(&self, idx: usize) -> Result<TorrentManagerHandle, Error> {
|
fn mgr_handle(&self, idx: usize) -> Result<TorrentManagerHandle> {
|
||||||
self.torrent_managers
|
self.torrent_managers
|
||||||
.read()
|
.read()
|
||||||
.get(idx)
|
.get(idx)
|
||||||
.cloned()
|
.cloned()
|
||||||
.ok_or(Error::torrent_not_found(idx))
|
.ok_or(ApiError::torrent_not_found(idx))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn api_torrent_list(&self) -> TorrentListResponse {
|
fn api_torrent_list(&self) -> TorrentListResponse {
|
||||||
|
|
@ -232,7 +242,7 @@ impl ApiInternal {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn api_torrent_details(&self, idx: usize) -> Result<TorrentDetailsResponse, Error> {
|
fn api_torrent_details(&self, idx: usize) -> Result<TorrentDetailsResponse> {
|
||||||
let handle = self.mgr_handle(idx)?;
|
let handle = self.mgr_handle(idx)?;
|
||||||
let info_hash = handle.torrent_state().info_hash();
|
let info_hash = handle.torrent_state().info_hash();
|
||||||
let only_files = handle.only_files();
|
let only_files = handle.only_files();
|
||||||
|
|
@ -243,7 +253,7 @@ impl ApiInternal {
|
||||||
&self,
|
&self,
|
||||||
url: String,
|
url: String,
|
||||||
opts: Option<AddTorrentOptions>,
|
opts: Option<AddTorrentOptions>,
|
||||||
) -> Result<ApiAddTorrentResponse, Error> {
|
) -> Result<ApiAddTorrentResponse> {
|
||||||
let response = match self
|
let response = match self
|
||||||
.session
|
.session
|
||||||
.add_torrent(&url, opts)
|
.add_torrent(&url, opts)
|
||||||
|
|
@ -251,12 +261,14 @@ impl ApiInternal {
|
||||||
.context("error adding torrent")?
|
.context("error adding torrent")?
|
||||||
{
|
{
|
||||||
AddTorrentResponse::AlreadyManaged(managed) => {
|
AddTorrentResponse::AlreadyManaged(managed) => {
|
||||||
return Err(Error::from(anyhow::anyhow!(
|
return Err(ApiError::from((
|
||||||
"{:?} is already managed, downloaded to {:?}",
|
StatusCode::CONFLICT,
|
||||||
managed.info_hash,
|
anyhow::anyhow!(
|
||||||
managed.output_folder
|
"{:?} is already managed, downloaded to {:?}",
|
||||||
))
|
managed.info_hash,
|
||||||
.with_status(StatusCode::CONFLICT));
|
managed.output_folder
|
||||||
|
),
|
||||||
|
)));
|
||||||
}
|
}
|
||||||
AddTorrentResponse::ListOnly(ListOnlyResponse {
|
AddTorrentResponse::ListOnly(ListOnlyResponse {
|
||||||
info_hash,
|
info_hash,
|
||||||
|
|
@ -274,7 +286,7 @@ impl ApiInternal {
|
||||||
handle.only_files(),
|
handle.only_files(),
|
||||||
)
|
)
|
||||||
.context("error making torrent details")?;
|
.context("error making torrent details")?;
|
||||||
let id = self.add_mgr(handle);
|
let id = self.add_torrent_handle(handle);
|
||||||
ApiAddTorrentResponse {
|
ApiAddTorrentResponse {
|
||||||
id: Some(id),
|
id: Some(id),
|
||||||
details,
|
details,
|
||||||
|
|
@ -288,7 +300,7 @@ impl ApiInternal {
|
||||||
self.dht.as_ref().map(|d| d.stats())
|
self.dht.as_ref().map(|d| d.stats())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn api_stats(&self, idx: usize) -> Result<StatsResponse, Error> {
|
fn api_stats(&self, idx: usize) -> Result<StatsResponse> {
|
||||||
let mgr = self.mgr_handle(idx)?;
|
let mgr = self.mgr_handle(idx)?;
|
||||||
let snapshot = mgr.torrent_state().stats_snapshot();
|
let snapshot = mgr.torrent_state().stats_snapshot();
|
||||||
let estimator = mgr.speed_estimator();
|
let estimator = mgr.speed_estimator();
|
||||||
|
|
@ -307,7 +319,7 @@ impl ApiInternal {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn api_dump_haves(&self, idx: usize) -> Result<String, Error> {
|
fn api_dump_haves(&self, idx: usize) -> Result<String> {
|
||||||
let mgr = self.mgr_handle(idx)?;
|
let mgr = self.mgr_handle(idx)?;
|
||||||
Ok(format!(
|
Ok(format!(
|
||||||
"{:?}",
|
"{:?}",
|
||||||
|
|
@ -332,11 +344,11 @@ pub struct TorrentAddQueryParams {
|
||||||
pub list_only: Option<bool>,
|
pub list_only: Option<bool>,
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn post_torrent(
|
async fn axum_post_torrent(
|
||||||
State(inner): State<ApiState>,
|
State(state): State<ApiState>,
|
||||||
Query(params): Query<TorrentAddQueryParams>,
|
Query(params): Query<TorrentAddQueryParams>,
|
||||||
url: String,
|
url: String,
|
||||||
) -> Result<axum::Json<impl Serialize>, impl IntoResponse> {
|
) -> Result<axum::Json<ApiAddTorrentResponse>> {
|
||||||
let opts = AddTorrentOptions {
|
let opts = AddTorrentOptions {
|
||||||
overwrite: params.overwrite.unwrap_or(false),
|
overwrite: params.overwrite.unwrap_or(false),
|
||||||
only_files_regex: params.only_files_regex,
|
only_files_regex: params.only_files_regex,
|
||||||
|
|
@ -345,105 +357,91 @@ async fn post_torrent(
|
||||||
list_only: params.list_only.unwrap_or(false),
|
list_only: params.list_only.unwrap_or(false),
|
||||||
..Default::default()
|
..Default::default()
|
||||||
};
|
};
|
||||||
match inner
|
state
|
||||||
.api_add_torrent(url, Some(opts))
|
.api_add_torrent(url, Some(opts))
|
||||||
.await
|
.await
|
||||||
.context("error calling HttpApi::api_add_torrent")
|
.map(axum::Json)
|
||||||
{
|
.map_err(|e| e.with_status(StatusCode::BAD_REQUEST))
|
||||||
Ok(response) => Ok(axum::Json(response)),
|
|
||||||
Err(err) => Err((StatusCode::BAD_REQUEST, format!("{err:#?}"))),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn get_torrent(
|
|
||||||
State(state): State<ApiState>,
|
|
||||||
axum::extract::Path(idx): axum::extract::Path<usize>,
|
|
||||||
) -> Result<impl IntoResponse, Error> {
|
|
||||||
Ok(axum::Json(state.api_torrent_details(idx)?))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Public API
|
||||||
impl HttpApi {
|
impl HttpApi {
|
||||||
pub fn new(session: Arc<Session>) -> Self {
|
pub fn new(session: Arc<Session>) -> Self {
|
||||||
Self {
|
Self {
|
||||||
inner: Arc::new(ApiInternal::new(session)),
|
inner: Arc::new(ApiInternal::new(session)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
pub fn add_mgr(&self, handle: TorrentManagerHandle) -> usize {
|
pub fn add_torrent_handle(&self, handle: TorrentManagerHandle) -> usize {
|
||||||
self.inner.add_mgr(handle)
|
self.inner.add_torrent_handle(handle)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn make_http_api_and_run(self, addr: SocketAddr) -> anyhow::Result<()> {
|
pub async fn make_http_api_and_run(self, addr: SocketAddr) -> anyhow::Result<()> {
|
||||||
let state = self.inner;
|
let state = self.inner;
|
||||||
|
let api_description_body = serde_json::json!({
|
||||||
|
"apis": {
|
||||||
|
"GET /": "list all available APIs",
|
||||||
|
"GET /dht/stats": "DHT stats",
|
||||||
|
"GET /dht/table": "DHT routing table",
|
||||||
|
"GET /torrents": "List torrents (default torrent is 0)",
|
||||||
|
"GET /torrents/{index}": "Torrent details",
|
||||||
|
"GET /torrents/{index}/haves": "The bitfield of have pieces",
|
||||||
|
"GET /torrents/{index}/stats": "Torrent stats",
|
||||||
|
// This is kind of not secure as it just reads any local file that it has access to,
|
||||||
|
// or any URL, but whatever, ok for our purposes / threat model.
|
||||||
|
"POST /torrents": "Add a torrent here. magnet: or http:// or a local file."
|
||||||
|
},
|
||||||
|
"server": "rqbit",
|
||||||
|
});
|
||||||
|
|
||||||
let app = Router::new()
|
let app = Router::new()
|
||||||
.route("/", routing::get({
|
.route(
|
||||||
let body = serde_json::json!({
|
"/",
|
||||||
"apis": {
|
routing::get(move || async move { axum::Json(api_description_body) }),
|
||||||
"GET /": "list all available APIs",
|
)
|
||||||
"GET /dht/stats": "DHT stats",
|
|
||||||
"GET /dht/table": "DHT routing table",
|
|
||||||
"GET /torrents": "List torrents (default torrent is 0)",
|
|
||||||
"GET /torrents/{index}": "Torrent details",
|
|
||||||
"GET /torrents/{index}/haves": "The bitfield of have pieces",
|
|
||||||
"GET /torrents/{index}/stats": "Torrent stats",
|
|
||||||
// This is kind of not secure as it just reads any local file that it has access to,
|
|
||||||
// or any URL, but whatever, ok for our purposes / thread model.
|
|
||||||
"POST /torrents": "Add a torrent here. magnet: or http:// or a local file."
|
|
||||||
},
|
|
||||||
"server": "rqbit",
|
|
||||||
});
|
|
||||||
|| async move {
|
|
||||||
axum::Json(body)
|
|
||||||
}
|
|
||||||
}))
|
|
||||||
.route(
|
.route(
|
||||||
"/dht/stats",
|
"/dht/stats",
|
||||||
routing::get({
|
routing::get(|State(state): State<ApiState>| async move {
|
||||||
let state = state.clone();
|
let dht_stats = state.api_dht_stats().ok_or(ApiError::dht_disabled())?;
|
||||||
move || async move {
|
Ok::<_, ApiError>(axum::Json(dht_stats))
|
||||||
let dht_stats = state.api_dht_stats().ok_or(Error::dht_disabled())?;
|
|
||||||
Ok::<_, Error>(axum::Json(dht_stats))
|
|
||||||
}
|
|
||||||
}),
|
}),
|
||||||
)
|
)
|
||||||
.route(
|
.route(
|
||||||
"/dht/table",
|
"/dht/table",
|
||||||
routing::get({
|
routing::get(|State(state): State<ApiState>| async move {
|
||||||
let state = state.clone();
|
let dht = state.dht.as_ref().ok_or(ApiError::dht_disabled())?;
|
||||||
move || async move {
|
Ok::<_, ApiError>(dht.with_routing_table(|r| axum::Json(r.clone())))
|
||||||
let dht = state.dht.as_ref().ok_or(Error::dht_disabled())?;
|
|
||||||
Ok::<_, Error>(dht.with_routing_table(|r| axum::Json(r.clone())))
|
|
||||||
}
|
|
||||||
}),
|
}),
|
||||||
)
|
)
|
||||||
.route(
|
.route(
|
||||||
"/torrents",
|
"/torrents",
|
||||||
routing::get({
|
routing::get(move |State(state): State<ApiState>| async move {
|
||||||
let state = state.clone();
|
axum::Json(state.api_torrent_list())
|
||||||
move || async move { axum::Json(state.api_torrent_list()) }
|
|
||||||
}),
|
}),
|
||||||
)
|
)
|
||||||
.route("/torrents", routing::post(post_torrent))
|
.route("/torrents", routing::post(axum_post_torrent))
|
||||||
.route(
|
.route(
|
||||||
"/torrents/:id",
|
"/torrents/:id",
|
||||||
routing::get(get_torrent),
|
routing::get(
|
||||||
|
|State(state): State<ApiState>, Path(idx): Path<usize>| async move {
|
||||||
|
state.api_torrent_details(idx).map(axum::Json)
|
||||||
|
},
|
||||||
|
),
|
||||||
)
|
)
|
||||||
.route(
|
.route(
|
||||||
"/torrents/:id/haves",
|
"/torrents/:id/haves",
|
||||||
routing::get({
|
routing::get(
|
||||||
let state = state.clone();
|
|State(state): State<ApiState>, Path(idx): Path<usize>| async move {
|
||||||
move |axum::extract::Path(idx): axum::extract::Path<usize>| async move {
|
|
||||||
state.api_dump_haves(idx)
|
state.api_dump_haves(idx)
|
||||||
}
|
},
|
||||||
}),
|
),
|
||||||
)
|
)
|
||||||
.route(
|
.route(
|
||||||
"/torrents/:id/stats",
|
"/torrents/:id/stats",
|
||||||
routing::get({
|
routing::get(
|
||||||
let state = state.clone();
|
|State(state): State<ApiState>, Path(idx): Path<usize>| async move {
|
||||||
move |axum::extract::Path(idx): axum::extract::Path<usize>| async move {
|
|
||||||
state.api_stats(idx).map(axum::Json)
|
state.api_stats(idx).map(axum::Json)
|
||||||
}
|
},
|
||||||
}),
|
),
|
||||||
)
|
)
|
||||||
.with_state(state);
|
.with_state(state);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -266,7 +266,10 @@ async fn async_main(opts: Opts, spawner: BlockingSpawner) -> anyhow::Result<()>
|
||||||
spawn("Stats printer", stats_printer(session.clone()));
|
spawn("Stats printer", stats_printer(session.clone()));
|
||||||
let http_api = HttpApi::new(session);
|
let http_api = HttpApi::new(session);
|
||||||
let http_api_listen_addr = opts.http_api_listen_addr;
|
let http_api_listen_addr = opts.http_api_listen_addr;
|
||||||
http_api.make_http_api_and_run(http_api_listen_addr).await
|
http_api
|
||||||
|
.make_http_api_and_run(http_api_listen_addr)
|
||||||
|
.await
|
||||||
|
.context("error starting HTTP API")
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
SubCommand::Download(download_opts) => {
|
SubCommand::Download(download_opts) => {
|
||||||
|
|
@ -385,7 +388,7 @@ async fn async_main(opts: Opts, spawner: BlockingSpawner) -> anyhow::Result<()>
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
http_api.add_mgr(handle.clone());
|
http_api.add_torrent_handle(handle.clone());
|
||||||
}
|
}
|
||||||
|
|
||||||
if download_opts.list {
|
if download_opts.list {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue