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

com.youcruit.billogram.client.AbstractRestClient Maven / Gradle / Ivy

There is a newer version: 1.0.3
Show newest version
package com.youcruit.billogram.client;

import static com.youcruit.billogram.client.http.HttpClient.Method.DELETE;
import static com.youcruit.billogram.client.http.HttpClient.Method.GET;
import static com.youcruit.billogram.client.http.HttpClient.Method.POST;
import static com.youcruit.billogram.client.http.HttpClient.Method.PUT;

import java.io.IOException;
import java.net.URI;

import com.youcruit.billogram.client.http.HttpClient;
import com.youcruit.billogram.objects.request.FilterField;
import com.youcruit.billogram.objects.request.OrderField;
import com.youcruit.billogram.objects.request.Search;
import com.youcruit.billogram.objects.response.common.Response;
import com.youcruit.billogram.objects.response.common.SearchResponse;

abstract class AbstractRestClient {
    protected final HttpClient httpClient;
    protected final String endpoint;
    protected final Class searchResponseClass;
    protected final Class responseClass;

    public AbstractRestClient(HttpClient httpClient, String endpoint, Class searchResponseClass, Class responseClass) {
	this.httpClient = httpClient;
	this.endpoint = endpoint;
	this.searchResponseClass = searchResponseClass;
	this.responseClass = responseClass;
    }


    protected SR search(Search search) throws IOException {
	final URI uri = httpClient.pathToUri(search, endpoint);
	return httpClient.sync(uri, null, GET, searchResponseClass);
    }

    protected void searchAsync(Search search, BillogramCallback callback) {
	final URI uri = httpClient.pathToUri(search, endpoint);
	httpClient.async(uri, null, GET, callback, searchResponseClass);
    }

    protected R get(String id) throws IOException {
	final URI uri = httpClient.pathToUri(endpoint, id);
	return httpClient.sync(uri, null, GET, responseClass);
    }

    protected void getAsync(String id, BillogramCallback callback) {
	final URI uri = httpClient.pathToUri(endpoint, id);
	httpClient.async(uri, null, GET, callback, responseClass);
    }

    protected R update(Data data) throws IOException {
	final URI uri = httpClient.pathToUri(endpoint, getId(data));
	return httpClient.sync(uri, data, PUT, responseClass);
    }

    protected void updateAsync(Data data, BillogramCallback callback) {
	final URI uri = httpClient.pathToUri(endpoint, getId(data));
	httpClient.async(uri, data, PUT, callback, responseClass);
    }

    protected R create(Data data) throws IOException {
	final URI uri = httpClient.pathToUri(endpoint);
	return httpClient.sync(uri, data, POST, responseClass);
    }

    protected void createAsync(Data data, BillogramCallback callback) {
	final URI uri = httpClient.pathToUri(endpoint);
	httpClient.async(uri, data, POST, callback, responseClass);
    }

    protected void delete(String id) throws IOException {
	final URI uri = httpClient.pathToUri(endpoint, id);
	httpClient.sync(uri, null, DELETE, Void.class);
    }

    protected void deleteAsync(String id, BillogramCallback callback) {
	final URI uri = httpClient.pathToUri(endpoint, id);
	httpClient.async(uri, null, DELETE, callback, Void.class);
    }

    protected abstract String getId(Data data);
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy