Skip to content

Commit

Permalink
Merge pull request fedimint#3847 from mayrf/improve-gateway-api
Browse files Browse the repository at this point in the history
chore: change info endpoint http method to get
  • Loading branch information
elsirion authored Dec 6, 2023
2 parents b7a7061 + 1a28037 commit 07dddb5
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 33 deletions.
4 changes: 2 additions & 2 deletions gateway/ln-gateway/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ use crate::lnrpc_client::GatewayLightningBuilder;
use crate::rpc::rpc_server::run_webserver;
use crate::rpc::{
BackupPayload, BalancePayload, ConnectFedPayload, DepositAddressPayload, GatewayInfo,
InfoPayload, RestorePayload, WithdrawPayload,
RestorePayload, WithdrawPayload,
};
use crate::state_machine::GatewayExtPayStates;

Expand Down Expand Up @@ -616,7 +616,7 @@ impl Gateway {
*lock = state;
}

pub async fn handle_get_info(&self, _payload: InfoPayload) -> Result<GatewayInfo> {
pub async fn handle_get_info(&self) -> Result<GatewayInfo> {
if let GatewayState::Running {
lnrpc,
lightning_public_key,
Expand Down
55 changes: 33 additions & 22 deletions gateway/ln-gateway/src/rpc/rpc_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use std::result::Result;
use bitcoin::Address;
use fedimint_core::util::SafeUrl;
use fedimint_core::{Amount, TransactionId};
use reqwest::StatusCode;
pub use reqwest::{Error, Response};
use reqwest::{Method, StatusCode};
use serde::de::DeserializeOwned;
use serde::Serialize;
use thiserror::Error;
Expand Down Expand Up @@ -39,25 +39,25 @@ impl GatewayRpcClient {

pub async fn get_info(&self) -> GatewayRpcResult<GatewayInfo> {
let url = self.base_url.join("/info").expect("invalid base url");
self.call(url, ()).await
self.call_get(url).await
}

pub async fn get_balance(&self, payload: BalancePayload) -> GatewayRpcResult<Amount> {
let url = self.base_url.join("/balance").expect("invalid base url");
self.call(url, payload).await
self.call_post(url, payload).await
}

pub async fn get_deposit_address(
&self,
payload: DepositAddressPayload,
) -> GatewayRpcResult<Address> {
let url = self.base_url.join("/address").expect("invalid base url");
self.call(url, payload).await
self.call_post(url, payload).await
}

pub async fn withdraw(&self, payload: WithdrawPayload) -> GatewayRpcResult<TransactionId> {
let url = self.base_url.join("/withdraw").expect("invalid base url");
self.call(url, payload).await
self.call_post(url, payload).await
}

pub async fn connect_federation(
Expand All @@ -68,22 +68,22 @@ impl GatewayRpcClient {
.base_url
.join("/connect-fed")
.expect("invalid base url");
self.call(url, payload).await
self.call_post(url, payload).await
}

pub async fn leave_federation(&self, payload: LeaveFedPayload) -> GatewayRpcResult<()> {
let url = self.base_url.join("/leave-fed").expect("invalid base url");
self.call(url, payload).await
self.call_post(url, payload).await
}

pub async fn backup(&self, payload: BackupPayload) -> GatewayRpcResult<()> {
let url = self.base_url.join("/backup").expect("invalid base url");
self.call(url, payload).await
self.call_post(url, payload).await
}

pub async fn restore(&self, payload: RestorePayload) -> GatewayRpcResult<()> {
let url = self.base_url.join("/restore").expect("invalid base url");
self.call(url, payload).await
self.call_post(url, payload).await
}

pub async fn set_configuration(
Expand All @@ -94,32 +94,43 @@ impl GatewayRpcClient {
.base_url
.join("/set_configuration")
.expect("invalid base url");
self.call(url, payload).await
self.call(Method::POST, url, Some(payload)).await
}

async fn call<P, T: DeserializeOwned>(
async fn call<P: Serialize, T: DeserializeOwned>(
&self,
method: Method,
url: SafeUrl,
payload: P,
) -> Result<T, GatewayRpcError>
where
P: Serialize,
{
let mut builder = self.client.post(url.to_unsafe());
payload: Option<P>,
) -> Result<T, GatewayRpcError> {
let mut builder = self.client.request(method, url.to_unsafe());
if let Some(password) = self.password.clone() {
builder = builder.bearer_auth(password);
}
let response = builder
.header(reqwest::header::CONTENT_TYPE, "application/json")
.json(&payload)
.send()
.await?;
if let Some(payload) = payload {
builder = builder
.json(&payload)
.header(reqwest::header::CONTENT_TYPE, "application/json")
}
let response = builder.send().await?;

match response.status() {
StatusCode::OK => Ok(response.json().await?),
status => Err(GatewayRpcError::BadStatus(status)),
}
}

async fn call_get<T: DeserializeOwned>(&self, url: SafeUrl) -> Result<T, GatewayRpcError> {
self.call(Method::GET, url, None::<()>).await
}

async fn call_post<P: Serialize, T: DeserializeOwned>(
&self,
url: SafeUrl,
payload: P,
) -> Result<T, GatewayRpcError> {
self.call(Method::POST, url, Some(payload)).await
}
}

pub type GatewayRpcResult<T> = Result<T, GatewayRpcError>;
Expand Down
15 changes: 6 additions & 9 deletions gateway/ln-gateway/src/rpc/rpc_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ use tower_http::validate_request::ValidateRequestHeaderLayer;
use tracing::{error, instrument};

use super::{
BackupPayload, BalancePayload, ConnectFedPayload, DepositAddressPayload, InfoPayload,
LeaveFedPayload, RestorePayload, SetConfigurationPayload, WithdrawPayload,
BackupPayload, BalancePayload, ConnectFedPayload, DepositAddressPayload, LeaveFedPayload,
RestorePayload, SetConfigurationPayload, WithdrawPayload,
};
use crate::db::GatewayConfiguration;
use crate::{Gateway, GatewayError};
Expand All @@ -33,7 +33,7 @@ pub async fn run_webserver(

// Authenticated, public routes used for gateway administration
let admin_routes = Router::new()
.route("/info", post(info))
.route("/info", get(info))
.route("/balance", post(balance))
.route("/address", post(address))
.route("/withdraw", post(withdraw))
Expand All @@ -47,7 +47,7 @@ pub async fn run_webserver(
} else {
let routes = Router::new()
.route("/set_configuration", post(set_configuration))
.route("/info", post(info));
.route("/info", get(info));
(routes, Router::new())
};

Expand Down Expand Up @@ -78,11 +78,8 @@ pub async fn run_webserver(
/// Display high-level information about the Gateway
#[debug_handler]
#[instrument(skip_all, err)]
async fn info(
Extension(gateway): Extension<Gateway>,
Json(payload): Json<InfoPayload>,
) -> Result<impl IntoResponse, GatewayError> {
let info = gateway.handle_get_info(payload).await?;
async fn info(Extension(gateway): Extension<Gateway>) -> Result<impl IntoResponse, GatewayError> {
let info = gateway.handle_get_info().await?;
Ok(Json(json!(info)))
}

Expand Down

0 comments on commit 07dddb5

Please sign in to comment.