-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Moved models.rs to the module root and declared struct methods as asy…
…nc to remove the tokio runtime.
- Loading branch information
Showing
8 changed files
with
329 additions
and
2,493 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
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 |
---|---|---|
@@ -1,15 +1,16 @@ | ||
mod models; | ||
mod pay; | ||
mod tor_http_client; | ||
mod utils; | ||
|
||
use anyhow::{Result}; | ||
use anyhow::Result; | ||
use models::LnUrlHttpClearnetClient; | ||
use pay::resolve_to_invoice; | ||
use pay::models::{LnUrlHttpClientImpl}; | ||
|
||
pub struct LNURL; | ||
|
||
impl LNURL { | ||
pub fn resolve_lnurl_to_invoice(&self, lnurl: &str, amount: u64) -> Result<String> { | ||
let http_client = LnUrlHttpClientImpl::new(); | ||
resolve_to_invoice(http_client, lnurl, amount) | ||
} | ||
} | ||
pub async fn resolve_lnurl_to_invoice(&self, lnurl: &str, amount: u64) -> Result<String> { | ||
let http_client = LnUrlHttpClearnetClient::new(); | ||
resolve_to_invoice(http_client, lnurl, amount).await | ||
} | ||
} |
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,79 @@ | ||
use anyhow::{anyhow, Result}; | ||
use async_trait::async_trait; | ||
use log::debug; | ||
use mockall::automock; | ||
use reqwest::Response; | ||
use serde::de::DeserializeOwned; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
#[derive(Serialize, Deserialize, Debug)] | ||
pub struct PayRequestResponse { | ||
pub callback: String, | ||
#[serde(rename = "maxSendable")] | ||
pub max_sendable: u64, | ||
#[serde(rename = "minSendable")] | ||
pub min_sendable: u64, | ||
pub tag: String, | ||
pub metadata: String, | ||
} | ||
|
||
#[derive(Deserialize)] | ||
pub struct PayRequestCallbackResponse { | ||
pub pr: String, | ||
pub routes: Vec<String>, | ||
} | ||
|
||
#[derive(Debug, Deserialize, Serialize)] | ||
struct ErrorResponse { | ||
status: String, | ||
reason: String, | ||
} | ||
|
||
#[async_trait] | ||
#[automock] | ||
pub trait LnUrlHttpClient { | ||
async fn get_pay_request_response(&self, lnurl: &str) -> Result<PayRequestResponse>; | ||
async fn get_pay_request_callback_response( | ||
&self, | ||
callback_url: &str, | ||
amount: u64, | ||
) -> Result<PayRequestCallbackResponse>; | ||
} | ||
|
||
pub struct LnUrlHttpClearnetClient { | ||
client: reqwest::Client, | ||
} | ||
|
||
impl LnUrlHttpClearnetClient { | ||
pub fn new() -> LnUrlHttpClearnetClient { | ||
LnUrlHttpClearnetClient { | ||
client: reqwest::Client::new(), | ||
} | ||
} | ||
|
||
async fn get<T: DeserializeOwned + 'static>(&self, url: &str) -> Result<T> { | ||
let response: Response = self.client.get(url).send().await?; | ||
match response.json::<T>().await { | ||
Ok(body) => Ok(body), | ||
Err(e) => { | ||
debug!("{}", e); | ||
Err(anyhow!("Unable to parse http response body as json")) | ||
} | ||
} | ||
} | ||
} | ||
|
||
#[async_trait] | ||
impl LnUrlHttpClient for LnUrlHttpClearnetClient { | ||
async fn get_pay_request_response(&self, lnurl: &str) -> Result<PayRequestResponse> { | ||
self.get::<PayRequestResponse>(lnurl).await | ||
} | ||
|
||
async fn get_pay_request_callback_response( | ||
&self, | ||
callback_url: &str, | ||
amount: u64, | ||
) -> Result<PayRequestCallbackResponse> { | ||
self.get::<PayRequestCallbackResponse>(callback_url).await | ||
} | ||
} |
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.