Skip to content
This repository was archived by the owner on Oct 6, 2020. It is now read-only.

Commit 47d047e

Browse files
author
Francesco Cogno
authored
Merge pull request #283 from MindFlavor/issue/278
Implemented stream for ListAttachmentsBuilder
2 parents d93ff51 + 30e8468 commit 47d047e

File tree

4 files changed

+70
-4
lines changed

4 files changed

+70
-4
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
[![Build Status](https://travis-ci.org/MindFlavor/AzureSDKForRust.svg?branch=master)](https://travis-ci.org/MindFlavor/AzureSDKForRust) [![Coverage Status](https://coveralls.io/repos/MindFlavor/AzureSDKForRust/badge.svg?branch=master&service=github)](https://coveralls.io/github/MindFlavor/AzureSDKForRust?branch=master) ![stability-unstable](https://img.shields.io/badge/stability-unstable-yellow.svg)
77

8-
[![tag](https://img.shields.io/github/tag/mindflavor/AzureSDKForRust.svg)](https://github.com/MindFlavor/AzureSDKForRust/tree/cosmos_0.100.0) [![release](https://img.shields.io/github/release/mindflavor/AzureSDKForRust.svg)](https://github.com/MindFlavor/AzureSDKForRust/releases/tag/cosmos_0.100.0) [![commitssince](https://img.shields.io/github/commits-since/mindflavor/AzureSDKForRust/cosmos_0.100.0)](https://github.com/MindFlavor/AzureSDKForRust/commits/master)
8+
[![tag](https://img.shields.io/github/tag/mindflavor/AzureSDKForRust.svg)](https://github.com/MindFlavor/AzureSDKForRust/tree/cosmos_0.100.1) [![release](https://img.shields.io/github/release/mindflavor/AzureSDKForRust.svg)](https://github.com/MindFlavor/AzureSDKForRust/releases/tag/cosmos_0.100.1) [![commitssince](https://img.shields.io/github/commits-since/mindflavor/AzureSDKForRust/cosmos_0.100.1)](https://github.com/MindFlavor/AzureSDKForRust/commits/master)
99

1010
[![GitHub contributors](https://img.shields.io/github/contributors/MindFlavor/AzureSDKForRust.svg)](https://github.com/MindFlavor/AzureSDKForRust/graphs/contributors)
1111

azure_sdk_cosmos/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "azure_sdk_cosmos"
3-
version = "0.100.0"
3+
version = "0.100.1"
44
description = "Rust wrappers around Microsoft Azure REST APIs - Azure Cosmos DB crate"
55
readme = "README.md"
66
authors = ["Francesco Cogno <francesco.cogno@outlook.com>", "Max Gortman <mgortman@microsoft.com>"]

azure_sdk_cosmos/src/requests/list_attachments_builder.rs

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@ use crate::DocumentClientRequired;
44
use crate::{DocumentClient, ResourceType};
55
use azure_sdk_core::errors::{check_status_extract_headers_and_body, AzureError};
66
use azure_sdk_core::prelude::*;
7+
use futures::stream::{unfold, Stream};
78
use hyper::StatusCode;
89
use std::convert::TryInto;
910

10-
#[derive(Debug, Clone)]
11+
#[derive(Debug)]
1112
pub struct ListAttachmentsBuilder<'a, 'b, C, D, COLL>
1213
where
1314
C: CosmosClient,
@@ -47,6 +48,26 @@ where
4748
}
4849
}
4950

51+
impl<'a, 'b, C, D, COLL> Clone for ListAttachmentsBuilder<'a, 'b, C, D, COLL>
52+
where
53+
C: CosmosClient,
54+
D: DatabaseClient<C>,
55+
COLL: CollectionClient<C, D>,
56+
{
57+
fn clone(&self) -> Self {
58+
Self {
59+
document_client: self.document_client,
60+
if_match_condition: self.if_match_condition,
61+
user_agent: self.user_agent,
62+
activity_id: self.activity_id,
63+
consistency_level: self.consistency_level.clone(),
64+
continuation: self.continuation,
65+
max_item_count: self.max_item_count,
66+
a_im: self.a_im,
67+
}
68+
}
69+
}
70+
5071
impl<'a, 'b, C, D, COLL> DocumentClientRequired<'a, C, D, COLL>
5172
for ListAttachmentsBuilder<'a, 'b, C, D, COLL>
5273
where
@@ -351,4 +372,45 @@ where
351372

352373
Ok((&headers, &whole_body as &[u8]).try_into()?)
353374
}
375+
376+
pub fn stream(&self) -> impl Stream<Item = Result<ListAttachmentsResponse, AzureError>> + '_ {
377+
#[derive(Debug, Clone, PartialEq)]
378+
enum States {
379+
Init,
380+
Continuation(String),
381+
};
382+
383+
unfold(
384+
Some(States::Init),
385+
move |continuation_token: Option<States>| {
386+
async move {
387+
debug!("continuation_token == {:?}", &continuation_token);
388+
let response = match continuation_token {
389+
Some(States::Init) => self.execute().await,
390+
Some(States::Continuation(continuation_token)) => {
391+
self.clone()
392+
.with_continuation(&continuation_token)
393+
.execute()
394+
.await
395+
}
396+
None => return None,
397+
};
398+
399+
// the ? operator does not work in async move (yet?)
400+
// so we have to resort to this boilerplate
401+
let response = match response {
402+
Ok(response) => response,
403+
Err(err) => return Some((Err(err), None)),
404+
};
405+
406+
let continuation_token = match &response.continuation_token {
407+
Some(ct) => Some(States::Continuation(ct.to_owned())),
408+
None => None,
409+
};
410+
411+
Some((Ok(response), continuation_token))
412+
}
413+
},
414+
)
415+
}
354416
}

azure_sdk_cosmos/src/responses/list_attachments_response.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
use crate::from_headers::*;
22
use crate::{Attachment, ResourceQuota};
33
use azure_sdk_core::errors::AzureError;
4-
use azure_sdk_core::{session_token_from_headers, SessionToken};
4+
use azure_sdk_core::{
5+
continuation_token_from_headers_optional, session_token_from_headers, SessionToken,
6+
};
57
use chrono::{DateTime, Utc};
68
use hyper::header::HeaderMap;
79

@@ -40,6 +42,7 @@ pub struct ListAttachmentsResponse {
4042
pub activity_id: uuid::Uuid,
4143
pub gateway_version: String,
4244
pub date: DateTime<Utc>,
45+
pub continuation_token: Option<String>,
4346
}
4447

4548
impl std::convert::TryFrom<(&HeaderMap, &[u8])> for ListAttachmentsResponse {
@@ -77,6 +80,7 @@ impl std::convert::TryFrom<(&HeaderMap, &[u8])> for ListAttachmentsResponse {
7780
activity_id: activity_id_from_headers(headers)?,
7881
gateway_version: gateway_version_from_headers(headers)?.to_owned(),
7982
date: date_from_headers(headers)?,
83+
continuation_token: continuation_token_from_headers_optional(headers)?,
8084
})
8185
}
8286
}

0 commit comments

Comments
 (0)