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

com.sigopt.net.APIMethodCaller Maven / Gradle / Ivy

There is a newer version: 6.3.0
Show newest version
package com.sigopt.net;

import com.sigopt.exception.APIException;
import com.sigopt.model.APIObject;
import com.sigopt.model.APIResource;

import java.lang.reflect.Type;
import java.util.Map;

public class APIMethodCaller {
    APIMethod apiMethod;
    APIMethod.Builder apiMethodBuilder = new APIMethod.Builder();
    Class klass;
    Type typeOfT;

    public APIMethodCaller(String method, String path) {
        this(method, path, null);
    }
    public APIMethodCaller(String method, String path, Class klass) {
        this.apiMethodBuilder.method(method).path(path);
        this.klass = klass;
    }

    public T call() throws APIException {
        this.apiMethod = apiMethodBuilder.build();
        apiMethod.execute();
        return this.processBody(apiMethod.response.body);

    }

    protected T processBody(String body) {
        return APIResource.constructFromJson(apiMethod.response.body, this.klass);
    }

    public APIMethod getApiMethod() {
        return apiMethod;
    }

    public APIMethodCaller data(APIObject data) {
        return this.data(data.toJson());
    }

    public APIMethodCaller data(String data) {
        this.apiMethodBuilder.data(data);
        return this;
    }

    public APIMethodCaller params(Map params) {
        this.apiMethodBuilder.params(params);
        return this;
    }

    public APIMethodCaller addParam(String key, Object value) {
        this.apiMethodBuilder.addParam(key, value);
        return this;
    }

    public APIMethodCaller addPathComponent(String key, String value) {
        this.apiMethodBuilder.addPathComponent(key, value);
        return this;
    }

    public APIMethodCaller headers(Map headers) {
        this.apiMethodBuilder.headers(headers);
        return this;
    }

    public APIMethodCaller addHeader(String key, String value) {
        this.apiMethodBuilder.addHeader(key, value);
        return this;
    }

    public APIMethodCaller clientToken(String clientToken) {
        this.apiMethodBuilder.clientToken(clientToken);
        return this;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy