Skip to content

Commit

Permalink
Upgrade http/hyper 1.0 (#564)
Browse files Browse the repository at this point in the history
  • Loading branch information
Hakuyume authored Feb 7, 2024
1 parent 59be97c commit f350014
Show file tree
Hide file tree
Showing 14 changed files with 122 additions and 148 deletions.
18 changes: 10 additions & 8 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,14 @@ futures = { version = "0.3.15" }
futures-core = { version = "0.3.15", optional = true }
futures-util = { version = "0.3.15", optional = true }
jsonwebtoken = "9"
http = "0.2.5"
http-body = "0.4.5"
hyper = { version = "0.14.13", features = ["client", "http1", "stream", "tcp"] }
hyper-rustls = { version = "0.24.0", optional = true }
hyper-timeout = { version = "0.4.1", optional = true }
hyper-tls = { version = "0.5.0", optional = true }
http = "1.0.0"
http-body = "1.0.0"
http-body-util = "0.1.0"
hyper = "1.1.0"
hyper-rustls = { version = "0.26.0", optional = true }
hyper-timeout = { version = "0.5.1", optional = true }
hyper-tls = { version = "0.6.0", optional = true }
hyper-util = { version = "0.1.3", features = ["client-legacy", "http1"] }
once_cell = "1.7.2"
percent-encoding = "2.2.0"
pin-project = "1.0.12"
Expand All @@ -48,7 +50,7 @@ serde_urlencoded = "0.7.1"
snafu = { version = "0.7", features = ["backtraces"] }
tokio = { version = "1.17.0", default-features = false, optional = true }
tower = { version = "0.4.13", default-features = false, features = ["util", "buffer"] }
tower-http = { version = "0.4.0", features = ["map-response-body", "trace"] }
tower-http = { version = "0.5.1", features = ["map-response-body", "trace"] }
tracing = { version = "0.1.37", features = ["log"], optional = true }
url = { version = "2.2.2", features = ["serde"] }

Expand All @@ -73,5 +75,5 @@ retry = ["tower/retry", "futures-util"]
rustls = ["hyper-rustls"]
rustls-webpki-tokio = ["hyper-rustls/webpki-tokio"]
opentls = ["hyper-tls"]
stream = ["futures-core", "futures-util", "hyper/stream"]
stream = ["futures-core", "futures-util"]
timeout = ["hyper-timeout", "tokio", "tower/timeout"]
4 changes: 3 additions & 1 deletion examples/custom_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@ use std::sync::Arc;
async fn main() -> octocrab::Result<()> {
let connector = HttpsConnectorBuilder::new()
.with_native_roots() // enabled the `rustls-native-certs` feature in hyper-rustls
.unwrap()
.https_only()
.enable_http1()
.build();

let client = hyper::Client::builder().build(connector);
let client = hyper_util::client::legacy::Client::builder(hyper_util::rt::TokioExecutor::new())
.build(connector);
let octocrab = OctocrabBuilder::new_empty()
.with_service(client)
.with_layer(&BaseUriLayer::new(Uri::from_static(
Expand Down
10 changes: 6 additions & 4 deletions src/api/actions.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
//! GitHub Actions
use bytes::Bytes;
use http_body_util::combinators::BoxBody;
use http_body_util::{BodyExt, Collected};
use snafu::ResultExt;

pub mod self_hosted_runners;

use self::self_hosted_runners::{CreateJitRunnerConfigBuilder, ListSelfHostedRunnersBuilder};
use crate::error::{HttpSnafu, HyperSnafu};
use crate::error::HttpSnafu;
use crate::etag::{EntityTag, Etagged};
use crate::models::{
workflows::WorkflowDispatch, workflows::WorkflowListArtifact, ArtifactId, RepositoryId, RunId,
Expand All @@ -13,7 +16,6 @@ use crate::models::{RunnerGroupId, RunnerId};
use crate::{params, FromResponse, Octocrab, Page};
use http::request::Builder;
use http::{header::HeaderMap, Method, StatusCode, Uri};
use hyper::body;

pub struct ListWorkflowRunArtifacts<'octo> {
crab: &'octo Octocrab,
Expand Down Expand Up @@ -268,13 +270,13 @@ impl<'octo> ActionsHandler<'octo> {

async fn follow_location_to_data(
&self,
response: http::Response<hyper::Body>,
response: http::Response<BoxBody<Bytes, crate::Error>>,
) -> crate::Result<bytes::Bytes> {
let data_response = self.crab.follow_location_to_data(response).await?;

let body = data_response.into_body();

body::to_bytes(body).await.context(HyperSnafu)
body.collect().await.map(Collected::to_bytes)
}

/// Downloads and returns the raw data representing a zip of the logs from
Expand Down
6 changes: 4 additions & 2 deletions src/api/repos.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
//! The repositories API.
use bytes::Bytes;
use http::header::ACCEPT;
use http::request::Builder;
use http::Uri;
use http_body_util::combinators::BoxBody;
use snafu::ResultExt;

mod branches;
Expand Down Expand Up @@ -561,7 +563,7 @@ impl<'octo> RepoHandler<'octo> {
self,
reference: impl Into<params::repos::Commitish>,
path: impl AsRef<str>,
) -> Result<http::Response<hyper::Body>> {
) -> Result<http::Response<BoxBody<Bytes, crate::Error>>> {
let route = format!(
"/repos/{owner}/{repo}/contents/{path}",
owner = self.owner,
Expand Down Expand Up @@ -606,7 +608,7 @@ impl<'octo> RepoHandler<'octo> {
pub async fn download_tarball(
&self,
reference: impl Into<params::repos::Commitish>,
) -> Result<http::Response<hyper::Body>> {
) -> Result<http::Response<BoxBody<Bytes, crate::Error>>> {
let route = format!(
"/repos/{owner}/{repo}/tarball/{reference}",
owner = self.owner,
Expand Down
8 changes: 2 additions & 6 deletions src/api/repos/releases.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,12 +194,8 @@ impl<'octo, 'r> ReleasesHandler<'octo, 'r> {
let request = self.parent.crab.build_request(builder, None::<&()>)?;
let response = self.parent.crab.execute(request).await?;
let response = self.parent.crab.follow_location_to_data(response).await?;
Ok(response
.into_body()
.map_err(|source| crate::error::Error::Hyper {
source,
backtrace: snafu::Backtrace::generate(),
}))
Ok(http_body_util::BodyStream::new(response.into_body())
.try_filter_map(|frame| futures_util::future::ok(frame.into_data().ok())))
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/etag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ pub struct EntityTag {
}

impl EntityTag {
pub fn extract_from_response(response: &http::Response<hyper::Body>) -> Option<EntityTag> {
pub fn extract_from_response<B>(response: &http::Response<B>) -> Option<EntityTag> {
response
.headers()
.get("ETag")
Expand Down
16 changes: 11 additions & 5 deletions src/from_response.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
use bytes::Bytes;
use http_body::Body;
use http_body_util::BodyExt;
use snafu::ResultExt;

/// A trait for mapping from a `http::Response` to an another type.
#[async_trait::async_trait]
pub trait FromResponse: Sized {
async fn from_response(response: http::Response<hyper::Body>) -> crate::Result<Self>;
async fn from_response<B>(response: http::Response<B>) -> crate::Result<Self>
where
B: Body<Data = Bytes, Error = crate::Error> + Send;
}

#[async_trait::async_trait]
impl<T: serde::de::DeserializeOwned> FromResponse for T {
async fn from_response(response: http::Response<hyper::Body>) -> crate::Result<Self> {
async fn from_response<B>(response: http::Response<B>) -> crate::Result<Self>
where
B: Body<Data = Bytes, Error = crate::Error> + Send,
{
let (_, body) = response.into_parts();
let body = hyper::body::to_bytes(body)
.await
.context(crate::error::HyperSnafu)?;
let body = body.collect().await?.to_bytes();
let de = &mut serde_json::Deserializer::from_slice(&body);
return serde_path_to_error::deserialize(de).context(crate::error::JsonSnafu);
}
Expand Down
Loading

0 comments on commit f350014

Please sign in to comment.