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

com.pengrad.telegrambot.request.BaseRequest Maven / Gradle / Ivy

There is a newer version: 7.11.0
Show newest version
package com.pengrad.telegrambot.request;

import com.pengrad.telegrambot.BotUtils;
import com.pengrad.telegrambot.response.BaseResponse;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * stas
 * 5/1/16.
 */
abstract public class BaseRequest, R extends BaseResponse> {

    @SuppressWarnings("unchecked")
    protected final T thisAsT = (T) this;

    private final Class responseClass;
    private final Map parameters;

    public BaseRequest(Class responseClass) {
        this.responseClass = responseClass;
        this.parameters = new LinkedHashMap<>();
    }

    protected T add(String name, Object val) {
        parameters.put(name, val);
        return thisAsT;
    }

    protected T addAll(Map values) {
        parameters.putAll(values);
        return thisAsT;
    }

    public String getMethod() {
        String className = this.getClass().getSimpleName();
        return Character.toLowerCase(className.charAt(0)) + className.substring(1);
    }

    public Map getParameters() {
        return parameters;
    }

    public Class getResponseType() {
        return responseClass;
    }

    public boolean isMultipart() {
        return false;
    }

    public String getFileName() {
        return ContentTypes.GENERAL_FILE_NAME;
    }

    public String getContentType() {
        return ContentTypes.GENERAL_MIME_TYPE;
    }

    public int getTimeoutSeconds() {
        return 0;
    }

    public String toWebhookResponse() {
        Map fullMap = new LinkedHashMap<>(parameters);
        fullMap.put("method", getMethod());
        return BotUtils.toJson(fullMap);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy