{"action":"created","issue":{"url":"https://api.github.com/repos/lightblue-platform/lightblue-client/issues/42","labels_url":"https://api.github.com/repos/lightblue-platform/lightblue-client/issues/42/labels{/name}","comments_url":"https://api.github.com/repos/lightblue-platform/lightblue-client/issues/42/comments","events_url":"https://api.github.com/repos/lightblue-platform/lightblue-client/issues/42/events","html_url":"https://github.com/lightblue-platform/lightblue-client/issues/42","id":52678462,"number":42,"title":"Inject an HttpClient instead of new-ing it up internally inside LightblueClient","user":{"login":"alechenninger","id":3496559,"avatar_url":"https://avatars.githubusercontent.com/u/3496559?v=3","gravatar_id":"","url":"https://api.github.com/users/alechenninger","html_url":"https://github.com/alechenninger","followers_url":"https://api.github.com/users/alechenninger/followers","following_url":"https://api.github.com/users/alechenninger/following{/other_user}","gists_url":"https://api.github.com/users/alechenninger/gists{/gist_id}","starred_url":"https://api.github.com/users/alechenninger/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/alechenninger/subscriptions","organizations_url":"https://api.github.com/users/alechenninger/orgs","repos_url":"https://api.github.com/users/alechenninger/repos","events_url":"https://api.github.com/users/alechenninger/events{/privacy}","received_events_url":"https://api.github.com/users/alechenninger/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":1,"created_at":"2014-12-22T19:07:44Z","updated_at":"2015-03-03T18:32:02Z","closed_at":null,"body":"From https://hc.apache.org/httpcomponents-client-ga/tutorial/html/connmgmt.html\r\n\r\n> 2.1. Connection persistence\r\nThe process of establishing a connection from one host to another is quite complex and involves multiple packet exchanges between two endpoints, which can be quite time consuming. The overhead of connection handshaking can be significant, especially for small HTTP messages. One can achieve a much higher data throughput if open connections can be re-used to execute multiple requests.\r\n\r\nSure, it makes it easy for clients to use because they don't have to worry about handling an HttpClient in their code, but this seems lazy to me. There should be a better way for clients to manage the life cycle of an HttpClient in various service and web frameworks that they use, rather than have the service client make a new connection for every service call. Currently the lightblue client gives no such option; it must manage the HttpClient, and gives no interface for a consumer to close it at some point, so it must close it after every request."},"comment":{"url":"https://api.github.com/repos/lightblue-platform/lightblue-client/issues/comments/77004926","html_url":"https://github.com/lightblue-platform/lightblue-client/issues/42#issuecomment-77004926","issue_url":"https://api.github.com/repos/lightblue-platform/lightblue-client/issues/42","id":77004926,"user":{"login":"alechenninger","id":3496559,"avatar_url":"https://avatars.githubusercontent.com/u/3496559?v=3","gravatar_id":"","url":"https://api.github.com/users/alechenninger","html_url":"https://github.com/alechenninger","followers_url":"https://api.github.com/users/alechenninger/followers","following_url":"https://api.github.com/users/alechenninger/following{/other_user}","gists_url":"https://api.github.com/users/alechenninger/gists{/gist_id}","starred_url":"https://api.github.com/users/alechenninger/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/alechenninger/subscriptions","organizations_url":"https://api.github.com/users/alechenninger/orgs","repos_url":"https://api.github.com/users/alechenninger/repos","events_url":"https://api.github.com/users/alechenninger/events{/privacy}","received_events_url":"https://api.github.com/users/alechenninger/received_events","type":"User","site_admin":false},"created_at":"2015-03-03T18:32:02Z","updated_at":"2015-03-03T18:32:02Z","body":"Fixing this is fairly straightforward though not trivial, and in doing so would close #50 as well.\r\n\r\nCurrently, `LightblueClient` news up its own `HttpClient` dependency by way of `HttpClientNoAuth` and `HttpClientAuth` classes, which are really just factories for Apache `HttpClient`s. This has a number of problems, which is in part why those classes are deprecated. The most egregious of these is #42 , in addition to this class being non-unit-testable, because it will always try to make real HTTP requests.\r\n\r\nThe solution is the dependency injection (aka inversion of control) pattern. This means that `LightblueClient` should accept an `HttpClient` in it's constructor, instead of \"controlling\" the implementation of that dependency itself.\r\n\r\nHowever, one step further would be to avoid coupling to Apache's `HttpClient` interface at all, and instead provide our own simple interface. Another option would be to use [Google's http client library][1], which is exactly that: an abstraction around various possible HTTP client implementations, which may be Apache, vanilla java.net, or anything else. Of course, there is some concern around coupling to Google's API's themselves, so it seems as though the best compromise is to roll our own (though there can still be an implementation that uses Google's APIs, which would probably be a good idea to leverage that OSS work that is already done.. I'm reminded of effective java 47: Know and use the libraries).\r\n\r\nWork that would need to be done:\r\n- [ ] Design API for http clients. Will need to be at least three interfaces: Request, Response, and the Client itself. (Names TBD)\r\n- [ ] Provide one or more stock implementations. One of which must not require explicit shutdown to maintain compatibility with the existing client. That is, so the `new LightblueClient()` can still work... it will just choose a default implementation that does not require explicit shutdown (like java.net).\r\n\r\n[1]: https://github.com/google/google-http-java-client"}}