|
| 1 | +/* |
| 2 | + * Copyright (c) 2017 AsyncHttpClient Project. All rights reserved. |
| 3 | + * |
| 4 | + * This program is licensed to you under the Apache License Version 2.0, |
| 5 | + * and you may not use this file except in compliance with the Apache License Version 2.0. |
| 6 | + * You may obtain a copy of the Apache License Version 2.0 at |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0. |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, |
| 10 | + * software distributed under the Apache License Version 2.0 is distributed on an |
| 11 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | + * See the Apache License Version 2.0 for the specific language governing permissions and limitations there under. |
| 13 | + */ |
| 14 | +package org.asynchttpclient.extras.rxjava2; |
| 15 | + |
| 16 | +import static java.util.Objects.requireNonNull; |
| 17 | + |
| 18 | +import java.util.concurrent.Future; |
| 19 | +import java.util.function.Supplier; |
| 20 | + |
| 21 | +import org.asynchttpclient.AsyncHandler; |
| 22 | +import org.asynchttpclient.AsyncHttpClient; |
| 23 | +import org.asynchttpclient.Request; |
| 24 | +import org.asynchttpclient.extras.rxjava2.maybe.MaybeAsyncHandlerBridge; |
| 25 | +import org.asynchttpclient.extras.rxjava2.maybe.ProgressAsyncMaybeEmitterBridge; |
| 26 | +import org.asynchttpclient.handler.ProgressAsyncHandler; |
| 27 | + |
| 28 | +import io.reactivex.Maybe; |
| 29 | +import io.reactivex.MaybeEmitter; |
| 30 | +import io.reactivex.disposables.Disposables; |
| 31 | + |
| 32 | +/** |
| 33 | + * Straight forward default implementation of the {@code RxHttpClient} interface. |
| 34 | + */ |
| 35 | +public class DefaultRxHttpClient implements RxHttpClient { |
| 36 | + |
| 37 | + private final AsyncHttpClient asyncHttpClient; |
| 38 | + |
| 39 | + /** |
| 40 | + * Returns a new {@code DefaultRxHttpClient} instance that uses the given {@code asyncHttpClient} under the hoods. |
| 41 | + * |
| 42 | + * @param asyncHttpClient |
| 43 | + * the Async HTTP Client instance to be used |
| 44 | + * |
| 45 | + * @throws NullPointerException |
| 46 | + * if {@code asyncHttpClient} is {@code null} |
| 47 | + */ |
| 48 | + public DefaultRxHttpClient(AsyncHttpClient asyncHttpClient) { |
| 49 | + this.asyncHttpClient = requireNonNull(asyncHttpClient); |
| 50 | + } |
| 51 | + |
| 52 | + @Override |
| 53 | + public <T> Maybe<T> prepare(Request request, Supplier<? extends AsyncHandler<T>> handlerSupplier) { |
| 54 | + requireNonNull(request); |
| 55 | + requireNonNull(handlerSupplier); |
| 56 | + |
| 57 | + return Maybe.create(emitter -> { |
| 58 | + final AsyncHandler<?> bridge = createBridge(emitter, handlerSupplier.get()); |
| 59 | + final Future<?> responseFuture = asyncHttpClient.executeRequest(request, bridge); |
| 60 | + emitter.setDisposable(Disposables.fromFuture(responseFuture)); |
| 61 | + }); |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Creates an {@code AsyncHandler} that bridges events from the given {@code handler} to the given {@code emitter} |
| 66 | + * and cancellation/disposal in the other direction. |
| 67 | + * |
| 68 | + * @param <T> |
| 69 | + * the result type produced by {@code handler} and emitted by {@code emitter} |
| 70 | + * |
| 71 | + * @param emitter |
| 72 | + * the RxJava emitter instance that receives results upon completion and will be queried for disposal |
| 73 | + * during event processing |
| 74 | + * @param handler |
| 75 | + * the {@code AsyncHandler} instance that receives downstream events and produces the result that will be |
| 76 | + * emitted upon request completion |
| 77 | + * |
| 78 | + * @return the bridge handler |
| 79 | + */ |
| 80 | + protected <T> AsyncHandler<?> createBridge(MaybeEmitter<T> emitter, AsyncHandler<T> handler) { |
| 81 | + if (handler instanceof ProgressAsyncHandler) { |
| 82 | + return new ProgressAsyncMaybeEmitterBridge<>(emitter, (ProgressAsyncHandler<? extends T>) handler); |
| 83 | + } |
| 84 | + |
| 85 | + return new MaybeAsyncHandlerBridge<>(emitter, handler); |
| 86 | + } |
| 87 | +} |
0 commit comments