com.speedment.common.rest.Rest Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of tool-deploy Show documentation
Show all versions of tool-deploy Show documentation
A Speedment bundle that shades all dependencies into one jar. This is
useful when deploying an application on a server.
The newest version!
/*
*
* Copyright (c) 2006-2019, Speedment, Inc. All Rights Reserved.
*
* 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.
*/
package com.speedment.common.rest;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Iterator;
import java.util.concurrent.CompletableFuture;
/**
* This is the main interface of the REST system.
*
* Usage:
*
* Rest rest = Rest.connect("127.0.0.1", 8080);
*
* CompletableFuture<Response> future = rest.post("user",
* Param.of("firstname", "Adam"),
* Param.of("lastname", "Adamsson")
* );
*
* future.get();
*
*
* @author Emil Forslund
* @since 1.0.0
*/
public interface Rest {
enum Method {
POST, GET, DELETE, PUT, OPTIONS, HEAD
}
enum Protocol {
HTTP, HTTPS
}
CompletableFuture get(String path, Option... option);
CompletableFuture post(String path, Option... option);
CompletableFuture delete(String path, Option... option);
CompletableFuture put(String path, Option... option);
CompletableFuture options(String path, Option... option);
CompletableFuture head(String path, Option... option);
CompletableFuture get(String path, InputStream body, Option... option);
CompletableFuture post(String path, InputStream body, Option... option);
CompletableFuture delete(String path, InputStream body, Option... option);
CompletableFuture put(String path, InputStream body, Option... option);
CompletableFuture options(String path, InputStream body, Option... option);
CompletableFuture head(String path, InputStream body, Option... option);
CompletableFuture get(String path, Iterator uploader, Option... option);
CompletableFuture post(String path, Iterator uploader, Option... option);
CompletableFuture delete(String path, Iterator uploader, Option... option);
CompletableFuture put(String path, Iterator uploader, Option... option);
CompletableFuture options(String path, Iterator uploader, Option... option);
CompletableFuture head(String path, Iterator uploader, Option... option);
CompletableFuture get(String path, String data, Option... option);
CompletableFuture post(String path, String data, Option... option);
CompletableFuture delete(String path, String data, Option... option);
CompletableFuture put(String path, String data, Option... option);
CompletableFuture options(String path, String data, Option... option);
CompletableFuture head(String path, String data, Option... option);
static Rest connect(String host) {
return new RestImpl(Protocol.HTTP, host, 0, null, null);
}
static Rest connect(String host, int port) {
return new RestImpl(Protocol.HTTP, host, port, null, null);
}
static Rest connect(String host, String username, String password) {
return new RestImpl(Protocol.HTTP, host, 0, username, password);
}
static Rest connect(String host, int port, String username, String password) {
return new RestImpl(Protocol.HTTP, host, port, username, password);
}
static Rest connectHttps(String host) {
return new RestImpl(Protocol.HTTPS, host, 0, null, null);
}
static Rest connectHttps(String host, int port) {
return new RestImpl(Protocol.HTTPS, host, port, null, null);
}
static Rest connectHttps(String host, String username, String password) {
return new RestImpl(Protocol.HTTPS, host, 0, username, password);
}
static Rest connectHttps(String host, int port, String username, String password) {
return new RestImpl(Protocol.HTTPS, host, port, username, password);
}
static String encode(String value) {
try {
return URLEncoder.encode(value, "UTF-8");
} catch (final UnsupportedEncodingException ex) {
throw new RuntimeException("Error encoding value '" + value + "'.");
}
}
}