Skip to content

Commit

Permalink
Merge pull request #1 from saasquatch/dev
Browse files Browse the repository at this point in the history
initial release
  • Loading branch information
slisaasquatch authored Mar 6, 2020
2 parents d2d66cd + 18e5a6e commit 6204550
Show file tree
Hide file tree
Showing 13 changed files with 770 additions and 1 deletion.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,9 @@

# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*

.project
.classpath
.settings
/target/
bin
8 changes: 8 additions & 0 deletions .travis.yml
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
74 changes: 73 additions & 1 deletion README.md
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.
```
114 changes: 114 additions & 0 deletions pom.xml
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 src/main/java/com/saasquatch/client5reactive/FutureCallbacks.java
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 src/main/java/com/saasquatch/client5reactive/HttpReactiveClient.java
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);
}

}
Loading

0 comments on commit 6204550

Please sign in to comment.