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

com.tinypass.client.publisher.api.PublisherWebhookApi Maven / Gradle / Ivy

There is a newer version: 16.331.0
Show newest version
package com.tinypass.client.publisher.api;

import com.tinypass.client.common.*;
import com.tinypass.client.publisher.model.WebhookEvent;
import com.tinypass.client.publisher.model.WebhookSettings;
import com.tinypass.client.publisher.model.WebhookStatus;

import java.util.*;
import java.math.*;
import java.io.*;

public class PublisherWebhookApi {

	private String basePath = "https://api.piano.io/api/v3";
	private String token;
	private ApiInvoker apiInvoker = null;
	private Map headerParams = new LinkedHashMap();

	public PublisherWebhookApi(String basePath, String token) {
		this.basePath = basePath;
		this.token = token;
	}

	public PublisherWebhookApi(String basePath, String token, ApiInvoker invoker, Map headerParams) {
		this(basePath, token);
		this.apiInvoker = invoker;
		this.headerParams = headerParams;
	}

	public ApiInvoker getInvoker() {
        if(apiInvoker == null) apiInvoker = ApiInvoker.getInstance();
		return apiInvoker;
	}

	public void setApiToken(String token){
		this.token = token;
	}

	public void setBasePath(String basePath) {
		this.basePath = basePath;
	}

	public String getBasePath() {
		return basePath;
	}

	public String toQueryParam(Object arg) {
        if(arg == null)
            return null;
		if (arg instanceof Date) {
			Date date = (Date) arg;
			long epochTime = date.getTime() / 1000;
			return String.valueOf(epochTime);
		} else {
			return String.valueOf(arg);
		}

	}

    public void setQueryParam(Map> params, String paramName, Object value) {
        List currentValues = params.computeIfAbsent(paramName, k -> new ArrayList<>());
        if (value instanceof List) {
            List values = (List) value;
            values.forEach(o -> addQueryParam(currentValues, o));
        } else {
            addQueryParam(currentValues, value);
        }
    }

    private void addQueryParam(List currentValues, Object value) {
        String v = toQueryParam(value);
        if (v != null)
            currentValues.add(v);
    }


    /**
      * API Request for events
      */
	public class eventsRequest implements ApiRequest {

        Map headerParams;
        Map> queryParams = new HashMap<>();
        Map> formParams = new HashMap<>();
        Object body = null;

        public eventsRequest(Map headerParams) {
            this.headerParams = headerParams;
        }

        public Map> getPreparedFormParams() {
            return Collections.unmodifiableMap(formParams);
        }

		public eventsRequest aid(String aid) {
            setQueryParam(this.queryParams, "aid", aid);
			return this;
		}
		public eventsRequest skipDeprecatedWebhooks(Boolean skipDeprecatedWebhooks) {
            setQueryParam(this.queryParams, "skip_deprecated_webhooks", skipDeprecatedWebhooks);
			return this;
		}






		public PageList  execute() throws ApiException{

			String path = "/publisher/webhook/events".replaceAll("\\{format\\}","json");

			return getInvoker().invokeAPI(basePath, path, "GET", token,
                                                     queryParams, null,
                                                     headerParams,
                                                     formParams,
                                                     "array",
                                                     String.class);
		}

	}


    /**
      * Helper method to create a new request.
      */
	public eventsRequest eventsRequest(){
		return new eventsRequest(headerParams);
	}



    /**
      * API Request for getEvent
      */
	public class getEventRequest implements ApiRequest {

        Map headerParams;
        Map> queryParams = new HashMap<>();
        Map> formParams = new HashMap<>();
        Object body = null;

        public getEventRequest(Map headerParams) {
            this.headerParams = headerParams;
        }

        public Map> getPreparedFormParams() {
            return Collections.unmodifiableMap(formParams);
        }

		public getEventRequest webhookId(String webhookId) {
            setQueryParam(this.queryParams, "webhook_id", webhookId);
			return this;
		}






		public WebhookEvent  execute() throws ApiException{

			String path = "/publisher/webhook/get".replaceAll("\\{format\\}","json");

			return getInvoker().invokeAPI(basePath, path, "GET", token,
                                                     queryParams, null,
                                                     headerParams,
                                                     formParams,
                                                     "",
                                                     WebhookEvent.class);
		}

	}


    /**
      * Helper method to create a new request.
      */
	public getEventRequest getEventRequest(){
		return new getEventRequest(headerParams);
	}



    /**
      * API Request for getSettings
      */
	public class getSettingsRequest implements ApiRequest {

        Map headerParams;
        Map> queryParams = new HashMap<>();
        Map> formParams = new HashMap<>();
        Object body = null;

        public getSettingsRequest(Map headerParams) {
            this.headerParams = headerParams;
        }

        public Map> getPreparedFormParams() {
            return Collections.unmodifiableMap(formParams);
        }

		public getSettingsRequest aid(String aid) {
            setQueryParam(this.queryParams, "aid", aid);
			return this;
		}






		public WebhookSettings  execute() throws ApiException{

			String path = "/publisher/webhook/settings".replaceAll("\\{format\\}","json");

			return getInvoker().invokeAPI(basePath, path, "GET", token,
                                                     queryParams, null,
                                                     headerParams,
                                                     formParams,
                                                     "",
                                                     WebhookSettings.class);
		}

	}


    /**
      * Helper method to create a new request.
      */
	public getSettingsRequest getSettingsRequest(){
		return new getSettingsRequest(headerParams);
	}



    /**
      * API Request for list
      */
	public class listRequest implements ApiRequest {

        Map headerParams;
        Map> queryParams = new HashMap<>();
        Map> formParams = new HashMap<>();
        Object body = null;

        public listRequest(Map headerParams) {
            this.headerParams = headerParams;
        }

        public Map> getPreparedFormParams() {
            return Collections.unmodifiableMap(formParams);
        }

		public listRequest aid(String aid) {
            setQueryParam(this.queryParams, "aid", aid);
			return this;
		}
		public listRequest status(String status) {
            setQueryParam(this.queryParams, "status", status);
			return this;
		}
		public listRequest keyword(String keyword) {
            setQueryParam(this.queryParams, "keyword", keyword);
			return this;
		}
		public listRequest limit(Integer limit) {
            setQueryParam(this.queryParams, "limit", limit);
			return this;
		}
		public listRequest offset(Integer offset) {
            setQueryParam(this.queryParams, "offset", offset);
			return this;
		}
		public listRequest orderBy(String orderBy) {
            setQueryParam(this.queryParams, "order_by", orderBy);
			return this;
		}
		public listRequest orderDirection(String orderDirection) {
            setQueryParam(this.queryParams, "order_direction", orderDirection);
			return this;
		}
		public listRequest eventType(List eventType) {
            setQueryParam(this.queryParams, "event_type", eventType);
			return this;
		}
		public listRequest type(List type) {
            setQueryParam(this.queryParams, "type", type);
			return this;
		}






		public PageList  execute() throws ApiException{

			String path = "/publisher/webhook/list".replaceAll("\\{format\\}","json");

			return getInvoker().invokeAPI(basePath, path, "GET", token,
                                                     queryParams, null,
                                                     headerParams,
                                                     formParams,
                                                     "array",
                                                     WebhookEvent.class);
		}

	}


    /**
      * Helper method to create a new request.
      */
	public listRequest listRequest(){
		return new listRequest(headerParams);
	}



    /**
      * API Request for skip
      */
	public class skipRequest implements ApiRequest {

        Map headerParams;
        Map> queryParams = new HashMap<>();
        Map> formParams = new HashMap<>();
        Object body = null;

        public skipRequest(Map headerParams) {
            this.headerParams = headerParams;
        }

        public Map> getPreparedFormParams() {
            return Collections.unmodifiableMap(formParams);
        }

		public skipRequest webhookId(String webhookId) {
            setQueryParam(this.queryParams, "webhook_id", webhookId);
			return this;
		}






		public WebhookEvent  execute() throws ApiException{

			String path = "/publisher/webhook/skip".replaceAll("\\{format\\}","json");

			return getInvoker().invokeAPI(basePath, path, "GET", token,
                                                     queryParams, null,
                                                     headerParams,
                                                     formParams,
                                                     "",
                                                     WebhookEvent.class);
		}

	}


    /**
      * Helper method to create a new request.
      */
	public skipRequest skipRequest(){
		return new skipRequest(headerParams);
	}



    /**
      * API Request for status
      */
	public class statusRequest implements ApiRequest {

        Map headerParams;
        Map> queryParams = new HashMap<>();
        Map> formParams = new HashMap<>();
        Object body = null;

        public statusRequest(Map headerParams) {
            this.headerParams = headerParams;
        }

        public Map> getPreparedFormParams() {
            return Collections.unmodifiableMap(formParams);
        }

		public statusRequest aid(String aid) {
            setQueryParam(this.queryParams, "aid", aid);
			return this;
		}






		public WebhookStatus  execute() throws ApiException{

			String path = "/publisher/webhook/status".replaceAll("\\{format\\}","json");

			return getInvoker().invokeAPI(basePath, path, "GET", token,
                                                     queryParams, null,
                                                     headerParams,
                                                     formParams,
                                                     "",
                                                     WebhookStatus.class);
		}

	}


    /**
      * Helper method to create a new request.
      */
	public statusRequest statusRequest(){
		return new statusRequest(headerParams);
	}




}