Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pager rename and refactor #1836

Merged
merged 10 commits into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions sdk/cosmos/azure_data_cosmos/src/clients/container_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use crate::{

use azure_core::{Context, Pager, Request, Response};
use serde::{de::DeserializeOwned, Serialize};
use typespec_client_core::http::PagerResult;
use url::Url;

#[cfg(doc)]
Expand Down Expand Up @@ -471,13 +472,13 @@ impl ContainerClientMethods for ContainerClient {
req.insert_header(constants::CONTINUATION, continuation);
}

let resp = pipeline
let response = pipeline
.send(Context::new(), &mut req, ResourceType::Items)
.await?;
let continuation_token =
resp.headers().get_optional_string(&constants::CONTINUATION);

Ok((resp, continuation_token))
Ok(PagerResult::from_response_header(
response,
&constants::CONTINUATION,
))
}
}))
}
Expand Down
84 changes: 57 additions & 27 deletions sdk/typespec/typespec_client_core/src/http/pager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,33 @@ use std::{future::Future, pin::Pin};
use futures::{stream::unfold, Stream};
use typespec::Error;

use crate::http::Response;
use crate::http::{headers::HeaderName, Response};

pub enum PagerResult<T, C> {
Continue {
response: Response<T>,
continuation: C,
},
Finish {
response: Response<T>,
},
}

impl<T> PagerResult<T, String> {
/// Creates a [`PagerResult<T, C>`] from the provided response, extracting the continuation value from the provided header.
///
/// If the provided response has a header with the matching name, this returns [`PagerResult::Continue`], using the value from the header as the continuation.
/// If the provided response does not have a header with the matching name, this returns [`PagerResult::Finish`].
pub fn from_response_header(response: Response<T>, header_name: &HeaderName) -> Self {
match response.headers().get_optional_string(header_name) {
Some(continuation) => PagerResult::Continue {
response,
continuation,
},
None => PagerResult::Finish { response },
}
}
}

/// Represents a paginated result across multiple requests.
#[pin_project::pin_project]
Expand Down Expand Up @@ -36,7 +62,7 @@ impl<T> Pager<T> {
/// ## Examples
///
/// ```rust,no_run
/// # use typespec_client_core::http::{Context, Pager, Pipeline, Request, Response, Method, headers::HeaderName};
/// # use typespec_client_core::http::{Context, Pager, PagerResult, Pipeline, Request, Response, Method, headers::HeaderName};
/// # let pipeline: Pipeline = panic!("Not a runnable example");
/// # struct MyModel;
/// let url = "https://example.com/my_paginated_api".parse().unwrap();
Expand All @@ -52,36 +78,41 @@ impl<T> Pager<T> {
/// let resp: Response<MyModel> = pipeline
/// .send(&Context::new(), &mut req)
/// .await?;
/// let continuation_token = resp.headers().get_optional_string(&HeaderName::from_static("x-next-continuation"));
/// Ok((resp, continuation_token))
/// Ok(PagerResult::from_response_header(resp, &HeaderName::from_static("x-next-continuation")))
/// }
/// });
/// ```
pub fn from_callback<
// This is a bit gnarly, but the only thing that differs between the WASM/non-WASM configs is the presence of Send bounds.
#[cfg(not(target_arch = "wasm32"))] C: Send + 'static,
#[cfg(not(target_arch = "wasm32"))] F: Fn(Option<C>) -> Fut + Send + 'static,
#[cfg(not(target_arch = "wasm32"))] Fut: Future<Output = Result<(Response<T>, Option<C>), typespec::Error>> + Send + 'static,
#[cfg(not(target_arch = "wasm32"))] Fut: Future<Output = Result<PagerResult<T, C>, typespec::Error>> + Send + 'static,
#[cfg(target_arch = "wasm32")] C: 'static,
#[cfg(target_arch = "wasm32")] F: Fn(Option<C>) -> Fut + 'static,
#[cfg(target_arch = "wasm32")] Fut: Future<Output = Result<(Response<T>, Option<C>), typespec::Error>> + 'static,
#[cfg(target_arch = "wasm32")] Fut: Future<Output = Result<PagerResult<T, C>, typespec::Error>> + 'static,
>(
make_request: F,
) -> Self {
let stream = unfold(
// We flow the `make_request` callback through the state value so that we can avoid cloning.
(State::Init, make_request),
|(state, make_request)| async move {
let result = match state {
State::Init => make_request(None).await,
State::Continuation(c) => make_request(Some(c)).await,
State::Done => return None,
};
let (response, continuation) = match result {
let (response, next_state) = match result {
Err(e) => return Some((Err(e), (State::Done, make_request))),
Ok(r) => r,
Ok(PagerResult::Continue {
response,
continuation,
}) => (Ok(response), State::Continuation(continuation)),
Ok(PagerResult::Finish { response }) => (Ok(response), State::Done),
};
let next_state = continuation.map_or(State::Done, State::Continuation);
Some((Ok(response), (next_state, make_request)))

// Flow 'make_request' through to avoid cloning
Some((response, (next_state, make_request)))
},
);
Self {
Expand Down Expand Up @@ -124,7 +155,7 @@ mod tests {

use crate::http::{
headers::{HeaderName, HeaderValue},
Pager, Response, StatusCode,
Pager, PagerResult, Response, StatusCode,
};

#[tokio::test]
Expand All @@ -137,8 +168,8 @@ mod tests {

let pager: Pager<Page> = Pager::from_callback(|continuation| async move {
match continuation {
None => Ok((
Response::from_bytes(
None => Ok(PagerResult::Continue {
response: Response::from_bytes(
StatusCode::Ok,
HashMap::from([(
HeaderName::from_static("x-test-header"),
Expand All @@ -147,10 +178,10 @@ mod tests {
.into(),
r#"{"page":1}"#,
),
Some("1"),
)),
Some("1") => Ok((
Response::from_bytes(
continuation: "1",
}),
Some("1") => Ok(PagerResult::Continue {
response: Response::from_bytes(
StatusCode::Ok,
HashMap::from([(
HeaderName::from_static("x-test-header"),
Expand All @@ -159,10 +190,10 @@ mod tests {
.into(),
r#"{"page":2}"#,
),
Some("2"),
)),
Some("2") => Ok((
Response::from_bytes(
continuation: "2",
}),
Some("2") => Ok(PagerResult::Finish {
response: Response::from_bytes(
StatusCode::Ok,
HashMap::from([(
HeaderName::from_static("x-test-header"),
Expand All @@ -171,8 +202,7 @@ mod tests {
.into(),
r#"{"page":3}"#,
),
None,
)),
}),
_ => {
panic!("Unexpected continuation value")
}
Expand Down Expand Up @@ -210,8 +240,8 @@ mod tests {

let pager: Pager<Page> = Pager::from_callback(|continuation| async move {
match continuation {
None => Ok((
Response::from_bytes(
None => Ok(PagerResult::Continue {
response: Response::from_bytes(
StatusCode::Ok,
HashMap::from([(
HeaderName::from_static("x-test-header"),
Expand All @@ -220,8 +250,8 @@ mod tests {
.into(),
r#"{"page":1}"#,
),
Some("1"),
)),
continuation: "1",
}),
Some("1") => Err(typespec::Error::message(
typespec::error::ErrorKind::Other,
"yon request didst fail",
Expand Down