-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathwebhook.rs
107 lines (95 loc) · 4.12 KB
/
webhook.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
use crate::error::Result;
use crate::types::{CreateWebhookRequest, EditWebhookRequest, Webhook};
use crate::Helius;
use reqwest::{Method, Url};
impl Helius {
/// Creates a webhook given account addresses
///
/// # Arguments
/// * `request` - A `CreateWebhookRequest` containing the relevant webhook parameters for creation such as the webhook url and type
///
/// # Returns
/// A `Result` wrapping a `Webhook` if the webhook is successfully created, or a `HeliusError` if creation fails
pub async fn create_webhook(&self, request: CreateWebhookRequest) -> Result<Webhook> {
let url: String = format!(
"{}v0/webhooks/?api-key={}",
self.config.endpoints.api, self.config.api_key
);
let parsed_url: Url = Url::parse(&url).expect("Failed to parse URL");
println!("PARSED URL: {}", parsed_url);
self.rpc_client
.handler
.send(Method::POST, parsed_url, Some(&request))
.await
}
/// Edits a Helius webhook programmatically
///
/// # Arguments
/// * `request` - An `EditWebhookRequest` containing the parameters to be updated for a given webhook
///
/// # Returns
/// A `Result` wrapping the updated `Webhook`, or a `HeliusError` if the edit request fails
pub async fn edit_webhook(&self, request: EditWebhookRequest) -> Result<Webhook> {
let url: String = format!(
"{}v0/webhooks/{}/?api-key={}",
self.config.endpoints.api, request.webhook_id, self.config.api_key
);
let parsed_url: Url = Url::parse(&url).expect("Failed to parse URL");
self.rpc_client
.handler
.send(Method::PUT, parsed_url, Some(&request))
.await
}
/// Appends a set of addresses to a given webhook
///
/// # Arguments
/// * `webhook_id` - The ID of the webhook to be updated
/// * `new_addresses` - A slice of strings representing the new account addresses to be added to the given webhook
///
/// # Returns
/// A `Result` containing the updated `Webhook` object
pub async fn append_addresses_to_webhook(&self, webhook_id: &str, new_addresses: &[String]) -> Result<Webhook> {
let mut webhook: Webhook = self.get_webhook_by_id(webhook_id).await?;
webhook.account_addresses.extend(new_addresses.to_vec());
let edit_request: EditWebhookRequest = EditWebhookRequest {
webhook_id: webhook_id.to_string(),
webhook_url: webhook.webhook_url,
transaction_types: webhook.transaction_types,
auth_header: webhook.auth_header,
txn_status: webhook.txn_status,
encoding: webhook.encoding,
account_addresses: webhook.account_addresses,
webhook_type: webhook.webhook_type,
};
self.edit_webhook(edit_request).await
}
/// Gets a webhook config given a webhook ID
///
/// # Arguments
/// * `webhook_id` - The ID of the webhook to be updated
///
/// # Returns
/// A `Result` wrapping the `Webhook` queried, if it exists
pub async fn get_webhook_by_id(&self, webhook_id: &str) -> Result<Webhook> {
let url: String = format!(
"{}v0/webhooks/{}/?api-key={}",
self.config.endpoints.api, webhook_id, self.config.api_key
);
let parsed_url: Url = Url::parse(&url).expect("Failed to parse URL");
self.rpc_client.handler.send(Method::GET, parsed_url, None::<&()>).await
}
/// Retrieves all Helius webhooks programmatically
///
/// Due to response size limitations, we will truncate addresses returned to 100 per config
///
/// # Returns
/// A `Result` containing a vector of `Webhook` representing all configured webhooks for a given account
pub async fn get_all_webhooks(&self) -> Result<Vec<Webhook>> {
let url: String = format!(
"{}v0/webhooks/?api-key={}",
self.config.endpoints.api, self.config.api_key
);
let parsed_url: Url = Url::parse(&url).expect("Failed to parse URL");
self.rpc_client.handler.send(Method::GET, parsed_url, None::<&()>).await
}
}