asanaclient 0.1.1

Rust SDK for the Asana API
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
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
//! Portfolio API endpoints.

use crate::types::requests::{
    AddItemData, AddItemRequest, AddMembersData, AddMembersRequest, CreatePortfolioData,
    CreatePortfolioRequest, RemoveItemData, RemoveItemRequest, RemoveMembersData,
    RemoveMembersRequest, UpdatePortfolioData, UpdatePortfolioRequest,
};
use crate::types::{Portfolio, PortfolioItemRef, Project, StatusUpdate};
use crate::{Client, Error};

/// Fields to request for a basic portfolio fetch.
pub const PORTFOLIO_FIELDS: &str = "gid,name,color,owner,owner.name,workspace,\
    current_status_update,current_status_update.gid,current_status_update.status_type,\
    current_status_update.title,current_status_update.text,\
    created_at,created_by,permalink_url,public";

/// Fields to request for portfolio items.
pub const PORTFOLIO_ITEMS_FIELDS: &str = "gid,resource_type,name";

/// Maximum recursion depth for fetching nested portfolios.
pub const MAX_PORTFOLIO_DEPTH: usize = 5;

/// API for portfolio operations.
pub struct PortfoliosApi<'a> {
    client: &'a Client,
}

impl<'a> PortfoliosApi<'a> {
    /// Create a new portfolios API instance.
    pub fn new(client: &'a Client) -> Self {
        Self { client }
    }

    /// Get a portfolio by its GID.
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// # use asanaclient::Client;
    /// # async fn example() -> Result<(), asanaclient::Error> {
    /// let client = Client::from_env()?;
    /// let portfolio = client.portfolios().get("12345").await?;
    /// println!("Portfolio: {}", portfolio.name);
    /// # Ok(())
    /// # }
    /// ```
    pub async fn get(&self, gid: &str) -> Result<Portfolio, Error> {
        let path = format!("/portfolios/{}", gid);
        let query = [("opt_fields", PORTFOLIO_FIELDS)];
        self.client.get(&path, &query).await
    }

    /// Get the items (projects and nested portfolios) in a portfolio.
    ///
    /// Returns compact references to the items. Use `get_items_full` for
    /// detailed information about each item.
    pub async fn items(&self, gid: &str) -> Result<Vec<PortfolioItemRef>, Error> {
        let path = format!("/portfolios/{}/items", gid);
        let query = [("opt_fields", PORTFOLIO_ITEMS_FIELDS)];
        self.client.get_all(&path, &query).await
    }

    /// Get status updates for a portfolio.
    pub async fn status_updates(&self, gid: &str) -> Result<Vec<StatusUpdate>, Error> {
        let path = "/status_updates".to_string();
        let query = [
            ("parent", gid),
            (
                "opt_fields",
                "gid,title,text,html_text,status_type,created_at,created_by,created_by.name",
            ),
        ];
        self.client.get_all(&path, &query).await
    }

    /// Get the latest status update for a portfolio.
    pub async fn latest_status_update(&self, gid: &str) -> Result<Option<StatusUpdate>, Error> {
        let updates = self.status_updates(gid).await?;
        Ok(updates.into_iter().next())
    }

    // ========== Write Operations ==========

    /// Create a new portfolio.
    pub async fn create(&self, data: CreatePortfolioData) -> Result<Portfolio, Error> {
        let path = "/portfolios".to_string();
        let request = CreatePortfolioRequest { data };
        self.client.post(&path, &request).await
    }

    /// Update a portfolio.
    pub async fn update(&self, gid: &str, data: UpdatePortfolioData) -> Result<Portfolio, Error> {
        let path = format!("/portfolios/{}", gid);
        let request = UpdatePortfolioRequest { data };
        self.client.put(&path, &request).await
    }

    /// Delete a portfolio.
    pub async fn delete(&self, gid: &str) -> Result<(), Error> {
        let path = format!("/portfolios/{}", gid);
        self.client.delete(&path).await
    }

    /// Add an item (project) to a portfolio.
    pub async fn add_item(&self, portfolio_gid: &str, item_gid: &str) -> Result<(), Error> {
        let path = format!("/portfolios/{}/addItem", portfolio_gid);
        let request = AddItemRequest {
            data: AddItemData {
                item: item_gid.to_string(),
                insert_before: None,
                insert_after: None,
            },
        };
        self.client.post_empty(&path, &request).await
    }

    /// Add an item with positioning.
    pub async fn add_item_with_position(
        &self,
        portfolio_gid: &str,
        item_gid: &str,
        insert_before: Option<&str>,
        insert_after: Option<&str>,
    ) -> Result<(), Error> {
        let path = format!("/portfolios/{}/addItem", portfolio_gid);
        let request = AddItemRequest {
            data: AddItemData {
                item: item_gid.to_string(),
                insert_before: insert_before.map(String::from),
                insert_after: insert_after.map(String::from),
            },
        };
        self.client.post_empty(&path, &request).await
    }

    /// Remove an item from a portfolio.
    pub async fn remove_item(&self, portfolio_gid: &str, item_gid: &str) -> Result<(), Error> {
        let path = format!("/portfolios/{}/removeItem", portfolio_gid);
        let request = RemoveItemRequest {
            data: RemoveItemData {
                item: item_gid.to_string(),
            },
        };
        self.client.post_empty(&path, &request).await
    }

    /// Add members to a portfolio.
    pub async fn add_members(
        &self,
        portfolio_gid: &str,
        member_gids: &[&str],
    ) -> Result<(), Error> {
        let path = format!("/portfolios/{}/addMembers", portfolio_gid);
        let request = AddMembersRequest {
            data: AddMembersData {
                members: member_gids.iter().map(|s| s.to_string()).collect(),
            },
        };
        self.client.post_empty(&path, &request).await
    }

    /// Remove members from a portfolio.
    pub async fn remove_members(
        &self,
        portfolio_gid: &str,
        member_gids: &[&str],
    ) -> Result<(), Error> {
        let path = format!("/portfolios/{}/removeMembers", portfolio_gid);
        let request = RemoveMembersRequest {
            data: RemoveMembersData {
                members: member_gids.iter().map(|s| s.to_string()).collect(),
            },
        };
        self.client.post_empty(&path, &request).await
    }

    /// Get a portfolio with its items recursively expanded.
    ///
    /// The `max_depth` parameter controls how many levels of children to include:
    /// - `None` - Unlimited depth (expand all nested portfolios)
    /// - `Some(0)` - Just the portfolio metadata, no items
    /// - `Some(1)` - Portfolio + immediate children only
    /// - `Some(2)` - Portfolio + children + grandchildren
    /// - etc.
    ///
    /// Note: Tasks are never included. Use `projects().tasks()` separately if needed.
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// # use asanaclient::Client;
    /// # async fn example() -> Result<(), asanaclient::Error> {
    /// let client = Client::from_env()?;
    ///
    /// // Get portfolio with all nested items
    /// let portfolio = client.portfolios().recursive("port123", None).await?;
    ///
    /// // Get portfolio with only direct children
    /// let portfolio = client.portfolios().recursive("port123", Some(1)).await?;
    /// # Ok(())
    /// # }
    /// ```
    pub async fn recursive(
        &self,
        gid: &str,
        max_depth: Option<usize>,
    ) -> Result<PortfolioWithItems, Error> {
        self.fetch_with_depth(gid, max_depth, 0).await
    }

    /// Internal recursive helper for fetching portfolios.
    fn fetch_with_depth<'b>(
        &'b self,
        gid: &'b str,
        max_depth: Option<usize>,
        current_depth: usize,
    ) -> std::pin::Pin<
        Box<dyn std::future::Future<Output = Result<PortfolioWithItems, Error>> + Send + 'b>,
    > {
        Box::pin(async move {
            let portfolio = self.get(gid).await?;

            // Check if we should fetch items at this depth
            let should_fetch_items = match max_depth {
                None => true,                     // Unlimited depth
                Some(max) => current_depth < max, // Only if we haven't reached max
            };

            if !should_fetch_items {
                return Ok(PortfolioWithItems {
                    portfolio,
                    items: Vec::new(),
                });
            }

            let item_refs = self.items(gid).await?;
            let mut items = Vec::new();

            for item_ref in item_refs {
                let expanded = match item_ref.resource_type.as_str() {
                    "project" => {
                        let project = self.client.projects().get_full(&item_ref.gid).await?;
                        PortfolioItemExpanded::Project(Box::new(project))
                    }
                    "portfolio" => {
                        let nested = self
                            .fetch_with_depth(&item_ref.gid, max_depth, current_depth + 1)
                            .await?;
                        PortfolioItemExpanded::Portfolio(Box::new(nested))
                    }
                    _ => continue, // Unknown type, skip
                };
                items.push(expanded);
            }

            Ok(PortfolioWithItems { portfolio, items })
        })
    }
}

/// A portfolio with its nested items expanded.
#[derive(Debug, Clone, serde::Serialize)]
pub struct PortfolioWithItems {
    /// The portfolio details.
    #[serde(flatten)]
    pub portfolio: Portfolio,
    /// The items in the portfolio.
    pub items: Vec<PortfolioItemExpanded>,
}

/// An expanded portfolio item with full details.
#[derive(Debug, Clone, serde::Serialize)]
#[serde(tag = "resource_type", rename_all = "snake_case")]
pub enum PortfolioItemExpanded {
    /// A project in the portfolio.
    Project(Box<Project>),
    /// A nested portfolio with its items.
    Portfolio(Box<PortfolioWithItems>),
}

impl Client {
    /// Access the portfolios API.
    pub fn portfolios(&self) -> PortfoliosApi<'_> {
        PortfoliosApi::new(self)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use wiremock::matchers::{body_json, method, path};
    use wiremock::{Mock, MockServer, ResponseTemplate};

    fn test_client(server: &MockServer) -> Client {
        Client::new("test-token")
            .unwrap()
            .with_base_url(&server.uri())
    }

    #[tokio::test]
    async fn test_create_portfolio() {
        let server = MockServer::start().await;

        Mock::given(method("POST"))
            .and(path("/portfolios"))
            .respond_with(ResponseTemplate::new(201).set_body_json(serde_json::json!({
                "data": {
                    "gid": "newport",
                    "name": "New Portfolio",
                    "public": true
                }
            })))
            .mount(&server)
            .await;

        let client = test_client(&server);
        let portfolio = client
            .portfolios()
            .create(CreatePortfolioData {
                name: "New Portfolio".to_string(),
                workspace: "ws123".to_string(),
                public: Some(true),
                ..Default::default()
            })
            .await
            .unwrap();

        assert_eq!(portfolio.gid, "newport");
        assert_eq!(portfolio.name, "New Portfolio");
    }

    #[tokio::test]
    async fn test_update_portfolio() {
        let server = MockServer::start().await;

        Mock::given(method("PUT"))
            .and(path("/portfolios/port123"))
            .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
                "data": {
                    "gid": "port123",
                    "name": "Updated Portfolio",
                    "color": "blue"
                }
            })))
            .mount(&server)
            .await;

        let client = test_client(&server);
        let portfolio = client
            .portfolios()
            .update(
                "port123",
                UpdatePortfolioData {
                    name: Some("Updated Portfolio".to_string()),
                    color: Some("blue".to_string()),
                    ..Default::default()
                },
            )
            .await
            .unwrap();

        assert_eq!(portfolio.name, "Updated Portfolio");
    }

    #[tokio::test]
    async fn test_add_item() {
        let server = MockServer::start().await;

        Mock::given(method("POST"))
            .and(path("/portfolios/port123/addItem"))
            .and(body_json(serde_json::json!({
                "data": {"item": "proj456"}
            })))
            .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
                "data": {}
            })))
            .mount(&server)
            .await;

        let client = test_client(&server);
        let result = client.portfolios().add_item("port123", "proj456").await;

        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn test_remove_item() {
        let server = MockServer::start().await;

        Mock::given(method("POST"))
            .and(path("/portfolios/port123/removeItem"))
            .and(body_json(serde_json::json!({
                "data": {"item": "proj456"}
            })))
            .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
                "data": {}
            })))
            .mount(&server)
            .await;

        let client = test_client(&server);
        let result = client.portfolios().remove_item("port123", "proj456").await;

        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn test_add_members() {
        let server = MockServer::start().await;

        Mock::given(method("POST"))
            .and(path("/portfolios/port123/addMembers"))
            .and(body_json(serde_json::json!({
                "data": {"members": ["user1", "user2"]}
            })))
            .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
                "data": {}
            })))
            .mount(&server)
            .await;

        let client = test_client(&server);
        let result = client
            .portfolios()
            .add_members("port123", &["user1", "user2"])
            .await;

        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn test_remove_members() {
        let server = MockServer::start().await;

        Mock::given(method("POST"))
            .and(path("/portfolios/port123/removeMembers"))
            .and(body_json(serde_json::json!({
                "data": {"members": ["user1"]}
            })))
            .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
                "data": {}
            })))
            .mount(&server)
            .await;

        let client = test_client(&server);
        let result = client
            .portfolios()
            .remove_members("port123", &["user1"])
            .await;

        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn test_delete_portfolio() {
        let server = MockServer::start().await;

        Mock::given(method("DELETE"))
            .and(path("/portfolios/port123"))
            .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
                "data": {}
            })))
            .mount(&server)
            .await;

        let client = test_client(&server);
        let result = client.portfolios().delete("port123").await;

        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn test_add_item_with_position() {
        let server = MockServer::start().await;

        Mock::given(method("POST"))
            .and(path("/portfolios/port123/addItem"))
            .and(body_json(serde_json::json!({
                "data": {
                    "item": "proj456",
                    "insert_before": "proj789"
                }
            })))
            .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
                "data": {}
            })))
            .mount(&server)
            .await;

        let client = test_client(&server);
        let result = client
            .portfolios()
            .add_item_with_position("port123", "proj456", Some("proj789"), None)
            .await;

        assert!(result.is_ok());
    }
}