use of io.reactivex.rxjava3.core.Maybe in project julian-http-client by ljtfreitas.
the class VertxHTTPClientResponse method valueOf.
static Single<VertxHTTPClientResponse> valueOf(HttpClientResponse response) {
HTTPStatus status = HTTPStatusCode.select(response.statusCode()).map(HTTPStatus::new).orElseGet(() -> new HTTPStatus(response.statusCode(), response.statusMessage()));
HTTPHeaders headers = response.headers().names().stream().map(name -> HTTPHeader.create(name, response.headers().getAll(name))).reduce(HTTPHeaders.empty(), HTTPHeaders::join, (a, b) -> b);
Maybe<byte[]> bodyAsBytes = response.body().map(Buffer::getBytes).toMaybe();
return bodyAsBytes.map(body -> HTTPResponseBody.optional(status, headers, () -> HTTPResponseBody.some(body))).map(body -> new VertxHTTPClientResponse(HTTPClientResponse.create(status, headers, body))).switchIfEmpty(Single.fromCallable(() -> new VertxHTTPClientResponse(HTTPClientResponse.empty(status, headers))));
}
use of io.reactivex.rxjava3.core.Maybe in project amplify-android by aws-amplify.
the class Merger method merge.
/**
* Merge an item back into the local store, using a default strategy.
* TODO: Change this method to return a Maybe, and remove the Consumer argument.
* @param modelWithMetadata A model, combined with metadata about it
* @param changeTypeConsumer A callback invoked when the merge method saves or deletes the model.
* @param <T> Type of model
* @return A completable operation to merge the model
*/
<T extends Model> Completable merge(ModelWithMetadata<T> modelWithMetadata, Consumer<StorageItemChange.Type> changeTypeConsumer) {
AtomicReference<Long> startTime = new AtomicReference<>();
return Completable.defer(() -> {
ModelMetadata metadata = modelWithMetadata.getSyncMetadata();
boolean isDelete = Boolean.TRUE.equals(metadata.isDeleted());
int incomingVersion = metadata.getVersion() == null ? -1 : metadata.getVersion();
T model = modelWithMetadata.getModel();
return versionRepository.findModelVersion(model).onErrorReturnItem(-1).filter(currentVersion -> currentVersion == -1 || incomingVersion > currentVersion).flatMapCompletable(shouldMerge -> {
Completable firstStep;
if (mutationOutbox.hasPendingMutation(model.getId())) {
LOG.info("Mutation outbox has pending mutation for " + model.getId() + ". Saving the metadata, but not model itself.");
firstStep = Completable.complete();
} else {
firstStep = (isDelete ? delete(model, changeTypeConsumer) : save(model, changeTypeConsumer));
}
return firstStep.andThen(save(metadata, NoOpConsumer.create()));
}).doOnComplete(() -> {
announceSuccessfulMerge(modelWithMetadata);
LOG.debug("Remote model update was sync'd down into local storage: " + modelWithMetadata);
}).onErrorComplete(failure -> {
if (!ErrorInspector.contains(failure, SQLiteConstraintException.class)) {
return false;
}
LOG.warn("Sync failed: foreign key constraint violation: " + modelWithMetadata, failure);
return true;
}).doOnError(failure -> LOG.warn("Failed to sync remote model into local storage: " + modelWithMetadata, failure));
}).doOnSubscribe(disposable -> startTime.set(System.currentTimeMillis())).doOnTerminate(() -> {
long duration = System.currentTimeMillis() - startTime.get();
LOG.verbose("Merged a single item in " + duration + " ms.");
});
}
use of io.reactivex.rxjava3.core.Maybe in project caravan-rhyme by wcm-io-caravan.
the class RenderRelatedResourceTest method should_throw_exception_if_server_impls_are_neither_linkable_or_embeddable.
@Test
public void should_throw_exception_if_server_impls_are_neither_linkable_or_embeddable() {
TestResourceWithObservableLinks resourceImpl = new TestResourceWithObservableLinks() {
@Override
public Observable<TestResource> getLinked() {
return Observable.just(new TestResource() {
@Override
public Maybe<TestState> getState() {
return Maybe.just(new TestState("foo"));
}
});
}
};
Throwable ex = catchThrowable(() -> render(resourceImpl));
assertThat(ex).isInstanceOf(HalApiDeveloperException.class).hasMessageStartingWith("Your server side resource implementation classes must implement either EmbeddableResource or LinkableResource.");
}
Aggregations