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

com.antonioaltieri.telegram.botapi.requests.ApiRequest Maven / Gradle / Ivy

There is a newer version: 1.3.7
Show newest version
package com.antonioaltieri.telegram.botapi.requests;

import com.antonioaltieri.telegram.botapi.requests.ApiResult;
import com.antonioaltieri.telegram.botapi.requests.TelegramApi;
import com.antonioaltieri.telegram.botapi.types.Message;
import com.antonioaltieri.telegram.botapi.types.Update;
import com.antonioaltieri.telegram.botapi.types.User;
import com.antonioaltieri.telegram.botapi.types.UserProfilePhotos;
import com.google.gson.reflect.TypeToken;

import java.io.File;
import java.lang.reflect.Type;
import java.util.List;
import java.util.Map;

interface ApiRequest {

    String getMethodName();

    ResultTypes getResultType();

    Map getArgs();

    RequestStrategy getRequestStrategy();

    enum ResultTypes {
        USER(new TypeToken>() {}.getType()),
        MESSAGE(new TypeToken>() {}.getType()),
        BOOLEAN(new TypeToken>() {}.getType()),
        USER_PROFILE_PHOTOS(new TypeToken>() {}.getType()),
        LIST_OF_UPDATES(new TypeToken>>() {}.getType());

        private Type type;

        ResultTypes(Type type) {
            this.type = type;
        }

        public Type getType() {
            return type;
        }
    }

    interface RequestStrategy {

        String makeRequest(com.antonioaltieri.telegram.botapi.requests.ApiRequest request, TelegramApi api);

    }

    final class PostStrategy implements RequestStrategy {

        @Override
        public String makeRequest(com.antonioaltieri.telegram.botapi.requests.ApiRequest request, TelegramApi api) {
            return api.makePostRequest(request.getMethodName(), request.getArgs());
        }

        @Override
        public String toString() {
            return "POST";
        }
    }

    final class GetStrategy implements RequestStrategy {

        @Override
        public String makeRequest(com.antonioaltieri.telegram.botapi.requests.ApiRequest request, TelegramApi api) {
            return api.makeGetRequest(request.getMethodName());
        }

        @Override
        public String toString() {
            return "GET";
        }
    }

    final class MultipartStrategy implements RequestStrategy {

        private File file;
        private String fieldName;

        public MultipartStrategy(File file, String fieldName) {
            this.file = file;
            this.fieldName = fieldName;
        }

        @Override
        public String makeRequest(com.antonioaltieri.telegram.botapi.requests.ApiRequest request, TelegramApi api) {
            return api.makeMultipartRequest(request.getMethodName(), request.getArgs(), fieldName, file);
        }

        @Override
        public String toString() {
            return "MULTIPART";
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy