use crate::models::enums::TransactionType;
use super::{
init::{HeliusClient, API_URL_V0},
parse_response,
};
use serde::{Deserialize, Serialize};
use solana_client::client_error::{ClientError, ClientErrorKind, Result as ClientResult};
#[derive(Deserialize, Debug, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct Webhook {
#[serde(rename = "webhookID")]
pub webhook_id: String,
pub wallet: String,
#[serde(rename = "webhookURL")]
pub webhook_url: String,
pub transaction_types: Vec<TransactionType>,
pub account_addresses: Vec<String>,
pub webhook_type: WebhookType,
pub auth_header: String,
}
#[derive(Serialize, Debug, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct CreateWebhookRequest {
#[serde(rename = "webhookURL")]
pub webhook_url: String,
pub transaction_types: Vec<TransactionType>,
pub account_addresses: Vec<String>,
pub webhook_type: WebhookType,
pub auth_header: String,
}
#[derive(Debug, Serialize, Deserialize, Copy, Clone, PartialEq)]
#[allow(non_camel_case_types)]
pub enum WebhookType {
enhanced,
enhancedDevnet,
raw,
rawDevnet,
discord,
discordDevnet,
}
impl HeliusClient {
pub async fn create_webhook(
&self,
webhook_request: CreateWebhookRequest,
) -> ClientResult<Webhook> {
let request_url = format!("{}/webhooks/?api-key={}", API_URL_V0, self.api_key);
let response = self
.http_client
.post(request_url)
.header("accept", "application/json")
.header("Content-Type", "application/json")
.json(&webhook_request)
.send()
.await;
parse_response(response).await
}
pub async fn get_webhooks(&self) -> ClientResult<Vec<Webhook>> {
let request_url = format!("{}/webhooks?api-key={}", API_URL_V0, self.api_key);
let response = self
.http_client
.get(request_url)
.header("accept", "application/json")
.header("Content-Type", "application/json")
.send()
.await;
parse_response(response).await
}
pub async fn get_webhook(&self, webhook_id: String) -> ClientResult<Webhook> {
let request_url = format!(
"{}/webhooks/{}?api-key={}",
API_URL_V0, webhook_id, self.api_key
);
let response = self
.http_client
.get(request_url)
.header("accept", "application/json")
.header("Content-Type", "application/json")
.send()
.await;
parse_response(response).await
}
pub async fn edit_webhook(
&self,
webhook_id: String,
new_webhook: CreateWebhookRequest,
) -> ClientResult<Webhook> {
let request_url = format!(
"{}/webhooks/{}?api-key={}",
API_URL_V0, webhook_id, self.api_key
);
let response = self
.http_client
.put(request_url)
.header("accept", "application/json")
.header("Content-Type", "application/json")
.json(&new_webhook)
.send()
.await;
parse_response(response).await
}
pub async fn delete_webhook(&self, webhook_id: String) -> ClientResult<()> {
let request_url = format!(
"{}/webhooks/{}?api-key={}",
API_URL_V0, webhook_id, self.api_key
);
let response = self
.http_client
.delete(request_url)
.header("accept", "application/json")
.header("Content-Type", "application/json")
.send()
.await;
match response {
Ok(res) => {
if res.status().is_success() {
Ok(())
} else {
Err(ClientError::from(ClientErrorKind::Custom(format!(
"Request failed with status code: {}",
res.status()
))))
}
}
Err(e) => Err(ClientError::from(ClientErrorKind::Reqwest(e))),
}
}
}