com.pengrad.telegrambot.request.BaseRequest Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of java-telegram-bot-api Show documentation
Show all versions of java-telegram-bot-api Show documentation
Java API for Telegram Bot API
package com.pengrad.telegrambot.request;
import com.pengrad.telegrambot.utility.BotUtils;
import com.pengrad.telegrambot.response.BaseResponse;
import org.jetbrains.annotations.NotNull;
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 extends R> responseClass;
private final Map parameters;
public BaseRequest(Class extends R> responseClass) {
this.responseClass = responseClass;
this.parameters = new LinkedHashMap<>();
}
@NotNull
protected T add(String name, Object val) {
parameters.put(name, val);
return thisAsT;
}
@NotNull
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 extends R> 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);
}
}