-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to add an Authorization layer to Client or RequestBuild ? #346
Comments
Actually hyper does not have a function to provide authorisation. But you can create a new client by wrapping client and passing all functions down to the original client. Pseudocode:
|
Thank you. pub struct Client<C, B: Builder> {
connector: C,
redirect_policy: RedirectPolicy,
_m: PhantomData<B>,
} |
Just for the recordThe original issue was not solved. A hyper Closing the issue without actually dealing with the described problem is behaviour I do not approve of. In case the point made here is not clear or faulty, then this was not made clear in the previous communication either. |
Hey @Byron, if you feel like there's more to discuss to have in this issue, I'm happy to reopen. I noticed that @seanmonstar closed this, but the issue also looked resolved to me as @pyfisch told you what to do in this case. On closer inspection, I can see how the solution is not entirely satisfactory. I agree that there's definitely more to do in terms of API surface to allow more complex modifications or extensions to Client and RequestBuilder, but that seems only tangentially related to this issue. I'd rather open another issue and link to this one from there though, since the major point of this issue has sort of been dealt with. Sound good? |
Considering I didn't (even) get the subject's grammar right, I'd be happy if you would open an issue that is more to the point, the way you see it by now. |
Filed #378 :) |
@Byron sorry, I didn't mean to just shrug this off. I read @pyfisch's response and felt it was sufficient. As for this specific case, I don't think Similar in nodejs, where there is the var request = require('request');
request.post({
url: 'https://example.domain/path',
auth: someAuthValue
}).pipe(process.stdout); I'd imagine a specialized client looking like this: struct Client {
inner: hyper::Client,
auth: String
}
impl Client {
fn request(&mut self, method: hyper::Method, url: url::Url) -> hyper::client::RequestBuilder {
self.inner.request(method,url)
.header(hyper::header::Authorization(self.auth.clone()))
}
} You could imagine any sorts of options that a specialized client could handle, such as |
@seanmonstar It's alright :) - I might have been reacting a bit strongly to this too, had a bad day. The reason I seem to be so much into having a func New(client *http.Client) (*Service, error) {
if client == nil {
return nil, errors.New("client is nil")
}
s := &Service{client: client, BasePath: basePath}
s.Userinfo = NewUserinfoService(s)
return s, nil
} By now I feel differently about it. Currently I just take a hyper-client and an authenticator, which takes care about providing tokens for authentication. That looks very much like this and feels OK to me, at least on the client side. On the side of the implementor, you see that not having a Maybe some day there will be a clear idea on how these cases can and should be tackled, and we do the right thing™ to our APIs to make them as nice and easy to use as possible. |
I see. That's no fun carrying around that generic, since you don't care about it. |
Though, we already do this for |
Compared to the cost of other operations, mainly io, the virtual calls seem to not matter at all. |
When looking at the google API implementations, you usually see the pattern that services are instantiated with an http client, through which all requests will be performed. Authorisation is handled through a special version of an HTTP-Client, which will insert the respective headers on the fly and even refresh expired tokens when needed.
As Client has no trait, and as the only things I can affect are the NetworkConnector and NetworkStream, both of which don't have access to the Header anymore, I wonder how I would best achieve that without working around Hyper entirely, in that case.
Sorry again for posting a question here, just let me know if you would like me to re-post on StackOverflow.
Thank you
The text was updated successfully, but these errors were encountered: