solagent_plugin_helius/
delete_webhook.rs

1// Copyright 2025 zTgx
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use solagent_core::SolanaAgentKit;
16
17/// Deletes a Helius Webhook by its ID.
18///
19/// # Arguments
20/// * `agent` - An instance of SolanaAgentKit (with .config.HELIUS_API_KEY)
21/// * `webhook_id` - The unique ID of the webhook to delete
22///
23/// # Returns
24/// The response body from the Helius API (which may contain status or other info)
25pub async fn delete_webhook(
26    agent: &SolanaAgentKit,
27    webhook_id: &str,
28) -> Result<serde_json::Value, Box<dyn std::error::Error>> {
29    // Get the Helius API key from the agent's configuration
30    let api_key = match agent.config.helius_api_key.as_ref() {
31        Some(key) => key,
32        None => return Err("Missing Helius API key in agent.config.HELIUS_API_KEY".into()),
33    };
34
35    // Construct the URL for the DELETE request
36    let url = format!("https://api.helius.xyz/v0/webhooks/{}?api-key={}", webhook_id, api_key);
37
38    // Create an HTTP client
39    let client = reqwest::Client::new();
40
41    // Send the DELETE request
42    let response = client.delete(&url).header("Content-Type", "application/json").send().await?;
43
44    // Check if the request was successful
45    if !response.status().is_success() {
46        return Err(format!(
47            "Failed to delete webhook: {} {}",
48            response.status(),
49            response.status().canonical_reason().unwrap_or("Unknown")
50        )
51        .into());
52    }
53
54    // Handle different response status codes
55    if response.status().as_u16() == 204 {
56        return Ok(serde_json::json!({"message": "Webhook deleted successfully (no content returned)"}));
57    }
58
59    // Check if the response body is empty
60    let content_length = response.headers().get("Content-Length");
61    if content_length.is_none() || content_length.expect("HeaderValue").to_str()? == "0" {
62        return Ok(serde_json::json!({"message": "Webhook deleted successfully (empty body)"}));
63    }
64
65    // Parse the response body as JSON
66    let data: serde_json::Value = response.json().await?;
67    Ok(data)
68}