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

io.contek.invoker.commons.rest.RestMethod Maven / Gradle / Ivy

There is a newer version: 3.8.0
Show newest version
package io.contek.invoker.commons.rest;

import okhttp3.Headers;
import okhttp3.Request;
import okhttp3.RequestBody;

import javax.annotation.Nullable;
import javax.annotation.concurrent.Immutable;
import java.util.function.Function;

@Immutable
public enum RestMethod {
  GET(
      body -> {
        if (body != null) {
          throw new IllegalArgumentException();
        }
        return new Request.Builder().get();
      }),
  POST(
      body -> {
        if (body == null) {
          throw new IllegalArgumentException();
        }
        return new Request.Builder().post(body);
      }),
  PUT(
      body -> {
        if (body == null) {
          throw new IllegalArgumentException();
        }
        return new Request.Builder().put(body);
      }),
  DELETE(body -> new Request.Builder().delete(body));

  private final Function builder;

  RestMethod(Function builder) {
    this.builder = builder;
  }

  Request createRequest(String url, Headers headers, @Nullable RequestBody body) {
    return builder.apply(body).url(url).headers(headers).build();
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy