async/await, 2018 edition, updated deps#21
Open
brigand wants to merge 2 commits into
Open
Conversation
Author
|
I've learned that more work is needed to properly support the newer version of k8s-openapi. Properly making the request is more like the following, where the tuple argument is returned by the k8s-openapi functions. This has the benefit of encoding the request/response types in one function call, rather than manually specifying the expected response type. use tokio::stream::StreamExt;
use k8s_openapi as kapi;
// ...snip
pub async fn request<T>(
&self,
request: (
http::Request<Vec<u8>>,
fn(_: http::StatusCode) -> kapi::ResponseBody<T>,
),
) -> Result<T>
where
T: kapi::Response,
{
let (parts, body) = request.0.into_parts();
let uri_str = format!("{}{}", self.host, parts.uri);
let mut req = match parts.method {
http::Method::GET => self.client.get(&uri_str),
http::Method::POST => self.client.post(&uri_str),
http::Method::DELETE => self.client.delete(&uri_str),
http::Method::PUT => self.client.put(&uri_str),
method => {
bail!(other(format!("Unexpected HTTP method {:?}", method)));
}
};
let res = req.body(body).send().await?;
let mut k8s_res = request.1(res.status());
let mut stream = res.bytes_stream();
while let Some(item) = stream.next().await {
if let Ok(item) = item {
k8s_res.append_slice(item.as_ref());
}
}
let result = k8s_res.parse()?;
Ok(result)
}Currently the PR probably shouldn't be merged. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This brings the crate up to date with changes in the ecosystem.
The changes here backwards-incompatible, but allow it to be easily used with recent tokio (std::future based) and k8s-openapi-codegen versions (previously broken by a switch to
http@2.x.x).The file system I/O is still done with blocking APIs, as the code looks tricky to migrate. The consumer can schedule that part with spawn_blocking if needed.
Tests and the list_pod example work. I haven't tested the incluster_config example.