All Downloads are FREE. Search and download functionalities are using the official Maven repository.

co.privacyone.sdk.restapi.rest.RestApi Maven / Gradle / Ivy

There is a newer version: 2.2.1
Show newest version
/*************************************************************************
 *
 * Privacy1 AB CONFIDENTIAL
 * ________________________
 *
 *  [2017] - [2020] Privacy1 AB
 *  All Rights Reserved.
 *
 * NOTICE:  All information contained herein is, and remains the property
 * of Privacy1 AB.  The intellectual and technical concepts contained herein
 * are proprietary to Privacy1 AB and may be covered by European, U.S. and Foreign
 * Patents, patents in process, and are protected by trade secret or copyright law.
 *
 * Dissemination of this information or reproduction of this material
 * is strictly forbidden.
 */
package co.privacyone.sdk.restapi.rest;

import co.privacyone.sdk.restapi.exception.RemoteServiceException;
import java.io.IOException;
import java.util.Arrays;
import java.util.Optional;

import okhttp3.*;
import okhttp3.Request.Builder;

import javax.annotation.Nullable;

public class RestApi {

  public static int SC_UNAUTHORIZED = 401;

  private final OkHttpClient httpClient;

  public RestApi() {
    httpClient = new OkHttpClient.Builder()
    .connectionSpecs(Arrays.asList(ConnectionSpec.MODERN_TLS, ConnectionSpec.COMPATIBLE_TLS, ConnectionSpec.CLEARTEXT))
    .build();
  }

  public Response head(final HttpUrl url, Optional headers) throws RemoteServiceException {
    final Request.Builder builder = new Builder();
    builder.url(url);
    headers.ifPresent(builder::headers);
    final Request request = builder.head().build();
    try {
      return httpClient.newCall(request).execute();
    } catch (IOException e) {
      throw new RemoteServiceException("http HEAD request throw IOException " + e.getMessage());
    }
  }

  public Response get(final HttpUrl url, Optional headers) throws RemoteServiceException {
    Request.Builder builder = new Builder();
    builder.url(url);
    if (headers.isPresent()) {
      builder.headers(headers.get());
    }
    final Request request = builder.build();
    try {
      return httpClient.newCall(request).execute();
    } catch (IOException e) {
      throw new RemoteServiceException("http GET request throw IOException " + e.getMessage());
    }
  }

  public Response post(
      final HttpUrl url,
      final RequestBody requestBody,
      Optional headers) throws RemoteServiceException {
    Request.Builder builder = new Builder();
    builder.url(url);
    if (headers.isPresent()) {
      builder.headers(headers.get());
    }
    final Request request = builder.post(requestBody).build();
    try {
      return httpClient.newCall(request).execute();
    } catch (IOException e) {
      throw new RemoteServiceException("http POST request throw IOException " + e.getMessage());
    }
  }

  public Response delete(final HttpUrl url, final Optional headers, @Nullable final RequestBody requestBody)
      throws RemoteServiceException {
    Builder builder = new Builder();
    builder.url(url);
    if (headers.isPresent()) {
      builder.headers(headers.get());
    }
    final Request request = builder.delete(requestBody).build();
    try {
      return httpClient.newCall(request).execute();
    } catch (IOException e) {
      throw new RemoteServiceException("http DELETE request throw IOException " + e.getMessage());
    }
  }

  public Response put(
      final HttpUrl url,
      final RequestBody requestBody,
      Optional headers) throws RemoteServiceException {
    Request.Builder builder = new Builder();
    builder.url(url);
    if (headers.isPresent()) {
      builder.headers(headers.get());
    }
    final Request request = builder.put(requestBody).build();
    try {
      return httpClient.newCall(request).execute();
    } catch (IOException e) {
      throw new RemoteServiceException("http PUT request throw IOException " + e.getMessage());
    }
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy