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

com.linecorp.bot.client.LineMessagingClientImpl Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2020 LINE Corporation
 *
 * LINE Corporation licenses this file to you under the Apache License,
 * version 2.0 (the "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at:
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations
 * under the License.
 */

package com.linecorp.bot.client;

import static java.util.Collections.emptyList;

import java.util.List;
import java.util.concurrent.CompletableFuture;

import com.linecorp.bot.client.exception.GeneralLineMessagingException;
import com.linecorp.bot.model.Broadcast;
import com.linecorp.bot.model.Multicast;
import com.linecorp.bot.model.Narrowcast;
import com.linecorp.bot.model.PushMessage;
import com.linecorp.bot.model.ReplyMessage;
import com.linecorp.bot.model.group.GroupMemberCountResponse;
import com.linecorp.bot.model.group.GroupSummaryResponse;
import com.linecorp.bot.model.profile.MembersIdsResponse;
import com.linecorp.bot.model.profile.UserProfileResponse;
import com.linecorp.bot.model.request.GetFollowersRequest;
import com.linecorp.bot.model.request.SetWebhookEndpointRequest;
import com.linecorp.bot.model.request.TestWebhookEndpointRequest;
import com.linecorp.bot.model.response.BotApiResponse;
import com.linecorp.bot.model.response.BotInfoResponse;
import com.linecorp.bot.model.response.GetAggregationUnitNameListResponse;
import com.linecorp.bot.model.response.GetAggregationUnitUsageResponse;
import com.linecorp.bot.model.response.GetFollowersResponse;
import com.linecorp.bot.model.response.GetMessageEventResponse;
import com.linecorp.bot.model.response.GetNumberOfFollowersResponse;
import com.linecorp.bot.model.response.GetNumberOfMessageDeliveriesResponse;
import com.linecorp.bot.model.response.GetStatisticsPerUnitResponse;
import com.linecorp.bot.model.response.GetWebhookEndpointResponse;
import com.linecorp.bot.model.response.IssueLinkTokenResponse;
import com.linecorp.bot.model.response.MessageQuotaResponse;
import com.linecorp.bot.model.response.NarrowcastProgressResponse;
import com.linecorp.bot.model.response.NumberOfMessagesResponse;
import com.linecorp.bot.model.response.QuotaConsumptionResponse;
import com.linecorp.bot.model.response.SetWebhookEndpointResponse;
import com.linecorp.bot.model.response.TestWebhookEndpointResponse;
import com.linecorp.bot.model.response.demographics.GetFriendsDemographicsResponse;
import com.linecorp.bot.model.richmenu.RichMenu;
import com.linecorp.bot.model.richmenu.RichMenuBulkLinkRequest;
import com.linecorp.bot.model.richmenu.RichMenuBulkUnlinkRequest;
import com.linecorp.bot.model.richmenu.RichMenuIdResponse;
import com.linecorp.bot.model.richmenu.RichMenuListResponse;
import com.linecorp.bot.model.richmenu.RichMenuResponse;
import com.linecorp.bot.model.richmenualias.CreateRichMenuAliasRequest;
import com.linecorp.bot.model.richmenualias.RichMenuAliasListResponse;
import com.linecorp.bot.model.richmenualias.RichMenuAliasResponse;
import com.linecorp.bot.model.richmenualias.UpdateRichMenuAliasRequest;
import com.linecorp.bot.model.room.RoomMemberCountResponse;

import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Proxy implementation of {@link LineMessagingClient} to hind internal implementation.
 */
@Slf4j
@AllArgsConstructor
public class LineMessagingClientImpl implements LineMessagingClient {
    static final ExceptionConverter EXCEPTION_CONVERTER = new ExceptionConverter();
    private static final String ORG_TYPE_GROUP = "group"; // TODO Enum
    private static final String ORG_TYPE_ROOM = "room";
    private static final BotApiResponseBody BOT_API_SUCCESS_RESPONSE_BODY =
            BotApiResponseBody.builder().message("").details(emptyList()).build();

    private final LineMessagingService retrofitImpl;

    private final LineBlobClient blobDelegationTarget;

    @Override
    public CompletableFuture replyMessage(final ReplyMessage replyMessage) {
        return toBotApiResponseFuture(retrofitImpl.replyMessage(replyMessage));
    }

    @Override
    public CompletableFuture pushMessage(final PushMessage pushMessage) {
        return toBotApiResponseFuture(retrofitImpl.pushMessage(null, pushMessage));
    }

    @Override
    public CompletableFuture multicast(final Multicast multicast) {
        return toBotApiResponseFuture(retrofitImpl.multicast(null, multicast));
    }

    @Override
    public CompletableFuture broadcast(Broadcast broadcast) {
        return toBotApiResponseFuture(retrofitImpl.broadcast(null, broadcast));
    }

    @Override
    public CompletableFuture narrowcast(Narrowcast narrowcast) {
        return toBotApiResponseFuture(retrofitImpl.narrowcast(null, narrowcast));
    }

    /**
     * Gets the status of a narrowcast message.
     */
    @Override
    public CompletableFuture getNarrowcastProgress(String requestId) {
        return toFuture(retrofitImpl.getNarrowcastProgress(requestId));
    }

    @Override
    public CompletableFuture getMessageQuota() {
        return toFuture(retrofitImpl.getMessageQuota());
    }

    @Override
    public CompletableFuture getMessageQuotaConsumption() {
        return toFuture(retrofitImpl.getMessageQuotaConsumption());
    }

    @Override
    public CompletableFuture getNumberOfSentReplyMessages(String date) {
        return toFuture(retrofitImpl.getNumberOfSentReplyMessages(date));
    }

    @Override
    public CompletableFuture getNumberOfSentPushMessages(String date) {
        return toFuture(retrofitImpl.getNumberOfSentPushMessages(date));
    }

    @Override
    public CompletableFuture getNumberOfSentMulticastMessages(String date) {
        return toFuture(retrofitImpl.getNumberOfSentMulticastMessages(date));
    }

    @Override
    public CompletableFuture getNumberOfSentBroadcastMessages(String date) {
        return toFuture(retrofitImpl.getNumberOfSentBroadcastMessages(date));
    }

    @Override
    public CompletableFuture getProfile(final String userId) {
        return toFuture(retrofitImpl.getProfile(userId));
    }

    @Override
    public CompletableFuture getGroupMemberProfile(
            final String groupId, final String userId) {
        return toFuture(retrofitImpl.getMemberProfile(ORG_TYPE_GROUP, groupId, userId));
    }

    @Override
    public CompletableFuture getRoomMemberProfile(
            final String roomId, final String userId) {
        return toFuture(retrofitImpl.getMemberProfile(ORG_TYPE_ROOM, roomId, userId));
    }

    @Override
    public CompletableFuture getGroupMembersIds(
            final String groupId, final String start) {
        return toFuture(retrofitImpl.getMembersIds(ORG_TYPE_GROUP, groupId, start));
    }

    @Override
    public CompletableFuture getRoomMembersIds(
            final String roomId, final String start) {
        return toFuture(retrofitImpl.getMembersIds(ORG_TYPE_ROOM, roomId, start));
    }

    @Override
    public CompletableFuture leaveGroup(final String groupId) {
        return toBotApiResponseFuture(retrofitImpl.leaveGroup(groupId));
    }

    @Override
    public CompletableFuture leaveRoom(final String roomId) {
        return toBotApiResponseFuture(retrofitImpl.leaveRoom(roomId));
    }

    @Override
    public CompletableFuture getGroupSummary(final String groupId) {
        return toFuture(retrofitImpl.getGroupSummary(groupId));
    }

    @Override
    public CompletableFuture getGroupMemberCount(String groupId) {
        return toFuture(retrofitImpl.getGroupMemberCount(groupId));
    }

    @Override
    public CompletableFuture getRoomMemberCount(String roomId) {
        return toFuture(retrofitImpl.getRoomMemberCount(roomId));
    }

    @Override
    public CompletableFuture getRichMenu(final String richMenuId) {
        return toFuture(retrofitImpl.getRichMenu(richMenuId));
    }

    @Override
    public CompletableFuture validateRichMenuObject(final RichMenu richMenu) {
        return toBotApiFuture(retrofitImpl.validateRichMenuObject(richMenu));
    }

    @Override
    public CompletableFuture createRichMenu(final RichMenu richMenu) {
        return toFuture(retrofitImpl.createRichMenu(richMenu));
    }

    @Override
    public CompletableFuture deleteRichMenu(final String richMenuId) {
        return toBotApiFuture(retrofitImpl.deleteRichMenu(richMenuId));
    }

    @Override
    public CompletableFuture getRichMenuIdOfUser(final String userId) {
        return toFuture(retrofitImpl.getRichMenuIdOfUser(userId));
    }

    @Override
    public CompletableFuture linkRichMenuIdToUser(
            final String userId, final String richMenuId) {
        return toBotApiFuture(retrofitImpl.linkRichMenuToUser(userId, richMenuId));
    }

    @Override
    public CompletableFuture linkRichMenuIdToUsers(List userIds, String richMenuId) {
        return toBotApiFuture(retrofitImpl.linkRichMenuToUsers(RichMenuBulkLinkRequest.builder()
                                                                                      .richMenuId(richMenuId)
                                                                                      .userIds(userIds)
                                                                                      .build()));
    }

    @Override
    public CompletableFuture unlinkRichMenuIdFromUser(final String userId) {
        return toBotApiFuture(retrofitImpl.unlinkRichMenuIdFromUser(userId));
    }

    @Override
    public CompletableFuture unlinkRichMenuIdFromUsers(List userIds) {
        return toBotApiFuture(retrofitImpl.unlinkRichMenuIdFromUsers(RichMenuBulkUnlinkRequest.builder()
                                                                                              .userIds(userIds)
                                                                                              .build()));
    }

    @Override
    public CompletableFuture getRichMenuList() {
        return toFuture(retrofitImpl.getRichMenuList());
    }

    @Override
    public CompletableFuture setDefaultRichMenu(final String richMenuId) {
        return toBotApiFuture(retrofitImpl.setDefaultRichMenu(richMenuId));
    }

    @Override
    public CompletableFuture getDefaultRichMenuId() {
        return toFuture(retrofitImpl.getDefaultRichMenuId());
    }

    @Override
    public CompletableFuture cancelDefaultRichMenu() {
        return toBotApiFuture(retrofitImpl.cancelDefaultRichMenu());
    }

    @Override
    public CompletableFuture createRichMenuAlias(CreateRichMenuAliasRequest request) {
        return toBotApiResponseFuture(retrofitImpl.createRichMenuAlias(request));
    }

    @Override
    public CompletableFuture updateRichMenuAlias(String richMenuAliasId,
                                                                 UpdateRichMenuAliasRequest request) {
        return toBotApiResponseFuture(retrofitImpl.updateRichMenuAlias(richMenuAliasId, request));
    }

    @Override
    public CompletableFuture getRichMenuAlias(
            String richMenuAliasId) {
        return toFuture(retrofitImpl.getRichMenuAlias(richMenuAliasId));
    }

    @Override
    public CompletableFuture getRichMenuAliasList() {
        return toFuture(retrofitImpl.getRichMenuAliasList());
    }

    @Override
    public CompletableFuture deleteRichMenuAlias(String richMenuAliasId) {
        return toBotApiResponseFuture(retrofitImpl.deleteRichMenuAlias(richMenuAliasId));
    }

    @Override
    public CompletableFuture issueLinkToken(String userId) {
        return toFuture(retrofitImpl.issueLinkToken(userId));
    }

    @Override
    public CompletableFuture getFriendsDemographics() {
        return toFuture(retrofitImpl.getFriendsDemographics());
    }

    @Override
    public CompletableFuture getNumberOfMessageDeliveries(String date) {
        return toFuture(retrofitImpl.getNumberOfMessageDeliveries(date));
    }

    @Override
    public CompletableFuture getNumberOfFollowers(String date) {
        return toFuture(retrofitImpl.getNumberOfFollowers(date));
    }

    @Override
    public CompletableFuture getFollowers(GetFollowersRequest request) {
        return toFuture(retrofitImpl.getFollowers(request.getNext(), request.getLimit()));
    }

    @Override
    public CompletableFuture getMessageEvent(String requestId) {
        return toFuture(retrofitImpl.getMessageEvent(requestId));
    }

    @Override
    public CompletableFuture getBotInfo() {
        return toFuture(retrofitImpl.getBotInfo());
    }

    @Override
    public CompletableFuture getWebhookEndpoint() {
        return toFuture(retrofitImpl.getWebhookEndpoint());
    }

    @Override
    public CompletableFuture setWebhookEndpoint(SetWebhookEndpointRequest request) {
        return toFuture(retrofitImpl.setWebhookEndpoint(request));
    }

    @Override
    public CompletableFuture testWebhookEndpoint(
            TestWebhookEndpointRequest request) {
        return toFuture(retrofitImpl.testWebhookEndpoint(request));
    }

    @Override
    public CompletableFuture getStatisticsPerUnit(
            String customAggregationUnit, String from, String to) {
        return toFuture(retrofitImpl.getStatisticsPerUnit(customAggregationUnit, from, to));
    }

    @Override
    public CompletableFuture getAggregationUnitUsage() {
        return toFuture(retrofitImpl.getAggregationUnitUsage());
    }

    @Override
    public CompletableFuture getAggregationUnitNameList(
            String limit, String start) {
        return toFuture(retrofitImpl.getAggregationUnitNameList(limit, start));
    }

    // TODO: Extract this method.
    static  CompletableFuture toFuture(Call callToWrap) {
        final CallbackAdaptor completableFuture = new CallbackAdaptor<>();
        callToWrap.enqueue(completableFuture);
        return completableFuture;
    }

    private static CompletableFuture toBotApiResponseFuture(
            final Call callToWrap) {
        final BotApiCallbackAdaptor completableFuture = new BotApiCallbackAdaptor();
        callToWrap.enqueue(completableFuture);
        return completableFuture;
    }

    static CompletableFuture toBotApiFuture(Call callToWrap) {
        final VoidToBotApiCallbackAdaptor completableFuture = new VoidToBotApiCallbackAdaptor();
        callToWrap.enqueue(completableFuture);
        return completableFuture;
    }

    static class CallbackAdaptor extends CompletableFuture implements Callback {
        @Override
        public void onResponse(final Call call, final Response response) {
            if (response.isSuccessful()) {
                complete(response.body());
            } else {
                completeExceptionally(EXCEPTION_CONVERTER.apply(response));
            }
        }

        @Override
        public void onFailure(final Call call, final Throwable t) {
            completeExceptionally(
                    new GeneralLineMessagingException(t.getMessage(), null, t));
        }
    }

    static class VoidToBotApiCallbackAdaptor extends CompletableFuture
            implements Callback {
        @Override
        public void onResponse(final Call call, final Response response) {
            if (response.isSuccessful()) {
                final String requestId = response.headers().get("x-line-request-id");
                complete(BOT_API_SUCCESS_RESPONSE_BODY.withRequestId(requestId));
            } else {
                completeExceptionally(EXCEPTION_CONVERTER.apply(response));
            }
        }

        @Override
        public void onFailure(final Call call, final Throwable t) {
            completeExceptionally(
                    new GeneralLineMessagingException(t.getMessage(), null, t));
        }
    }

    static class BotApiCallbackAdaptor extends CompletableFuture
            implements Callback {
        @Override
        public void onResponse(final Call call,
                               final Response response) {
            if (response.isSuccessful()) {
                final String requestId = response.headers().get("x-line-request-id");
                complete(response.body().withRequestId(requestId));
            } else {
                completeExceptionally(EXCEPTION_CONVERTER.apply(response));
            }
        }

        @Override
        public void onFailure(final Call call, final Throwable t) {
            completeExceptionally(
                    new GeneralLineMessagingException(t.getMessage(), null, t));
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy