-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from saasquatch/dev
initial release
- Loading branch information
Showing
13 changed files
with
770 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
language: java | ||
jdk: | ||
- openjdk8 | ||
- openjdk11 | ||
os: linux | ||
cache: | ||
directories: | ||
- $HOME/.m2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,74 @@ | ||
# apache-client5-reactive | ||
Thin wrapper around Apache HttpAsyncClient 5.x to expose Reactive Streams interfaces. | ||
|
||
[![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0) | ||
[![Build Status](https://travis-ci.org/saasquatch/apache-client5-reactive.svg?branch=master)](https://travis-ci.org/saasquatch/apache-client5-reactive) | ||
[![](https://jitpack.io/v/saasquatch/apache-client5-reactive.svg)](https://jitpack.io/#saasquatch/apache-client5-reactive) | ||
|
||
Thin wrapper around Apache HttpComponents HttpAsyncClient 5.x to expose Reactive Streams interfaces. | ||
|
||
## Sample usage | ||
|
||
For examples, see package [`com.saasquatch.client5reactive.examples`](https://github.com/saasquatch/apache-client5-reactive/tree/master/src/test/java/com/saasquatch/client5reactive/examples). | ||
|
||
## Adding it to your project | ||
|
||
### Add the repository | ||
|
||
Maven | ||
|
||
```xml | ||
<repositories> | ||
<repository> | ||
<id>jitpack.io</id> | ||
<url>https://jitpack.io</url> | ||
</repository> | ||
</repositories> | ||
``` | ||
|
||
Gradle | ||
|
||
```gradle | ||
repositories { | ||
maven { url 'https://jitpack.io' } | ||
} | ||
``` | ||
|
||
### Add the dependency | ||
|
||
Maven | ||
|
||
```xml | ||
<dependency> | ||
<groupId>com.github.saasquatch</groupId> | ||
<artifactId>apache-client5-reactive</artifactId> | ||
<version>0.0.1</version> | ||
</dependency> | ||
``` | ||
|
||
Gradle | ||
|
||
```gradle | ||
compile 'com.github.saasquatch:apache-client5-reactive:0.0.1' | ||
``` | ||
|
||
## License | ||
|
||
Unless explicitly stated otherwise all files in this repository are licensed under the Apache License 2.0. | ||
|
||
License boilerplate: | ||
|
||
``` | ||
Copyright 2020 ReferralSaaSquatch.com Inc. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<groupId>com.saasquatch</groupId> | ||
<artifactId>apache-client5-reactive</artifactId> | ||
<version>0.0.1-SNAPSHOT</version> | ||
<packaging>jar</packaging> | ||
|
||
<name>apache-client5-reactive</name> | ||
<url>https://github.com/saasquatch/apache-client5-reactive</url> | ||
<inceptionYear>2020</inceptionYear> | ||
<licenses> | ||
<license> | ||
<name>Apache License 2.0</name> | ||
<url>http://www.apache.org/licenses/LICENSE-2.0.html</url> | ||
<distribution>repo</distribution> | ||
</license> | ||
</licenses> | ||
|
||
<properties> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
<junit.version>5.6.0</junit.version> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.junit.jupiter</groupId> | ||
<artifactId>junit-jupiter-api</artifactId> | ||
<version>${junit.version}</version> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.junit.jupiter</groupId> | ||
<artifactId>junit-jupiter-engine</artifactId> | ||
<version>${junit.version}</version> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.apache.httpcomponents.client5</groupId> | ||
<artifactId>httpclient5</artifactId> | ||
<version>5.0</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.apache.httpcomponents.core5</groupId> | ||
<artifactId>httpcore5-reactive</artifactId> | ||
<version>5.0</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>io.reactivex.rxjava3</groupId> | ||
<artifactId>rxjava</artifactId> | ||
<version>3.0.0</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.google.code.findbugs</groupId> | ||
<artifactId>jsr305</artifactId> | ||
<version>3.0.2</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.slf4j</groupId> | ||
<artifactId>slf4j-simple</artifactId> | ||
<version>1.7.30</version> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-compiler-plugin</artifactId> | ||
<version>3.8.1</version> | ||
<configuration> | ||
<source>1.8</source> | ||
<target>1.8</target> | ||
</configuration> | ||
</plugin> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-source-plugin</artifactId> | ||
<version>3.2.0</version> | ||
<executions> | ||
<execution> | ||
<id>attach-sources</id> | ||
<goals> | ||
<goal>jar</goal> | ||
</goals> | ||
</execution> | ||
</executions> | ||
</plugin> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-javadoc-plugin</artifactId> | ||
<version>3.1.1</version> | ||
<configuration> | ||
<doclint>none</doclint> | ||
</configuration> | ||
<executions> | ||
<execution> | ||
<id>attach-javadocs</id> | ||
<goals> | ||
<goal>jar</goal> | ||
</goals> | ||
</execution> | ||
</executions> | ||
</plugin> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-surefire-plugin</artifactId> | ||
<version>3.0.0-M4</version> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
|
||
</project> |
45 changes: 45 additions & 0 deletions
45
src/main/java/com/saasquatch/client5reactive/FutureCallbacks.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package com.saasquatch.client5reactive; | ||
|
||
import java.util.concurrent.CancellationException; | ||
import org.apache.hc.core5.concurrent.FutureCallback; | ||
import io.reactivex.rxjava3.core.MaybeEmitter; | ||
|
||
/** | ||
* Utilities for {@link FutureCallback}s. Not public. | ||
* | ||
* @author sli | ||
*/ | ||
final class FutureCallbacks { | ||
|
||
private FutureCallbacks() {} | ||
|
||
public static <T> FutureCallback<T> maybeEmitter(MaybeEmitter<T> emitter) { | ||
return new FutureCallback<T>() { | ||
|
||
@Override | ||
public void completed(T result) { | ||
if (result == null) { | ||
emitter.onComplete(); | ||
} else { | ||
emitter.onSuccess(result); | ||
} | ||
} | ||
|
||
@Override | ||
public void failed(Exception ex) { | ||
emitter.onError(ex); | ||
} | ||
|
||
@Override | ||
public void cancelled() { | ||
emitter.onError(cancelledException()); | ||
} | ||
|
||
}; | ||
} | ||
|
||
private static Exception cancelledException() { | ||
return new CancellationException("Future cancelled"); | ||
} | ||
|
||
} |
133 changes: 133 additions & 0 deletions
133
src/main/java/com/saasquatch/client5reactive/HttpReactiveClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
package com.saasquatch.client5reactive; | ||
|
||
import java.nio.ByteBuffer; | ||
import java.util.concurrent.Future; | ||
import javax.annotation.Nonnull; | ||
import javax.annotation.Nullable; | ||
import org.apache.hc.client5.http.async.HttpAsyncClient; | ||
import org.apache.hc.client5.http.async.methods.SimpleHttpRequest; | ||
import org.apache.hc.client5.http.async.methods.SimpleHttpResponse; | ||
import org.apache.hc.client5.http.async.methods.SimpleRequestProducer; | ||
import org.apache.hc.client5.http.async.methods.SimpleResponseConsumer; | ||
import org.apache.hc.client5.http.impl.async.CloseableHttpAsyncClient; | ||
import org.apache.hc.core5.concurrent.FutureCallback; | ||
import org.apache.hc.core5.http.HttpResponse; | ||
import org.apache.hc.core5.http.Message; | ||
import org.apache.hc.core5.http.nio.AsyncPushConsumer; | ||
import org.apache.hc.core5.http.nio.AsyncRequestProducer; | ||
import org.apache.hc.core5.http.nio.AsyncResponseConsumer; | ||
import org.apache.hc.core5.http.nio.HandlerFactory; | ||
import org.apache.hc.core5.http.protocol.HttpContext; | ||
import org.reactivestreams.Publisher; | ||
|
||
/** | ||
* Thin wrapper around Apache {@link HttpAsyncClient} to expose | ||
* <a href="https://www.reactive-streams.org/">Reactive Streams</a> interfaces.<br> | ||
* The methods in this interface aim to mirror the ones in {@link HttpAsyncClient} and | ||
* {@link CloseableHttpAsyncClient}. | ||
* | ||
* @author sli | ||
* @see HttpReactiveClients | ||
*/ | ||
public interface HttpReactiveClient { | ||
|
||
/** | ||
* Execute the given request. This method is equivalent to | ||
* {@link HttpAsyncClient#execute(AsyncRequestProducer, AsyncResponseConsumer, HandlerFactory, HttpContext , FutureCallback)}. | ||
* If the {@link Future} produced by the equivalent {@link HttpAsyncClient} method completes with | ||
* {@code null}, then the returning {@link Publisher} of this method will complete with no | ||
* element. | ||
*/ | ||
<T> Publisher<T> execute(@Nonnull AsyncRequestProducer requestProducer, | ||
@Nonnull AsyncResponseConsumer<T> responseConsumer, | ||
@Nullable HandlerFactory<AsyncPushConsumer> pushHandlerFactory, | ||
@Nullable HttpContext context); | ||
|
||
/** | ||
* Convenience method for | ||
* {@link #execute(AsyncRequestProducer, AsyncResponseConsumer, HandlerFactory, HttpContext)}, | ||
* equivalent to | ||
* {@link CloseableHttpAsyncClient#execute(AsyncRequestProducer, AsyncResponseConsumer, HttpContext, FutureCallback)} | ||
*/ | ||
default <T> Publisher<T> execute(@Nonnull AsyncRequestProducer requestProducer, | ||
@Nonnull AsyncResponseConsumer<T> responseConsumer, @Nullable HttpContext context) { | ||
return execute(requestProducer, responseConsumer, null, context); | ||
} | ||
|
||
/** | ||
* Convenience method for | ||
* {@link #execute(AsyncRequestProducer, AsyncResponseConsumer, HandlerFactory, HttpContext)}, | ||
* equivalent to | ||
* {@link CloseableHttpAsyncClient#execute(AsyncRequestProducer, AsyncResponseConsumer, FutureCallback)}. | ||
*/ | ||
default <T> Publisher<T> execute(@Nonnull AsyncRequestProducer requestProducer, | ||
@Nonnull AsyncResponseConsumer<T> responseConsumer) { | ||
return execute(requestProducer, responseConsumer, null); | ||
} | ||
|
||
/** | ||
* Execute a simple in-memory request and get a simple in-memory response. This method is | ||
* equivalent to | ||
* {@link CloseableHttpAsyncClient#execute(SimpleHttpRequest, HttpContext, FutureCallback)}. The | ||
* returning {@link Publisher} completes with exactly 1 element. | ||
*/ | ||
default Publisher<SimpleHttpResponse> execute(@Nonnull SimpleHttpRequest request, | ||
@Nullable HttpContext context) { | ||
return execute(SimpleRequestProducer.create(request), SimpleResponseConsumer.create(), context); | ||
} | ||
|
||
/** | ||
* Convenience method for {@link #execute(SimpleHttpRequest, HttpContext)}, equivalent to | ||
* {@link CloseableHttpAsyncClient#execute(SimpleHttpRequest, FutureCallback)}. | ||
*/ | ||
default Publisher<SimpleHttpResponse> execute(@Nonnull SimpleHttpRequest request) { | ||
return execute(request, null); | ||
} | ||
|
||
/** | ||
* Execute the given request and get a streaming response body as a {@link Publisher} of | ||
* {@link ByteBuffer}s. The returning {@link Publisher} completes with exactly 1 element. The | ||
* {@link Publisher} within the returning {@link Publisher} may contain 0 to n elements. | ||
*/ | ||
Publisher<Message<HttpResponse, Publisher<ByteBuffer>>> streamingExecute( | ||
@Nonnull AsyncRequestProducer requestProducer, | ||
@Nullable HandlerFactory<AsyncPushConsumer> pushHandlerFactory, | ||
@Nullable HttpContext context); | ||
|
||
/** | ||
* Convenience method for | ||
* {@link #streamingExecute(AsyncRequestProducer, HandlerFactory, HttpContext)} | ||
*/ | ||
default Publisher<Message<HttpResponse, Publisher<ByteBuffer>>> streamingExecute( | ||
@Nonnull AsyncRequestProducer requestProducer, @Nullable HttpContext context) { | ||
return streamingExecute(requestProducer, null, context); | ||
} | ||
|
||
/** | ||
* Convenience method for | ||
* {@link #streamingExecute(AsyncRequestProducer, HandlerFactory, HttpContext)} | ||
*/ | ||
default Publisher<Message<HttpResponse, Publisher<ByteBuffer>>> streamingExecute( | ||
@Nonnull AsyncRequestProducer requestProducer) { | ||
return streamingExecute(requestProducer, null); | ||
} | ||
|
||
/** | ||
* Execute a simple in-memory request and get a streaming response. Convenience method for | ||
* {@link #streamingExecute(AsyncRequestProducer, HandlerFactory, HttpContext)} | ||
*/ | ||
default Publisher<Message<HttpResponse, Publisher<ByteBuffer>>> streamingExecute( | ||
@Nonnull SimpleHttpRequest request, @Nullable HttpContext context) { | ||
return streamingExecute(SimpleRequestProducer.create(request), context); | ||
} | ||
|
||
/** | ||
* Convenience method for | ||
* {@link #streamingExecute(AsyncRequestProducer, HandlerFactory, HttpContext)} | ||
*/ | ||
default Publisher<Message<HttpResponse, Publisher<ByteBuffer>>> streamingExecute( | ||
@Nonnull SimpleHttpRequest request) { | ||
return streamingExecute(request, null); | ||
} | ||
|
||
} |
Oops, something went wrong.