Skip to content

Commit 8819f04

Browse files
committed
Enable usage of the crate only as a parser + types via.
The client specific functions are now enabled via the `client` feature. This enables more specific handling of the http requests and makes testing easier.
1 parent 0a0ecc8 commit 8819f04

File tree

10 files changed

+457
-260
lines changed

10 files changed

+457
-260
lines changed

.github/workflows/ci.yml

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ jobs:
3838
uses: actions/cache@v1
3939
id: cache
4040
with:
41-
path: |
41+
path: |
4242
~/.cargo/registry/index
4343
~/.cargo/registry/cache
4444
~/.cargo/git
@@ -65,7 +65,6 @@ jobs:
6565
with:
6666
command: fmt
6767
args: --all -- --check
68-
6968
clippy:
7069
name: Clippy
7170
runs-on: ubuntu-latest
@@ -81,17 +80,22 @@ jobs:
8180
uses: actions/cache@v1
8281
id: cache
8382
with:
84-
path: |
83+
path: |
8584
~/.cargo/registry/index
8685
~/.cargo/registry/cache
8786
~/.cargo/git
8887
target
8988
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
89+
- name: Run clippy
90+
uses: actions-rs/cargo@v1
91+
with:
92+
command: clippy
93+
args: --locked
9094
- name: Run clippy --all-targets --all-features
9195
uses: actions-rs/cargo@v1
9296
with:
9397
command: clippy
94-
args: --locked --all-targets --all-features
98+
args: --locked --all-targets --all-features
9599
docs:
96100
name: Docs
97101
runs-on: ubuntu-latest
@@ -106,7 +110,7 @@ jobs:
106110
uses: actions/cache@v1
107111
id: cache
108112
with:
109-
path: |
113+
path: |
110114
~/.cargo/registry/index
111115
~/.cargo/registry/cache
112116
~/.cargo/git

Cargo.toml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,12 @@ readme = "README.md"
1212
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
1313
[features]
1414
default = []
15-
reqwest = ["dep:reqwest"]
16-
surf_client_curl = ["dep:surf", "surf/curl-client"]
17-
surf = ["dep:surf", "dep:http-types", "http-types?/hyperium_http"]
15+
client = []
16+
reqwest = ["dep:reqwest", "client"]
17+
surf_client_curl = ["surf", "surf/curl-client"]
18+
surf = ["dep:surf", "dep:http-types", "http-types?/hyperium_http", "client"]
1819
mock_api = []
19-
all = ["surf_client_curl", "dep:reqwest"]
20+
all = ["surf_client_curl", "reqwest"]
2021

2122
[dependencies]
2223
thiserror = "1.0.29"

release.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,5 @@ pre-release-replacements = [
99
{file="CHANGELOG.md", search="ReleaseDate", replace="{{date}}", prerelease=false},
1010
{file="CHANGELOG.md", search="<!-- next-header -->", replace="<!-- next-header -->\n\n## [Unreleased] - ReleaseDate\n\n[Commits](https://github.com/twitch-rs/twitch_oauth2/compare/v{{version}}...Unreleased)", prerelease=false},
1111
{file="README.md", search="twitch_oauth2/[a-z0-9\\.-]+/twitch_oauth2", replace="{{crate_name}}/{{version}}/{{crate_name}}", prerelease=true},
12-
{file="src/lib.rs", search="https://docs.rs/twitch_oauth2/[a-z0-9\\.-]+", replace="https://docs.rs/{{crate_name}}/{{version}}", prerelease=true},
1312
{file="Cargo.toml", search="https://docs.rs/twitch_oauth2/[a-z0-9\\.-]+", replace="https://docs.rs/{{crate_name}}/{{version}}", prerelease=true},
1413
]

src/client.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ pub trait Client<'a>: Sync + Send + 'a {
2020
/// Send a request
2121
fn req(
2222
&'a self,
23-
request: crate::HttpRequest,
24-
) -> BoxedFuture<'a, Result<crate::HttpResponse, <Self as Client>::Error>>;
23+
request: http::Request<Vec<u8>>,
24+
) -> BoxedFuture<'a, Result<http::Response<Vec<u8>>, <Self as Client>::Error>>;
2525
}
2626

2727
#[doc(hidden)]
@@ -35,8 +35,8 @@ impl<'a> Client<'a> for DummyClient {
3535

3636
fn req(
3737
&'a self,
38-
_: crate::HttpRequest,
39-
) -> BoxedFuture<'a, Result<crate::HttpResponse, Self::Error>> {
38+
_: http::Request<Vec<u8>>,
39+
) -> BoxedFuture<'a, Result<http::Response<Vec<u8>>, Self::Error>> {
4040
Box::pin(async move { Err(self.clone()) })
4141
}
4242
}
@@ -49,8 +49,8 @@ impl<'a> Client<'a> for ReqwestClient {
4949

5050
fn req(
5151
&'a self,
52-
request: crate::HttpRequest,
53-
) -> BoxedFuture<'a, Result<crate::HttpResponse, Self::Error>> {
52+
request: http::Request<Vec<u8>>,
53+
) -> BoxedFuture<'a, Result<http::Response<Vec<u8>>, Self::Error>> {
5454
// Reqwest plays really nice here and has a try_from on `http::Request` -> `reqwest::Request`
5555
let req = match reqwest::Request::try_from(request) {
5656
Ok(req) => req,
@@ -98,8 +98,8 @@ impl<'a> Client<'a> for SurfClient {
9898

9999
fn req(
100100
&'a self,
101-
request: crate::HttpRequest,
102-
) -> BoxedFuture<'a, Result<crate::HttpResponse, Self::Error>> {
101+
request: http::Request<Vec<u8>>,
102+
) -> BoxedFuture<'a, Result<http::Response<Vec<u8>>, Self::Error>> {
103103
// First we translate the `http::Request` method and uri into types that surf understands.
104104

105105
let method: surf::http::Method = request.method().clone().into();
@@ -124,7 +124,7 @@ impl<'a> Client<'a> for SurfClient {
124124
}
125125

126126
// assembly the request, now we can send that to our `surf::Client`
127-
req.body_bytes(&request.body());
127+
req.body_bytes(request.body());
128128

129129
let client = self.clone();
130130
Box::pin(async move {

src/id.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,14 @@
22
33
use serde::{Deserialize, Serialize};
44

5-
use crate::AccessToken;
5+
use crate::{AccessToken, RequestParseError};
66
use std::time::Duration;
77
/// Twitch's representation of the oauth flow.
8+
///
9+
/// Retrieve with
10+
///
11+
/// * [`UserTokenBuilder::get_user_token_request`](crate::tokens::UserTokenBuilder::get_user_token_request)
12+
/// * [`AppAccessToken::::get_app_access_token_request`](crate::tokens::AppAccessToken::get_app_access_token_request)
813
#[derive(Clone, Debug, Deserialize, Serialize)]
914
pub struct TwitchTokenResponse {
1015
/// Access token
@@ -21,6 +26,15 @@ pub struct TwitchTokenResponse {
2126
pub scopes: Option<Vec<crate::Scope>>,
2227
}
2328

29+
impl TwitchTokenResponse {
30+
/// Create a [TwitchTokenResponse] from a [http::Response]
31+
pub fn from_response<B: AsRef<[u8]>>(
32+
resp: &http::Response<B>,
33+
) -> Result<TwitchTokenResponse, RequestParseError> {
34+
crate::parse_response(resp)
35+
}
36+
}
37+
2438
/// Twitch's representation of the oauth flow for errors
2539
#[derive(Clone, Debug, Deserialize, Serialize, thiserror::Error)]
2640
pub struct TwitchTokenErrorResponse {

0 commit comments

Comments
 (0)