Skip to content

Commit

Permalink
Replace reqwest with http and hyper, and tower (#297)
Browse files Browse the repository at this point in the history
* sync changes made so far

* get it to compile

* clippy fixes, and enable Timeout/default HTTPS  connector

* prepend routes with slashes where applicable

* rename base_url -> base uri
rename url -> route where applicable
revert to older version of base64
fix more instances with missing /

* only override base-uri IF authority/scheme are not present

* add #[tokio::test] for tests/docs since tower(specifically buffer module) requires async runtime: https://users.rust-lang.org/t/no-reactor-running-when-calling-runtime-spawn/81256.

add tracing as a feature

* fix stream feature

* make base_uri a wrapping layer

* use hyper-tls instead

* Update README.md

* Update README.md

* add simple retry feature

* fmt

* move new to Octocrab

* set user-agent on build

* fmt

* remove debugging code...

* remove unused dep

* fix base_uri layer to actually take parameters from current_uri

* make parameterized_uri be able to ingest full URIs(for case with pages), and combine parameters with these uris

* add comments

* add comments

* service based octocrab needs tokio env

* Make builder extensible, supporting different building capabilities(with_layer, Config, etc)

* add comments

* add new() to extra headers

* add example of using custom client

* backward compatibility fixes:
 - add new_empty to facilitate bare bones octocrab builder. Point fn new to fn default to make changes backward compatible.
 - add base_url function, and point it to base_uri with deprecation notice

* sync changes made so far

* get it to compile

* clippy fixes, and enable Timeout/default HTTPS  connector

* prepend routes with slashes where applicable

* rename base_url -> base uri
rename url -> route where applicable
revert to older version of base64
fix more instances with missing /

* only override base-uri IF authority/scheme are not present

* add #[tokio::test] for tests/docs since tower(specifically buffer module) requires async runtime: https://users.rust-lang.org/t/no-reactor-running-when-calling-runtime-spawn/81256.

add tracing as a feature

* fix stream feature

* make base_uri a wrapping layer

* use hyper-tls instead

* Update README.md

* Update README.md

* add simple retry feature

* fmt

* move new to Octocrab

* set user-agent on build

* fmt

* remove debugging code...

* remove unused dep

* fix base_uri layer to actually take parameters from current_uri

* make parameterized_uri be able to ingest full URIs(for case with pages), and combine parameters with these uris

* add comments

* add comments

* service based octocrab needs tokio env

* Make builder extensible, supporting different building capabilities(with_layer, Config, etc)

* add comments

* add new() to extra headers

* add example of using custom client

* backward compatibility fixes:
 - add new_empty to facilitate bare bones octocrab builder. Point fn new to fn default to make changes backward compatible.
 - add base_url function, and point it to base_uri with deprecation notice

* sync commits

* better message

* fmt

* retry on 5**, and 429

* fix up a few base64 functions after the upgrade to 0.21

* cargo fmt

* sync changes made so far

* get it to compile

* clippy fixes, and enable Timeout/default HTTPS  connector

* prepend routes with slashes where applicable

* rename base_url -> base uri
rename url -> route where applicable
revert to older version of base64
fix more instances with missing /

* only override base-uri IF authority/scheme are not present

* add #[tokio::test] for tests/docs since tower(specifically buffer module) requires async runtime: https://users.rust-lang.org/t/no-reactor-running-when-calling-runtime-spawn/81256.

add tracing as a feature

* fix stream feature

* make base_uri a wrapping layer

* use hyper-tls instead

* Update README.md

* Update README.md

* add simple retry feature

* fmt

* move new to Octocrab

* set user-agent on build

* fmt

* remove debugging code...

* remove unused dep

* fix base_uri layer to actually take parameters from current_uri

* make parameterized_uri be able to ingest full URIs(for case with pages), and combine parameters with these uris

* add comments

* service based octocrab needs tokio env

* Make builder extensible, supporting different building capabilities(with_layer, Config, etc)

* add comments

* add new() to extra headers

* add example of using custom client

* backward compatibility fixes:
 - add new_empty to facilitate bare bones octocrab builder. Point fn new to fn default to make changes backward compatible.
 - add base_url function, and point it to base_uri with deprecation notice

* sync changes made so far

* get it to compile

* clippy fixes, and enable Timeout/default HTTPS  connector

* rename base_url -> base uri
rename url -> route where applicable
revert to older version of base64
fix more instances with missing /

* add #[tokio::test] for tests/docs since tower(specifically buffer module) requires async runtime: https://users.rust-lang.org/t/no-reactor-running-when-calling-runtime-spawn/81256.

add tracing as a feature

* fix stream feature

* make base_uri a wrapping layer

* use hyper-tls instead

* Update README.md

* Update README.md

* add simple retry feature

* add comments

* Make builder extensible, supporting different building capabilities(with_layer, Config, etc)

* backward compatibility fixes:
 - add new_empty to facilitate bare bones octocrab builder. Point fn new to fn default to make changes backward compatible.
 - add base_url function, and point it to base_uri with deprecation notice

* sync commits

* better message

* fmt

* retry on 5**, and 429

* fix up a few base64 functions after the upgrade to 0.21

* resolve merge conflicts, and cargo fmt + fix

* remove references to Octocrab::absolute_url

* fmt
  • Loading branch information
L1ghtman2k authored Mar 29, 2023
1 parent a546dcf commit 16134a8
Show file tree
Hide file tree
Showing 95 changed files with 1,876 additions and 1,063 deletions.
40 changes: 23 additions & 17 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ keywords = ["github", "github-api"]
targets = ["x86_64-unknown-linux-gnu"]

[dependencies]
reqwest = { version = "0.11.10", default-features = false, features = ["json"] }
serde = { version = "1.0.126", features = ["derive"] }
serde_json = "1.0.64"
serde_path_to_error = "0.1.4"
Expand All @@ -33,33 +32,40 @@ arc-swap = "1.3.0"
base64 = "0.21.0"
bytes = "1.0.1"
jsonwebtoken = "8"
futures-core = { version = "0.3.15", optional = true }
futures-util = { version = "0.3.15", optional = true }
futures = { version = "0.3.15" }
secrecy = "0.8.0"
cfg-if = "1.0.0"
either = "1.8.0"
tracing = "0.1"
hyper = { version = "0.14.13", features = ["client", "http1", "stream", "tcp"] }
http = "0.2.5"
tower-http = { version = "0.3.2", features = ["map-response-body", "trace"] }
hyper-timeout = {version = "0.4.1", optional = true }
http-body = "0.4.5"
tower = { version = "0.4.13", default-features = false, features = ["util", "buffer"] }
hyper-rustls = { version = "0.23.2", optional = true }
tracing = { version = "0.1.37", features = ["log"], optional = true }
serde_urlencoded = "0.7.1"
pin-project = "1.0.12"
tokio = { version = "1.17.0", default-features = false, optional = true }
futures-core = { version = "0.3.15", optional = true }
futures-util = { version = "0.3.15", optional = true }
hyper-tls = { version = "0.5.0", optional = true }
reqwest = { version = "0.11.10", default-features = false, features = ["json"] }

rustls-pemfile = { version = "1.0.0", optional = true }
[dev-dependencies]
tokio = { version = "1.17.0", default-features = false, features = [
"macros",
"rt-multi-thread",
"time",
] }
tokio-test = "0.4.2"
wiremock = "0.5.3"

[features]
default = ["native-tls"]
rustls = ["rustls-tls"] # Legacy support (<=0.17.0)

# Enables native-tls specific functionality not available by default.
native-tls = ["reqwest/native-tls"]
native-tls-alpn = ["reqwest/native-tls-alpn"]
native-tls-vendored = ["reqwest/native-tls-vendored"]

rustls-tls = ["reqwest/rustls-tls"]
rustls-tls-manual-roots = ["reqwest/rustls-tls-manual-roots"]
rustls-tls-webpki-roots = ["reqwest/rustls-tls-webpki-roots"]
rustls-tls-native-roots = ["reqwest/rustls-tls-native-roots"]
default = ["tls", "timeout", "tracing", "retry"]

stream = ["futures-core", "futures-util", "reqwest/stream"]
retry = ["tower/retry", "futures-util"]
tls = ["hyper-tls"]
timeout = ["hyper-timeout", "tokio", "tower/timeout"]
stream = ["futures-core", "futures-util", "hyper/stream"]
9 changes: 4 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,14 +125,13 @@ the request.

```rust
let octocrab = octocrab::instance();
let response = octocrab
._get("https://api.github.com/organizations", None::<&()>)
let response = octocrab
._get("https://api.github.com/organizations")
.await?;

// You can also use `Octocrab::absolute_url` if you want to still to go to
// the same base.
// You can also use `Uri::builder().authority("<my custom base>").path_and_query("<my custom path>")` if you want to customize the base uri and path.
let response = octocrab
._get(octocrab.absolute_url("/organizations")?, None::<&()>)
._get(Uri::builder().path_and_query("/organizations").build().expect("valid uri"))
.await?;
```

Expand Down
41 changes: 41 additions & 0 deletions examples/custom_client.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
use http::header::USER_AGENT;
use http::Uri;
use hyper_tls::HttpsConnector;

use octocrab::service::middleware::base_uri::BaseUriLayer;
use octocrab::service::middleware::extra_headers::ExtraHeadersLayer;
use octocrab::{AuthState, OctocrabBuilder};
use std::sync::Arc;

#[tokio::main]
async fn main() -> octocrab::Result<()> {
let client = hyper::Client::builder().build(HttpsConnector::new());
let octocrab = OctocrabBuilder::new_empty()
.with_service(client)
.with_layer(&BaseUriLayer::new(Uri::from_static(
"https://api.github.com",
)))
.with_layer(&ExtraHeadersLayer::new(Arc::new(vec![(
USER_AGENT,
"octocrab".parse().unwrap(),
)])))
.with_auth(AuthState::None)
.build()
.unwrap();

let repo = octocrab.repos("rust-lang", "rust").get().await?;

let repo_metrics = octocrab
.repos("rust-lang", "rust")
.get_community_profile_metrics()
.await?;

println!(
"{} has {} stars and {}% health percentage",
repo.full_name.unwrap(),
repo.stargazers_count.unwrap_or(0),
repo_metrics.health_percentage
);

Ok(())
}
6 changes: 3 additions & 3 deletions examples/device_flow.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use either::Either;
use reqwest::header::ACCEPT;
use http::header::ACCEPT;
use std::time::Duration;

#[tokio::main]
async fn main() -> octocrab::Result<()> {
let client_id = secrecy::Secret::from(std::env::var("GITHUB_CLIENT_ID").unwrap());
let crab = octocrab::Octocrab::builder()
.base_url("https://github.com")?
.base_uri("https://github.com")?
.add_header(ACCEPT, "application/json".to_string())
.build()?;

Expand Down Expand Up @@ -41,5 +41,5 @@ async fn main() -> octocrab::Result<()> {
};

println!("Authorization succeeded with access to {:?}", auth.scope);
return Ok(());
Ok(())
}
5 changes: 1 addition & 4 deletions examples/get_content.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,7 @@ async fn main() -> octocrab::Result<()> {
.send()
.await?;

println!(
"{} files/dirs in the repo root",
content.items.into_iter().count()
);
println!("{} files/dirs in the repo root", content.items.len());

Ok(())
}
2 changes: 1 addition & 1 deletion examples/github_app_authentication.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ async fn main() -> octocrab::Result<()> {
}

fn read_env_var(var_name: &str) -> String {
let err = format!("Missing environment variable: {}", var_name);
let err = format!("Missing environment variable: {var_name}");
std::env::var(var_name).expect(&err)
}
2 changes: 1 addition & 1 deletion examples/github_app_authentication_manual.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,6 @@ async fn main() -> octocrab::Result<()> {
}

fn read_env_var(var_name: &str) -> String {
let err = format!("Missing environment variable: {}", var_name);
let err = format!("Missing environment variable: {var_name}");
std::env::var(var_name).expect(&err)
}
2 changes: 1 addition & 1 deletion examples/paging_results.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ async fn main() -> octocrab::Result<()> {
prs.extend(new_page.take_items());

for pr in prs.drain(..) {
println!("{:?}", pr);
println!("{pr:?}");
}

current_page = new_page;
Expand Down
2 changes: 1 addition & 1 deletion examples/print_license.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ async fn main() -> octocrab::Result<()> {
.license()
.await?;

println!("{:#?}", license);
println!("{license:#?}");

Ok(())
}
2 changes: 1 addition & 1 deletion examples/print_pr_diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ async fn main() -> octocrab::Result<()> {
.get_diff(72033)
.await?;

println!("{}", diff);
println!("{diff}");

Ok(())
}
4 changes: 2 additions & 2 deletions examples/search_issues.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
.send()
.await
{
Ok(page) => println!("{:#?}", page),
Err(error) => println!("{:#?}", error),
Ok(page) => println!("{page:#?}"),
Err(error) => println!("{error:#?}"),
}

Ok(())
Expand Down
2 changes: 1 addition & 1 deletion examples/update_pull_request_branch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ async fn main() -> octocrab::Result<()> {
.update_branch(200)
.await?;

println!("Result of pull request update: {}", update,);
println!("Result of pull request update: {update}",);

Ok(())
}
Loading

0 comments on commit 16134a8

Please sign in to comment.