Use typed Url to construct XRPC URI #1

closed
opened by bad-example.com targeting main from bad-example.com/jacquard: fix/url-leading-double-slash

this probably either does too much or too little:

  • could have fixed the double-slash thing by stripping trailing slash from the base uri or,
  • if you like typed URLs, it probably makes sense to do a bigger refactor toward them.

but it should at least fix https://tangled.org/@nonbinary.computer/jacquard/issues/7

Changed files
+9 -6
crates
jacquard
src
+9 -6
crates/jacquard/src/client.rs
···
use std::future::Future;
use bytes::Bytes;
-
pub use error::{ClientError, Result};
+
pub use error::{ClientError, Result, TransportError};
use http::{
HeaderName, HeaderValue, Request,
header::{AUTHORIZATION, CONTENT_TYPE, InvalidHeaderValue},
···
R: XrpcRequest,
C: XrpcClient + ?Sized,
{
-
// Build URI: base_uri + /xrpc/ + NSID
-
let mut uri = format!("{}/xrpc/{}", client.base_uri(), R::NSID);
+
// Configure the XRPC endpoint
+
let mut url: reqwest::Url = client
+
.base_uri()
+
.parse()
+
.map_err(|e| error::EncodeError::Other(format!("failed to parse base_uri: {e}").into()))?;
+
url.set_path(&format!("/xrpc/{}", R::NSID));
// Add query parameters for Query methods
if let XrpcMethod::Query = R::METHOD {
let qs = serde_html_form::to_string(&request).map_err(error::EncodeError::from)?;
if !qs.is_empty() {
-
uri.push('?');
-
uri.push_str(&qs);
+
url.set_query(Some(&qs))
}
}
···
XrpcMethod::Procedure(_) => http::Method::POST,
};
-
let mut builder = Request::builder().method(method).uri(&uri);
+
let mut builder = Request::builder().method(method).uri(&url.to_string());
// Add Content-Type for procedures
if let XrpcMethod::Procedure(encoding) = R::METHOD {