Skip to content

Commit

Permalink
Merge branch 'release/1.0.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
maesenka committed May 8, 2019
2 parents 491654a + 84dd8f5 commit eb7071f
Show file tree
Hide file tree
Showing 26 changed files with 915 additions and 375 deletions.
24 changes: 23 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,31 @@
[![Build Status](https://travis-ci.org/WegenenVerkeer/RxHttpClient.png?branch=develop)](https://travis-ci.org/WegenenVerkeer/RxHttpClient)


This HTTP Client wraps the excellent [AsyncHttpClient](https://github.com/AsyncHttpClient/async-http-client) so that
This HTTP Client wraps the excellent [AsyncHttpClient](https://github.com/AsyncHttpClient/async-http-client) (AHC) so that
Observables are returned, and a number of best practices in RESTful integration are enforced.

# Upgrade to AHC 2

This version of RxHttpClient uses AHC 2.8.x. This implies a number of minor API changes w.r.t to the 0.x versions.

API Changes:

- The methods in ObservableBodyGenerators no longer declare that the throw `Exception`s
- `ServerResponse#getResponseBody(String)` replaced by `ServerResponse#getResponseBody(String)`

The following methods have been removed:

- `RxHttpClient.Builder#setExecutorService()`. Replaced by `RxHttpClient.Builder#setThreadFactory()`
- `RxHttpClient.Builder#setHostnameVerifier()`
- `RxHttpClient.Builder#setUseRelativeURIsWithConnectProxies()`


The following methods have been deprecated:

- `ClientRequest#getContentLength()`
- `RxHttpClient.Builder#setAllowPoolingConnections(boolean)`: use `setKeepAlive()`
- `RxHttpClient.Builder#setAcceptAnyCertificate(boolean)`: use `RxHttpClient.Builder#setUseInsecureTrustManager(boolean)`
- `RxHttpClient.Builder setDisableUrlEncodingForBoundedRequests(boolean)`: use ` RxHttpClient.Builder#setDisableUrlEncodingForBoundRequests(boolean)`

# User Guide

Expand Down
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ val scalaModuleName = Name + "-scala"
lazy val scalaModule =(project in file("modules/scala")). settings(
name := scalaModuleName,
buildSettings(scalaModuleName),
crossScalaVersions := Seq("2.11.8", "2.12.1"),
//crossScalaVersions := Seq("2.12.8"),
libraryDependencies ++= scalaDependencies
) dependsOn javaModule

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package be.wegenenverkeer.rxhttp;

import com.ning.http.client.AsyncCompletionHandler;
import com.ning.http.client.Response;
import org.asynchttpclient.AsyncCompletionHandler;
import org.asynchttpclient.Response;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import rx.exceptions.OnErrorFailedException;
Expand Down Expand Up @@ -38,7 +38,7 @@ class AsyncCompletionHandlerWrapper<F> extends AsyncCompletionHandler<F> {
}

@Override
public F onCompleted(Response response) throws Exception {
public F onCompleted(Response response) {
try {
withCompleteResponse(
response,
Expand All @@ -47,8 +47,8 @@ public F onCompleted(Response response) throws Exception {
if (value != null) subject.onNext(value);
subject.onCompleted();
},
(ex) -> subject.onError(ex),
(ex) -> subject.onError(ex)
subject::onError,
subject::onError
);
} catch (Throwable t) {
//TODO Should this logging not be done in the global onError handler? See Class RxJavaErrorHandler
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
package be.wegenenverkeer.rxhttp;

import com.ning.http.client.AsyncHandler;
import com.ning.http.client.HttpResponseBodyPart;
import com.ning.http.client.HttpResponseHeaders;
import com.ning.http.client.HttpResponseStatus;
import io.netty.handler.codec.http.HttpHeaders;
import org.asynchttpclient.AsyncHandler;
import org.asynchttpclient.HttpResponseBodyPart;
import org.asynchttpclient.HttpResponseStatus;
import rx.subjects.BehaviorSubject;

import java.util.Optional;

/**
* A {@link AsyncHandler} that pushes received items to a specified {@link BehaviorSubject}
*
* A {@link AsyncHandler} that pushes received items to a specified {@link BehaviorSubject}
* <p>
* Created by Karel Maesen, Geovise BVBA on 18/12/14.
*/
class AsyncHandlerWrapper implements AsyncHandler<Boolean> {
Expand All @@ -20,7 +20,7 @@ class AsyncHandlerWrapper implements AsyncHandler<Boolean> {
/**
* Constructs an instance.
*
* @param subject
* @param subject BehaviorSubject wrapped by this instance
*/
AsyncHandlerWrapper(BehaviorSubject<ServerResponseElement> subject) {
this.subject = subject;
Expand All @@ -41,20 +41,19 @@ public void onThrowable(Throwable t) {
* Invoked as soon as some response body part are received. Could be invoked many times.
*
* @param bodyPart response's body part.
* @return a {@link com.ning.http.client.AsyncHandler.STATE} telling to CONTINUE or ABORT the current processing.
* @throws Exception if something wrong happens
* @return a {@link org.asynchttpclient.AsyncHandler.State} telling to CONTINUE or ABORT the current processing.
*/
@Override
public STATE onBodyPartReceived(HttpResponseBodyPart bodyPart) throws Exception {
public State onBodyPartReceived(HttpResponseBodyPart bodyPart) {
if (!subject.hasObservers()) {
bodyPart.markUnderlyingConnectionAsToBeClosed();
//bodyPart.markUnderlyingConnectionAsToBeClosed();
onCompleted(); //send the uncompleted message
return STATE.ABORT;
return State.ABORT;
}
subject.onNext(
(ServerResponseBodyPart)( bodyPart::getBodyPartBytes )
);
return STATE.CONTINUE;
(ServerResponseBodyPart) (bodyPart::getBodyPartBytes)
);
return State.CONTINUE;
}

//we don't check for hasObservers in the onStatusReceived() and onHeadersReceived(). This guarantees that
Expand All @@ -65,11 +64,10 @@ public STATE onBodyPartReceived(HttpResponseBodyPart bodyPart) throws Exception
* Invoked as soon as the HTTP status line has been received
*
* @param responseStatus the status code and test of the response
* @return a {@link com.ning.http.client.AsyncHandler.STATE} telling to CONTINUE or ABORT the current processing.
* @throws Exception if something wrong happens
* @return a {@link org.asynchttpclient.AsyncHandler.State} telling to CONTINUE or ABORT the current processing.
*/
@Override
public STATE onStatusReceived(HttpResponseStatus responseStatus) throws Exception {
public State onStatusReceived(HttpResponseStatus responseStatus) {
final int statuscode = responseStatus.getStatusCode();

if (statuscode >= 400 && statuscode < 500) {
Expand All @@ -89,21 +87,20 @@ public Optional<String> getStatusText() {
return Optional.ofNullable(responseStatus.getStatusText());
}
});
return STATE.CONTINUE;
return State.CONTINUE;
}

/**
* Invoked as soon as the HTTP headers has been received. Can potentially be invoked more than once if a broken server
* sent trailing headers.
*
* @param headers the HTTP headers.
* @return a {@link com.ning.http.client.AsyncHandler.STATE} telling to CONTINUE or ABORT the current processing.
* @throws Exception if something wrong happens
* @return a {@link org.asynchttpclient.AsyncHandler.State} telling to CONTINUE or ABORT the current processing.
*/
@Override
public STATE onHeadersReceived(HttpResponseHeaders headers) throws Exception {
public State onHeadersReceived(HttpHeaders headers) {
subject.onNext(new ServerResponseHeadersBase(headers));
return STATE.CONTINUE;
return State.CONTINUE;
}

/**
Expand All @@ -113,10 +110,9 @@ public STATE onHeadersReceived(HttpResponseHeaders headers) throws Exception {
* Gets always invoked as last callback method.
*
* @return T Value that will be returned by the associated {@link java.util.concurrent.Future}
* @throws Exception if something wrong happens
*/
@Override
public Boolean onCompleted() throws Exception {
public Boolean onCompleted() {
subject.onCompleted();
return true;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package be.wegenenverkeer.rxhttp;

import com.ning.http.client.Param;
import com.ning.http.client.Request;
import io.netty.handler.codec.http.HttpHeaders;
import org.asynchttpclient.Param;
import org.asynchttpclient.Request;

import java.io.File;
import java.io.InputStream;
Expand All @@ -10,9 +11,11 @@
import java.util.List;
import java.util.Map;

import static io.netty.handler.codec.http.HttpHeaderNames.CONTENT_LENGTH;

/**
* Wraps a {@link Request} into a more limited interface.
*
* <p>
* Created by Karel Maesen, Geovise BVBA on 06/12/14.
*/
public class ClientRequest {
Expand All @@ -27,21 +30,25 @@ public String getMethod() {
return request.getMethod();
}

//@deprecated "No longer supported
@Deprecated
public long getContentLength() {
return request.getContentLength();
Integer length = request.getHeaders().getInt(CONTENT_LENGTH);
return length == null ? -1 : length;
}

public Map<String, List<String>> getHeaders() {
return request.getHeaders();
HttpHeaders headers = request.getHeaders();
return CompatUtilities.headersToMap(headers);
}

public Map<String, List<String>> getQueryParams(){
public Map<String, List<String>> getQueryParams() {
List<Param> queryParams = request.getQueryParams();
Map<String, List<String>> result = new HashMap<>();
for (Param p : queryParams) {
String name = p.getName();
String val = p.getValue();
if(result.get(name) == null) {
if (result.get(name) == null) {
List<String> vals = new ArrayList<>();
vals.add(val);
result.put(name, vals);
Expand Down Expand Up @@ -69,43 +76,10 @@ public Boolean getFollowRedirect() {
return request.getFollowRedirect();
}

// public Realm getRealm() {
// return request.getRealm();
// }
//
// public ConnectionPoolPartitioning getConnectionPoolPartitioning() {
// return request.getConnectionPoolPartitioning();
// }
//
// public InetAddress getInetAddress() {
// return request.getInetAddress();
// }
//
// public List<Param> getFormParams() {
// return request.getFormParams();
// }
//
// public List<Param> getQueryParams() {
// return request.getQueryParams();
// }
//
// public BodyGenerator getBodyGenerator() {
// return request.getBodyGenerator();
// }
//
// public ProxyServer getProxyServer() {
// return request.getProxyServer();
// }

public long getRangeOffset() {
return request.getRangeOffset();
}

// public Uri getUri() {
// return request.getUri();
// }


public byte[] getByteData() {
return request.getByteData();
}
Expand All @@ -118,10 +92,6 @@ public InputStream getStreamData() {
return request.getStreamData();
}

// public InetAddress getLocalAddress() {
// return request.getLocalAddress();
// }

public String getUrl() {
return request.getUrl();
}
Expand All @@ -130,12 +100,9 @@ public List<byte[]> getCompositeByteData() {
return request.getCompositeByteData();
}

// public List<Part> getParts() {
// return request.getParts();
// }

//TODO -- method should return charset
public String getBodyEncoding() {
return request.getBodyEncoding();
return request.getCharset().name();
}

Request unwrap() {
Expand Down
Loading

0 comments on commit eb7071f

Please sign in to comment.