rustc 1.3.0-nightly (98dcd5e10 2015-07-11)
I want to wrap some of hyper's functionality for usability's sake. Every time I do a GET I want to always send some default headers. This is the way I currently have it working:
fn get<'a, U: IntoUrl>(&'a self, url: U) -> RequestBuilder<'a, U> {
let mut headers = Headers::new();
headers.set(
Authorization(
Token { token: self.token.clone() }
)
);
headers.set(ContentType::json());
self.client.get(url).headers(headers)
}
But the implementation which defines this method knows what the domain of the URL is so instead of passing in the fully qualified URL into that method I would only like to pass in the path of the URL. Something like this (doesn't compile):
fn get<'a>(&'a self, path: &'a str) -> RequestBuilder<'a, String>
{
let url = self.url(path); // builds fully qualified url as String
let mut headers = Headers::new();
headers.set(
Authorization(
Token { token: self.token.clone() }
)
);
headers.set(ContentType::json());
self.client.get(url).headers(headers)
}
The problem is that I keep getting the following error:
the trait `hyper::client::IntoUrl` is not implemented for the type `collections::string::String`
Any ideas on how I could do this? I don't know what the difference is between std::string::String and collections::string::String. I import std::string::String directly.
I tried to implement IntoUrl for collections::string::String but I couldn't use UrlError.
They are the same thing. std reexports from collections.
In this case, the client doesn't need to take ownership of the String,
since it creates a Url from it and doesn't need it longer. You can pass
&url, or construct the Url yourself and pass that.
On Sun, Jul 19, 2015 at 9:06 PM Alexei Nunez [email protected]
wrote:
rustc 1.3.0-nightly (98dcd5e10 2015-07-11)
I want to wrap some of hyper's functionality for usability's sake. Every
time I do a get I want to always send some default headers. This is the way
I currently have it working:fn get<'a, U: IntoUrl>(&'a self, url: U) -> RequestBuilder<'a, U> { let mut headers = Headers::new(); headers.set( Authorization( Token { token: self.token.clone() } ) ); headers.set(ContentType::json()); self.client.get(url).headers(headers) }But the implementation which defines this method knows what the domain of
the URL is so instead of passing in the fully qualified URL into that
method I would only like to pass in the path of the URL. Something like
this (doesn't compile):fn get<'a>(&'a self, path: &'a str) -> RequestBuilder<'a, String> { let url = self.url(path); // builds fully qualified url as String let mut headers = Headers::new(); headers.set( Authorization( Token { token: self.token.clone() } ) ); headers.set(ContentType::json()); self.client.get(url).headers(headers) }The problem is that I keep getting the following error:
the trait
hyper::client::IntoUrlis not implemented for the typecollections::string::StringAny ideas on how I could do this? I don't know what the difference is
between std::string::String and collections::string::String. I import
std::string::String directly.—
Reply to this email directly or view it on GitHub
https://github.com/hyperium/hyper/issues/612.
@seanmonstar Thanks for the help but none of that worked :(
Does the function signature look O.K? I think my problem is that I'm trying to return a RequestBuilder but the type I'm returning and the parameterized types are mismatching
Ah, yes, that's the issue. If you look here in the docs, you'll see what IntoUrl is implemented for. You can switch your function signature to return RequestBuilder<'a, &'a str>, and inside the function call self.client.get(&*url).
@seanmonstar They build a modified String on the stack so they can't use &str in this case?
@arnm I think something similar to the below may work for you (assuming you import the IntoUrl trait):
fn get<'a>(&'a self, path: &'a str) -> RequestBuilder<'a, Url> {
let url = self.url(path); // builds fully qualified url as String
let url = url.into_url(); // convert it to an actual Url instance
... // use url, same as before
}
@Ryman Thanks, that worked.
I get the same error when i call the method get
let url = ["https://api.leancloud.cn/1.1/classes/",class.as_ref(),
"/",id.as_ref()].concat();
let mut ress = client.get(url).headers(headers).send().unwrap();
class and id is String type.
but I get the IntoUrl code
pub trait IntoUrl {
/// Consumes the object, trying to return a Url.
fn into_url(self) -> Result<Url, UrlError>;
}
impl IntoUrl for Url {
fn into_url(self) -> Result<Url, UrlError> {
Ok(self)
}
}
impl<'a> IntoUrl for &'a str {
fn into_url(self) -> Result<Url, UrlError> {
Url::parse(self)
}
}
//may be it is impl for String?
impl<'a> IntoUrl for &'a String {
fn into_url(self) -> Result<Url, UrlError> {
Url::parse(self)
}
}
So i don't know why about this i get the error
my rust and cargo:
rustc 1.3.0 (9a92aaf19 2015-09-15)
cargo 0.4.0-nightly (553b363 2015-08-03) (built 2015-08-02)
@paomian just pass a reference to your String, by doing client.get(&url)...
I could implement IntoUrl for String, but I don't actually need to take ownership of the String to parse it, a reference it sufficient.
Most helpful comment
@paomian just pass a reference to your String, by doing
client.get(&url)...I could implement
IntoUrlforString, but I don't actually need to take ownership of the String to parse it, a reference it sufficient.