diff --git a/http/Cargo.toml b/http/Cargo.toml index e62b659..93f405f 100644 --- a/http/Cargo.toml +++ b/http/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "gw2lib" -version = "3.0.0" +version = "3.0.1" edition = "2021" workspace = "../" description = "A simple to use Gw2 API wrapper library" diff --git a/http/src/client/requester.rs b/http/src/client/requester.rs index 2337976..6ca6386 100644 --- a/http/src/client/requester.rs +++ b/http/src/client/requester.rs @@ -138,7 +138,7 @@ pub trait Requester: Sized + Sync let request = build_request::( self, - &T::format_url(T::format_id(&id).as_ref()), + T::format_url(T::format_id(&id).as_ref()), None, )?; @@ -595,69 +595,54 @@ async fn wait_for_rate_limit, const A: bool, const F: bool> Ok(()) } -fn build_request< - T: Endpoint, - Q: Into, - Req: Requester, - const A: bool, - const F: bool, ->( +fn build_request, Req: Requester, const A: bool, const F: bool>( req: &Req, - path: &str, + path: impl AsRef, extra_queries: Option, ) -> Result, EndpointError> { if T::AUTHENTICATED && !A { return Err(EndpointError::NotAuthenticated); } - let uri = build_query::( - &req.client().host, - path, - req.client().language, - extra_queries, - ); + let client = req.client(); - let mut request = hyper::Request::builder().uri(uri); + let mut pnq = String::with_capacity(400); + pnq.push('/'); + pnq.push_str(path.as_ref()); + pnq.push('?'); - request = request.header("X-Schema-Version", T::VERSION); - if T::AUTHENTICATED { - request = request.header( - "Authorization", - &format!("Bearer {}", req.client().api_key.as_ref().unwrap()), - ); - } - - let request = request.body(hyper::Body::empty()).unwrap(); - - Ok(request) -} + pnq.push_str("v="); + pnq.push_str(T::VERSION); -fn build_query>( - host: &str, - path: &str, - lang: Language, - extra_queries: Option, -) -> Uri { - let (scheme, host) = host.split_once("://").expect("invalid host"); + if let Some(extra) = extra_queries { + pnq.push('&'); + pnq.push_str(extra.as_ref()); + } - let mut args = Vec::new(); if T::LOCALE { - args.push(format!("lang={}", lang.as_str())); + pnq.push_str("&lang="); + pnq.push_str(client.language.as_str()); } - if let Some(q) = extra_queries { - args.push(q.into()); - } - - let query = args.join("&"); - let pnq = format!("/{}?{}", &path, &query); + if T::AUTHENTICATED { + pnq.push_str("&access_token="); + pnq.push_str(client.api_key.as_ref().unwrap()); + } - Uri::builder() + let (scheme, host) = client.host.split_once("://").expect("invalid host"); + let uri = Uri::builder() .scheme(scheme) .authority(host) .path_and_query(pnq) .build() - .expect("invalid uri") + .expect("invalid uri"); + + let request = hyper::Request::builder() + .uri(uri) + .body(hyper::Body::empty()) + .unwrap(); + + Ok(request) } /// returns the remaining ids not found in cache