pub struct WebhookHandler { /* private fields */ }Implementations§
Source§impl WebhookHandler
impl WebhookHandler
pub fn parse_update(json: &str) -> Result<WebhookUpdate, CryptoBotError>
Sourcepub fn verify_signature(&self, body: &str, signature: &str) -> bool
pub fn verify_signature(&self, body: &str, signature: &str) -> bool
Verifies the signature of a webhook request
The signature is created by the Crypto Bot API using HMAC-SHA-256 with the API token as the key and the request body as the message.
§Arguments
body- The raw request bodysignature- The signature from the ‘crypto-pay-api-signature’ header
§Returns
trueif the signature is validfalseif the signature is invalid or malformed
§Example
use crypto_pay_api::prelude::*;
#[tokio::main]
async fn main() -> Result<(), CryptoBotError> {
let client = CryptoBot::builder().api_token("your_api_token").build().unwrap();
let handler = client.webhook_handler().build();
let body = r#"{"update_id": 1, "update_type": "invoice_paid"}"#;
let signature = "1234567890abcdef"; // The actual signature from the request header
if handler.verify_signature(body, signature) {
println!("Signature is valid");
} else {
println!("Invalid signature");
}
Ok(())
}Sourcepub async fn handle_update(
&self,
body: &str,
) -> Result<WebhookResponse, CryptoBotError>
pub async fn handle_update( &self, body: &str, ) -> Result<WebhookResponse, CryptoBotError>
Handles a webhook update from Crypto Bot API
This method:
- Parses the webhook update from JSON
- Validates the request date
- Checks if the request has expired
- Calls the registered update handler if one exists
§Arguments
body- The raw webhook request body as JSON string
§Returns
Ok(WebhookResponse)- If the update was handled successfullyErr(CryptoBotError)- If any validation fails or the handler returns an error
§Errors
WebhookErrorKind::InvalidPayload- If the JSON is invalid or missing required fieldsWebhookErrorKind::Expired- If the request is older than the expiration time
Sourcepub fn on_update<F, Fut>(&mut self, handler: F)where
F: Fn(WebhookUpdate) -> Fut + Send + Sync + 'static,
Fut: Future<Output = Result<(), CryptoBotError>> + Send + 'static,
pub fn on_update<F, Fut>(&mut self, handler: F)where
F: Fn(WebhookUpdate) -> Fut + Send + Sync + 'static,
Fut: Future<Output = Result<(), CryptoBotError>> + Send + 'static,
Registers a handler function for webhook updates
The handler function will be called for each webhook update received through
handle_update. The function should process the update and return a Result
indicating success or failure.
§Arguments
handler- An async function that takes aWebhookUpdateand returns aResult<(), CryptoBotError>
§Type Parameters
F- The handler function typeFut- The future type returned by the handler
§Requirements
The handler function must:
- Be
Send+Sync+ ’static - Return a Future that is
Send+ ’static - The Future must resolve to
Result<(), CryptoBotError>
§Example
use crypto_pay_api::prelude::*;
#[tokio::main]
async fn main() {
let client = CryptoBot::builder().api_token("YOUR_API_TOKEN").build().unwrap();
let mut handler = client.webhook_handler().build();
handler.on_update(|update| async move {
match (update.update_type, update.payload) {
(UpdateType::InvoicePaid, WebhookPayload::InvoicePaid(invoice)) => {
println!("Payment received!");
println!("Amount: {} {}", invoice.amount, invoice.asset.unwrap());
// Process the payment...
}
}
Ok(())
});
// Now ready to handle webhook updates
}Auto Trait Implementations§
impl !RefUnwindSafe for WebhookHandler
impl !UnwindSafe for WebhookHandler
impl Freeze for WebhookHandler
impl Send for WebhookHandler
impl Sync for WebhookHandler
impl Unpin for WebhookHandler
impl UnsafeUnpin for WebhookHandler
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more