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

org.infinispan.client.rest.impl.okhttp.RestCounterClientOkHttp Maven / Gradle / Ivy

There is a newer version: 14.0.33.Final
Show newest version
package org.infinispan.client.rest.impl.okhttp;

import static org.infinispan.client.rest.impl.okhttp.RestClientOkHttp.EMPTY_BODY;
import static org.infinispan.client.rest.impl.okhttp.RestClientOkHttp.sanitize;

import java.util.concurrent.CompletionStage;

import org.infinispan.client.rest.RestCounterClient;
import org.infinispan.client.rest.RestEntity;
import org.infinispan.client.rest.RestResponse;

import okhttp3.Request;

/**
 * @author Tristan Tarrant <[email protected]>
 * @since 10.0
 **/
public class RestCounterClientOkHttp implements RestCounterClient {
   private final RestClientOkHttp client;
   private final String name;
   private final String counterUrl;

   RestCounterClientOkHttp(RestClientOkHttp client, String name) {
      this.client = client;
      this.name = name;
      this.counterUrl = String.format("%s%s/v2/counters/%s", client.getBaseURL(), client.getConfiguration().contextPath(), sanitize(name)).replaceAll("//", "/");
   }

   @Override
   public CompletionStage create(RestEntity configuration) {
      Request.Builder builder = new Request.Builder();
      builder.url(counterUrl).post(((RestEntityAdaptorOkHttp) configuration).toRequestBody());
      return client.execute(builder);
   }

   @Override
   public CompletionStage delete() {
      Request.Builder builder = new Request.Builder();
      builder.url(counterUrl).delete();
      return client.execute(builder);
   }

   @Override
   public CompletionStage configuration() {
      return configuration(null);
   }

   @Override
   public CompletionStage configuration(String mediaType) {
      Request.Builder builder = new Request.Builder();
      builder.url(counterUrl + "/config");
      if (mediaType != null) {
         builder.header("Accept", mediaType);
      }
      return client.execute(builder);
   }

   @Override
   public CompletionStage get() {
      Request.Builder builder = new Request.Builder();
      builder.url(counterUrl);
      return client.execute(builder);
   }

   @Override
   public CompletionStage add(long delta) {
      Request.Builder builder = new Request.Builder();
      builder.url(counterUrl + "?action=add&delta=" + delta).post(EMPTY_BODY);
      return client.execute(builder);
   }

   @Override
   public CompletionStage increment() {
      Request.Builder builder = new Request.Builder();
      builder.url(counterUrl + "?action=increment").post(EMPTY_BODY);
      return client.execute(builder);
   }

   @Override
   public CompletionStage decrement() {
      Request.Builder builder = new Request.Builder();
      builder.url(counterUrl + "?action=decrement").post(EMPTY_BODY);
      return client.execute(builder);
   }

   @Override
   public CompletionStage reset() {
      Request.Builder builder = new Request.Builder();
      builder.url(counterUrl + "?action=reset").post(EMPTY_BODY);
      return client.execute(builder);
   }

   @Override
   public CompletionStage compareAndSet(long expect, long value) {
      Request.Builder builder = new Request.Builder();
      builder.post(EMPTY_BODY).url(counterUrl + "?action=compareAndSet&expect=" + expect + "&update=" + value);
      return client.execute(builder);
   }

   @Override
   public CompletionStage compareAndSwap(long expect, long value) {
      Request.Builder builder = new Request.Builder();
      builder.post(EMPTY_BODY).url(counterUrl + "?action=compareAndSwap&expect=" + expect + "&update=" + value);
      return client.execute(builder);
   }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy