Skip to content

Commit

Permalink
Merge pull request #846 from johanhaleby/master
Browse files Browse the repository at this point in the history
Added overloaded createRequest method that takes an HttpContext instance
  • Loading branch information
benjchristensen committed Feb 11, 2014
2 parents 3d3894f + 9e35276 commit 0d83c94
Showing 1 changed file with 39 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import org.apache.http.nio.client.HttpAsyncClient;
import org.apache.http.nio.client.methods.HttpAsyncMethods;
import org.apache.http.nio.protocol.HttpAsyncRequestProducer;
import org.apache.http.protocol.Http.HttpContext;
import org.apache.http.protocol.BasicHttpContext;

import rx.Observable;
import rx.Observable.OnSubscribeFunc;
Expand Down Expand Up @@ -134,6 +136,42 @@ public static ObservableHttp<ObservableHttpResponse> createGet(String uri, final
* @return
*/
public static ObservableHttp<ObservableHttpResponse> createRequest(final HttpAsyncRequestProducer requestProducer, final HttpAsyncClient client) {
return createRequest(requestProducer, client, new BasicHttpContext());
}

/**
* Execute request using {@link HttpAsyncRequestProducer} to define HTTP Method, URI and payload (if applicable).
* <p>
* If the response is chunked (or flushed progressively such as with <i>text/event-stream</i> <a href="http://www.w3.org/TR/2009/WD-eventsource-20091029/">Server-Sent Events</a>) this will call
* {@link Observer#onNext} multiple times.
* <p>
* Use {@code HttpAsyncMethods.create* } factory methods to create {@link HttpAsyncRequestProducer} instances.
* <p>
* A client can be retrieved like this:
* <p>
* <pre> {@code CloseableHttpAsyncClient httpclient = HttpAsyncClients.createDefault(); } </pre>
* <p>
* A client with custom configurations can be created like this:
* </p>
* <pre> {@code
* final RequestConfig requestConfig = RequestConfig.custom()
* .setSocketTimeout(3000)
* .setConnectTimeout(3000).build();
* final CloseableHttpAsyncClient httpclient = HttpAsyncClients.custom()
* .setDefaultRequestConfig(requestConfig)
* .setMaxConnPerRoute(20)
* .setMaxConnTotal(50)
* .build();
* httpclient.start();
* }</pre>
*
*
* @param requestProducer
* @param client
* @param context The HttpContext
* @return
*/
public static ObservableHttp<ObservableHttpResponse> createRequest(final HttpAsyncRequestProducer requestProducer, final HttpAsyncClient client, final HttpContext context) {

return ObservableHttp.create(new OnSubscribeFunc<ObservableHttpResponse>() {

Expand All @@ -144,7 +182,7 @@ public Subscription onSubscribe(final Observer<? super ObservableHttpResponse> o

// return a Subscription that wraps the Future so it can be cancelled
parentSubscription.add(Subscriptions.from(client.execute(requestProducer, new ResponseConsumerDelegate(observer, parentSubscription),
new FutureCallback<HttpResponse>() {
context, new FutureCallback<HttpResponse>() {

@Override
public void completed(HttpResponse result) {
Expand Down

0 comments on commit 0d83c94

Please sign in to comment.