slacko 0.2.2

Comprehensive Rust SDK for the Slack API with stealth mode support
Documentation
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
//! Core Slack API client

use crate::api::{
    admin::AdminApi, api_test::ApiApi, apps::AppsApi, auth::AuthApi, bookmarks::BookmarksApi,
    bots::BotsApi, calls::CallsApi, chat::ChatApi, conversations::ConversationsApi,
    dialog::DialogApi, dnd::DndApi, emoji::EmojiApi, files::FilesApi, lists::ListsApi,
    oauth::OAuthApi, openid::OpenIDApi, pins::PinsApi, reactions::ReactionsApi,
    reminders::RemindersApi, rtm::RtmApi, search::SearchApi, socket_mode::SocketModeApi,
    stars::StarsApi, team::TeamApi, usergroups::UsergroupsApi, users::UsersApi, views::ViewsApi,
    workflows::WorkflowsApi,
};
use crate::auth::AuthConfig;
use crate::error::{Result, SlackError};
use crate::types::SlackResponse;
use reqwest::header::HeaderMap;
use std::sync::Arc;

const SLACK_API_BASE: &str = "https://slack.com/api";

/// Main Slack API client
///
/// This is the primary entry point for interacting with the Slack API.
/// It provides access to all API endpoints through specialized API clients.
///
/// # Example
///
/// ```no_run
/// use slacko::{SlackClient, AuthConfig};
///
/// #[tokio::main]
/// async fn main() -> Result<(), Box<dyn std::error::Error>> {
///     let client = SlackClient::new(AuthConfig::oauth("xoxp-token"))?;
///
///     // Post a message
///     client.chat()
///         .post_message("C12345", "Hello!")
///         .await?;
///
///     // List channels
///     let channels = client.conversations()
///         .list()
///         .await?;
///
///     Ok(())
/// }
/// ```
#[derive(Clone)]
pub struct SlackClient {
    pub(crate) http: reqwest::Client,
    pub(crate) auth: Arc<AuthConfig>,
    pub(crate) base_url: String,
}

impl SlackClient {
    /// Create a new Slack client with the given authentication configuration
    ///
    /// # Arguments
    ///
    /// * `auth` - Authentication configuration
    ///
    /// # Example
    ///
    /// ```
    /// use slacko::{SlackClient, AuthConfig};
    ///
    /// let client = SlackClient::new(
    ///     AuthConfig::oauth("xoxp-token")
    /// ).unwrap();
    /// ```
    pub fn new(auth: AuthConfig) -> Result<Self> {
        let http = reqwest::Client::builder()
            .user_agent("slack-sdk-rust/0.1.0")
            .build()
            .map_err(|e| SlackError::config_error(format!("Failed to build HTTP client: {}", e)))?;

        Ok(Self {
            http,
            auth: Arc::new(auth),
            base_url: SLACK_API_BASE.to_string(),
        })
    }

    /// Get the API test client
    ///
    /// Provides methods for testing the Slack API connection.
    pub fn api(&self) -> ApiApi {
        ApiApi::new(self.clone())
    }

    /// Get the Bots API client
    ///
    /// Provides methods for getting information about bot users.
    pub fn bots(&self) -> BotsApi {
        BotsApi::new(self.clone())
    }

    /// Get the Chat API client
    ///
    /// Provides methods for posting, updating, and deleting messages.
    pub fn chat(&self) -> ChatApi {
        ChatApi::new(self.clone())
    }

    /// Get the Conversations API client
    ///
    /// Provides methods for managing channels, groups, and DMs.
    pub fn conversations(&self) -> ConversationsApi {
        ConversationsApi::new(self.clone())
    }

    /// Get the Users API client
    ///
    /// Provides methods for retrieving user information.
    pub fn users(&self) -> UsersApi {
        UsersApi::new(self.clone())
    }

    /// Get the Files API client
    ///
    /// Provides methods for uploading and managing files.
    pub fn files(&self) -> FilesApi {
        FilesApi::new(self.clone())
    }

    /// Get the Reactions API client
    ///
    /// Provides methods for adding and removing emoji reactions.
    pub fn reactions(&self) -> ReactionsApi {
        ReactionsApi::new(self.clone())
    }

    /// Get the Search API client
    ///
    /// Provides methods for searching messages and files.
    pub fn search(&self) -> SearchApi {
        SearchApi::new(self.clone())
    }

    /// Get the Team API client
    ///
    /// Provides methods for retrieving team/workspace information.
    pub fn team(&self) -> TeamApi {
        TeamApi::new(self.clone())
    }

    /// Get the RTM API client
    ///
    /// Provides methods for real-time messaging via WebSocket.
    pub fn rtm(&self) -> RtmApi {
        RtmApi::new(self.clone())
    }

    /// Get the Socket Mode API client
    ///
    /// Provides methods for receiving events via WebSocket using Socket Mode.
    /// This is the modern alternative to RTM for receiving events.
    /// Note: Requires an app-level token (xapp-...).
    pub fn socket_mode(&self) -> SocketModeApi {
        SocketModeApi::new(self.clone())
    }

    /// Get the Auth API client
    ///
    /// Provides methods for testing and managing authentication.
    pub fn auth(&self) -> AuthApi {
        AuthApi::new(self.clone())
    }

    /// Get the Pins API client
    ///
    /// Provides methods for pinning and unpinning messages.
    pub fn pins(&self) -> PinsApi {
        PinsApi::new(self.clone())
    }

    /// Get the Stars API client
    ///
    /// Provides methods for starring items.
    pub fn stars(&self) -> StarsApi {
        StarsApi::new(self.clone())
    }

    /// Get the Reminders API client
    ///
    /// Provides methods for creating and managing reminders.
    pub fn reminders(&self) -> RemindersApi {
        RemindersApi::new(self.clone())
    }

    /// Get the DND API client
    ///
    /// Provides methods for Do Not Disturb settings.
    pub fn dnd(&self) -> DndApi {
        DndApi::new(self.clone())
    }

    /// Get the Emoji API client
    ///
    /// Provides methods for listing custom emoji.
    pub fn emoji(&self) -> EmojiApi {
        EmojiApi::new(self.clone())
    }

    /// Get the OAuth v2 API client
    ///
    /// Provides methods for OAuth token exchange and management.
    /// Use this for building public Slack apps with OAuth flows.
    pub fn oauth(&self) -> OAuthApi {
        OAuthApi::new(self.clone())
    }

    /// Get the OpenID Connect API client
    ///
    /// Provides methods for OpenID Connect authentication flows.
    /// Use this for implementing Sign in with Slack and identity verification.
    pub fn openid(&self) -> OpenIDApi {
        OpenIDApi::new(self.clone())
    }

    /// Get the Usergroups API client
    ///
    /// Provides methods for managing user groups.
    pub fn usergroups(&self) -> UsergroupsApi {
        UsergroupsApi::new(self.clone())
    }

    /// Get the Views API client
    ///
    /// Provides methods for managing modals and App Home.
    pub fn views(&self) -> ViewsApi {
        ViewsApi::new(self.clone())
    }

    /// Get the Dialog API client (Legacy)
    ///
    /// Provides methods for opening legacy dialogs.
    pub fn dialog(&self) -> DialogApi {
        DialogApi::new(self.clone())
    }

    /// Get the Bookmarks API client
    ///
    /// Provides methods for managing channel bookmarks.
    pub fn bookmarks(&self) -> BookmarksApi {
        BookmarksApi::new(self.clone())
    }

    /// Get the Admin API client
    ///
    /// Provides methods for Enterprise Grid administration.
    pub fn admin(&self) -> AdminApi {
        AdminApi::new(self.clone())
    }

    /// Get the Apps API client
    ///
    /// Provides methods for managing app configurations.
    pub fn apps(&self) -> AppsApi {
        AppsApi::new(self.clone())
    }

    /// Get the Calls API client
    ///
    /// Provides methods for Slack Calls integration.
    pub fn calls(&self) -> CallsApi {
        CallsApi::new(self.clone())
    }

    /// Get the Workflows API client
    ///
    /// Provides methods for Workflow Builder integrations.
    pub fn workflows(&self) -> WorkflowsApi {
        WorkflowsApi::new(self.clone())
    }

    /// Get the Lists API client
    ///
    /// Provides methods for managing Slack Lists.
    pub fn lists(&self) -> ListsApi {
        ListsApi::new(self.clone())
    }

    /// Make a POST request to the Slack API
    pub(crate) async fn post<T: serde::de::DeserializeOwned>(
        &self,
        method: &str,
        params: &impl serde::Serialize,
    ) -> Result<T> {
        let url = format!("{}/{}", self.base_url, method);
        let headers = self.auth.build_headers();

        let response = self
            .http
            .post(&url)
            .headers(headers)
            .json(params)
            .send()
            .await?;

        // Check for rate limiting
        if response.status().as_u16() == 429 {
            let retry_after = response
                .headers()
                .get("retry-after")
                .and_then(|v| v.to_str().ok())
                .and_then(|v| v.parse().ok())
                .unwrap_or(60);

            return Err(SlackError::RateLimitExceeded { retry_after });
        }

        let slack_response: SlackResponse<T> = response.json().await?;

        if !slack_response.ok {
            let error_msg = slack_response
                .error
                .unwrap_or_else(|| "Unknown error".to_string());
            return Err(SlackError::api_error(method, error_msg));
        }

        slack_response
            .data
            .ok_or_else(|| SlackError::api_error(method, "No data in response"))
    }

    /// Make a GET request to the Slack API
    pub(crate) async fn get<T: serde::de::DeserializeOwned>(
        &self,
        method: &str,
        params: &[(&str, &str)],
    ) -> Result<T> {
        let url = format!("{}/{}", self.base_url, method);
        let headers = self.auth.build_headers();

        let response = self
            .http
            .get(&url)
            .headers(headers)
            .query(params)
            .send()
            .await?;

        // Check for rate limiting
        if response.status().as_u16() == 429 {
            let retry_after = response
                .headers()
                .get("retry-after")
                .and_then(|v| v.to_str().ok())
                .and_then(|v| v.parse().ok())
                .unwrap_or(60);

            return Err(SlackError::RateLimitExceeded { retry_after });
        }

        let slack_response: SlackResponse<T> = response.json().await?;

        if !slack_response.ok {
            let error_msg = slack_response
                .error
                .unwrap_or_else(|| "Unknown error".to_string());
            return Err(SlackError::api_error(method, error_msg));
        }

        slack_response
            .data
            .ok_or_else(|| SlackError::api_error(method, "No data in response"))
    }

    /// Get headers for API requests
    #[allow(dead_code)]
    pub(crate) fn headers(&self) -> HeaderMap {
        self.auth.build_headers()
    }

    /// Upload a file via multipart form
    pub(crate) async fn upload_file<T: serde::de::DeserializeOwned>(
        &self,
        method: &str,
        file_data: Vec<u8>,
        field_name: &str,
        file_name: &str,
    ) -> Result<T> {
        self.upload_file_with_params(method, file_data, field_name, file_name, &[])
            .await
    }

    /// Upload a file via multipart form with additional parameters
    pub(crate) async fn upload_file_with_params<T: serde::de::DeserializeOwned>(
        &self,
        method: &str,
        file_data: Vec<u8>,
        field_name: &str,
        file_name: &str,
        params: &[(&str, &str)],
    ) -> Result<T> {
        use reqwest::multipart::{Form, Part};

        let url = format!("{}/{}", self.base_url, method);
        let headers = self.auth.build_headers();

        let part = Part::bytes(file_data).file_name(file_name.to_string());

        let mut form = Form::new().part(field_name.to_string(), part);

        for (key, value) in params {
            form = form.text(key.to_string(), value.to_string());
        }

        let response = self
            .http
            .post(&url)
            .headers(headers)
            .multipart(form)
            .send()
            .await?;

        if response.status().as_u16() == 429 {
            let retry_after = response
                .headers()
                .get("retry-after")
                .and_then(|v| v.to_str().ok())
                .and_then(|v| v.parse().ok())
                .unwrap_or(60);

            return Err(SlackError::RateLimitExceeded { retry_after });
        }

        let slack_response: SlackResponse<T> = response.json().await?;

        if !slack_response.ok {
            let error_msg = slack_response
                .error
                .unwrap_or_else(|| "Unknown error".to_string());
            return Err(SlackError::api_error(method, error_msg));
        }

        slack_response
            .data
            .ok_or_else(|| SlackError::api_error(method, "No data in response"))
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_client_creation() {
        let client = SlackClient::new(AuthConfig::oauth("xoxp-test-token"));
        assert!(client.is_ok());
    }

    #[test]
    fn test_client_creation_stealth() {
        let client = SlackClient::new(AuthConfig::stealth("xoxc-token", "xoxd-cookie"));
        assert!(client.is_ok());
    }
}