protected final <T> Promise<T> callAndParse(final ResponsePromise responsePromise, final ResponseHandler<T> responseHandler) { final Function<Response, ? extends T> transformFunction = toFunction(responseHandler); return new DelegatingPromise<T>(responsePromise.<T>transform() .ok(transformFunction) .created(transformFunction) .others(AbstractAsynchronousRestClient.<T>errorFunction()) .toPromise()); }
public <T> Promise<T> transform(ResponseTransformation<T> transformation) { return delegate.fold(transformation.getFailFunction(), transformation.getSuccessFunctions()); } }
@Override public Promise<Either<ClientError, List<Room>>> list() { return httpClient.newRequest("/rooms/list").get().<Either<ClientError, List<Room>>>transform() .ok(parseJson("rooms", new TypeReference<List<Room>>(){})) .others(ClientUtils.<List<Room>>error()) .fail(ClientUtils.<List<Room>>throwable()) .toPromise(); }
@Override public Promise<Either<ClientError, List<User>>> list() { return httpClient.newRequest("/users/list").get().<Either<ClientError, List<User>>>transform() .ok(parseJson("users", new TypeReference<List<User>>(){})) .others(ClientUtils.<List<User>>error()) .fail(ClientUtils.<List<User>>throwable()) .toPromise(); } }
@Override public Promise<Either<ClientError, Room>> create(String name, long ownerId, Option<Boolean> privacy, Option<String> topic, Option<Boolean> enableGuestAccess) { // TODO validate params final Map<String, String> params = createParametersAsMap(name, ownerId, privacy, topic, enableGuestAccess); return httpClient.newRequest("/rooms/create", "application/x-www-form-urlencoded", urlEncode(params)) .post().<Either<ClientError, Room>>transform() .ok(parseJson("room", Room.class)) .others(ClientUtils.<Room>error()) .fail(ClientUtils.<Room>throwable()) .toPromise(); }
protected final Promise<Void> call(final ResponsePromise responsePromise) { return new DelegatingPromise<Void>(responsePromise.<Void>transform() .ok(constant((Void) null)) .created(constant((Void) null)) .noContent(constant((Void) null)) .others(AbstractAsynchronousRestClient.<Void>errorFunction()) .toPromise()); }
@Override public Promise<Either<ClientError, Status>> message(String roomIdOrName, String from, String message, Option<Message.Format> format, Option<Message.BackgroundColor> color, Option<Boolean> notify) { // TODO: validate params final Map<String, String> params = messageParametersAsMap(roomIdOrName, from, message, format, color, notify); return httpClient.newRequest("/rooms/message", "application/x-www-form-urlencoded", urlEncode(params)) .post().<Either<ClientError, Status>>transform() .ok(constantRight(Status.SENT)) .others(ClientUtils.<Status>error()) .fail(ClientUtils.<Status>throwable()) .toPromise(); }
@Override public Promise<Either<ClientError, Boolean>> canAuthenticate(Option<String> token) { final String uri = token.fold( Suppliers.ofInstance("/"), new Function<String, String>() { @Override public String apply(String t) { return "/?auth_token=" + t; } } ); return httpClient.newRequest(uri).get().<Either<ClientError, Boolean>>transform() .ok(constantRight(Boolean.TRUE)) .unauthorized(constantRight(Boolean.FALSE)) .others(ClientUtils.<Boolean>error()) .fail(ClientUtils.<Boolean>throwable()) .toPromise(); }