-
Notifications
You must be signed in to change notification settings - Fork 140
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
Allow the client to specify a custom Agent. #76
Conversation
By default, use the same Agent treq always provides, but allow using a custom Agent.
This is interesting to me, as I have a similar need to deal with client certificates issued by an internal CA. I think replacing the agent itself is perhaps doing too much work, when you could just supply a policy: def _client(*args, **kwargs):
policy = kwargs.get('policy', BrowserLikePolicyForHTTPS())
reactor = default_reactor(kwargs.get('reactor'))
pool = default_pool(reactor,
kwargs.get('pool'),
kwargs.get('persistent'))
agent = Agent(reactor, contextFactory=policy, pool=pool)
return HTTPClient(agent) What do you think? |
That seems reasonable. Are you going to create a pull request? |
Yes, but it should have tests and documentation updates, since creating a valid policy object from a cert file isn't immediately obvious. |
The change looks okay-ish to me, but it would be good if this came with documentation as well, explaining how to specify an agent. |
Also tests. I guess that's the same thing @ldanielburr was saying :). |
@glyph - To be clear, I was suggesting that, rather than passing in an agent, which seems like overkill in terms of solving the specific problem of self-signed SSL certs, it would be better to allow for the passing of an IPolicyForHTTPS implementer, as per my code-sketch in the comments, above. |
@ldanielburr - I agree. Sometimes you do need to pass the whole Agent too, of course. |
I've forked the repo, and have started working on the policy-oriented change. |
I've created https://github.com/dreid/treq/pull/80 to allow the user to specify their own IPolicyForHTTPS implementation, which I think satisfies the original use-case describe in this issue. |
Add an in-memory stub version of treq
Current coverage is
|
I had rather forgot I submitted this PR a while back. I had been sitting on the fork and using it in some projects, but I'd really like to see either this PR or #80 (or both) get folded into the main project, as they seem to be the only way to allow treq to deal with one-off internal certs (short of monkeying with the global cert bundle). I added some minimal docs with a short code example and a very basic test. To be fair, the code change was also pretty minimal. Thanks, |
Thanks for checking in @cwaldbieser ! |
Allow the client to specify a custom Agent.
I'm merging this, for now, since the change looks OK and it would be nice to have some way to do this. But I don't think we should stop here. This is an instance of an anti-pattern I've identified - let's call it the "quasiglobal" anti-pattern - where you have an API which maintains some global state, but then, if you like, you can opt out of that global state by passing a parameter along. Superficially this enables easier testing and such, but it creates an obvious division between "normal" code and "testable" code. It's an anti-pattern because it leads to a problem where A calls B and B calls C and C calls D, and A, B, and C all take the quasiglobal parameter (in this case, What I think we should do longer-term is to get rid of the @cwaldbieser - would you be up for doing any further PRs in this area? |
Glyph, I have the cycles to devote to it. Are you saying that (for instance) treq might have a singleton If I am on the wrong track, a short psuedocode example might be useful. Thanks,
|
@cwaldbieser - I'm saying that
|
I see what you mean. So basically, I could just do something like: from treq.client import HTTPClient custom_agent = make_my_agent(secret_sauce) And if I just wanted to use the default agent: import treq as http_client ... That makes sense. Thanks, On Wed, Jul 29, 2015 at 2:20 PM, Glyph [email protected] wrote:
|
Exactly. The main problem here is that the docs don't really cover how to do this, and the fact that functions like So this is mostly a documentation challenge. |
By default, use the same Agent treq always provides, but allow using a custom Agent.
Unless/until treq grows addtional features, this would allow you to customize some of the behavior.
I have been experimenting with this by adding the ability to add internal CA certs to the certificate store. This lets treq make requests to sites with public certs and sites on an internal network. I find this more desirable than adjusting the OS certificate bundle, as treq may be used in different contexts on the same server. E.g. process "A" should really only be dealing with public sites, but process "B" might need to make requests from internal sites.