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

com.twilio.rest.ipmessaging.v2.service.channel.MemberUpdater Maven / Gradle / Ivy

/**
 * This code was generated by
 * \ / _    _  _|   _  _
 *  | (_)\/(_)(_|\/| |(/_  v1.0.0
 *       /       /
 */

package com.twilio.rest.ipmessaging.v2.service.channel;

import com.twilio.base.Updater;
import com.twilio.converter.DateConverter;
import com.twilio.exception.ApiConnectionException;
import com.twilio.exception.ApiException;
import com.twilio.exception.RestException;
import com.twilio.http.HttpMethod;
import com.twilio.http.Request;
import com.twilio.http.Response;
import com.twilio.http.TwilioRestClient;
import com.twilio.rest.Domains;
import org.joda.time.DateTime;

public class MemberUpdater extends Updater {
    private final String pathServiceSid;
    private final String pathChannelSid;
    private final String pathSid;
    private String roleSid;
    private Integer lastConsumedMessageIndex;
    private DateTime lastConsumptionTimestamp;
    private DateTime dateCreated;
    private DateTime dateUpdated;

    /**
     * Construct a new MemberUpdater.
     * 
     * @param pathServiceSid Sid of the Service this member belongs to.
     * @param pathChannelSid Key that uniquely defines the channel this member
     *                       belongs to.
     * @param pathSid Key that uniquely defines the member to update.
     */
    public MemberUpdater(final String pathServiceSid, 
                         final String pathChannelSid, 
                         final String pathSid) {
        this.pathServiceSid = pathServiceSid;
        this.pathChannelSid = pathChannelSid;
        this.pathSid = pathSid;
    }

    /**
     * The role to be assigned to this member. Defaults to the roles specified on
     * the [Service](https://www.twilio.com/docs/chat/api/services)..
     * 
     * @param roleSid The role to be assigned to this member.
     * @return this
     */
    public MemberUpdater setRoleSid(final String roleSid) {
        this.roleSid = roleSid;
        return this;
    }

    /**
     * Field used to specify the last consumed Message index for the Channel for
     * this Member.  Should only be used when recreating a Member from a
     * backup/separate source..
     * 
     * @param lastConsumedMessageIndex Field used to specify the last consumed
     *                                 Message index for the Channel for this
     *                                 Member.
     * @return this
     */
    public MemberUpdater setLastConsumedMessageIndex(final Integer lastConsumedMessageIndex) {
        this.lastConsumedMessageIndex = lastConsumedMessageIndex;
        return this;
    }

    /**
     * ISO8601 time indicating the last datetime the Member consumed a Message in
     * the Channel.  Should only be used when recreating a Member from a
     * backup/separate source.
     * 
     * @param lastConsumptionTimestamp ISO8601 time indicating the last datetime
     *                                 the Member consumed a Message in the Channel.
     * @return this
     */
    public MemberUpdater setLastConsumptionTimestamp(final DateTime lastConsumptionTimestamp) {
        this.lastConsumptionTimestamp = lastConsumptionTimestamp;
        return this;
    }

    /**
     * The ISO8601 time specifying the datetime the Members should be set as being
     * created.  Will be set to the current time by the Chat service if not
     * specified.  Note that this should only be used in cases where a Member is
     * being recreated from a backup/separate source.
     * 
     * @param dateCreated The ISO8601 time specifying the datetime the Members
     *                    should be set as being created.
     * @return this
     */
    public MemberUpdater setDateCreated(final DateTime dateCreated) {
        this.dateCreated = dateCreated;
        return this;
    }

    /**
     * The ISO8601 time specifying the datetime the Member should be set as having
     * been last updated.  Will be set to the `null` by the Chat service if not
     * specified.  Note that this should only be used in cases where a Member is
     * being recreated from a backup/separate source  and where a Member was
     * previously updated..
     * 
     * @param dateUpdated The ISO8601 time specifying the datetime the Member
     *                    should be set as having been last updated.
     * @return this
     */
    public MemberUpdater setDateUpdated(final DateTime dateUpdated) {
        this.dateUpdated = dateUpdated;
        return this;
    }

    /**
     * Make the request to the Twilio API to perform the update.
     * 
     * @param client TwilioRestClient with which to make the request
     * @return Updated Member
     */
    @Override
    @SuppressWarnings("checkstyle:linelength")
    public Member update(final TwilioRestClient client) {
        Request request = new Request(
            HttpMethod.POST,
            Domains.IPMESSAGING.toString(),
            "/v2/Services/" + this.pathServiceSid + "/Channels/" + this.pathChannelSid + "/Members/" + this.pathSid + "",
            client.getRegion()
        );

        addPostParams(request);
        Response response = client.request(request);

        if (response == null) {
            throw new ApiConnectionException("Member update failed: Unable to connect to server");
        } else if (!TwilioRestClient.SUCCESS.apply(response.getStatusCode())) {
            RestException restException = RestException.fromJson(response.getStream(), client.getObjectMapper());
            if (restException == null) {
                throw new ApiException("Server Error, no content");
            }

            throw new ApiException(
                restException.getMessage(),
                restException.getCode(),
                restException.getMoreInfo(),
                restException.getStatus(),
                null
            );
        }

        return Member.fromJson(response.getStream(), client.getObjectMapper());
    }

    /**
     * Add the requested post parameters to the Request.
     * 
     * @param request Request to add post params to
     */
    private void addPostParams(final Request request) {
        if (roleSid != null) {
            request.addPostParam("RoleSid", roleSid);
        }

        if (lastConsumedMessageIndex != null) {
            request.addPostParam("LastConsumedMessageIndex", lastConsumedMessageIndex.toString());
        }

        if (lastConsumptionTimestamp != null) {
            request.addPostParam("LastConsumptionTimestamp", lastConsumptionTimestamp.toString());
        }

        if (dateCreated != null) {
            request.addPostParam("DateCreated", dateCreated.toString());
        }

        if (dateUpdated != null) {
            request.addPostParam("DateUpdated", dateUpdated.toString());
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy