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

com.twilio.rest.api.v2010.account.availablephonenumbercountry.MobileReader Maven / Gradle / Ivy

There is a newer version: 7.9.1
Show newest version
/**
 * This code was generated by
 * \ / _    _  _|   _  _
 *  | (_)\/(_)(_|\/| |(/_  v1.0.0
 *       /       /
 */

package com.twilio.rest.api.v2010.account.availablephonenumbercountry;

import com.twilio.base.Page;
import com.twilio.base.Reader;
import com.twilio.base.ResourceSet;
import com.twilio.converter.Promoter;
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;

public class MobileReader extends Reader {
    private String pathAccountSid;
    private final String pathCountryCode;
    private Integer areaCode;
    private String contains;
    private Boolean smsEnabled;
    private Boolean mmsEnabled;
    private Boolean voiceEnabled;
    private Boolean excludeAllAddressRequired;
    private Boolean excludeLocalAddressRequired;
    private Boolean excludeForeignAddressRequired;
    private Boolean beta;
    private com.twilio.type.PhoneNumber nearNumber;
    private String nearLatLong;
    private Integer distance;
    private String inPostalCode;
    private String inRegion;
    private String inRateCenter;
    private String inLata;
    private String inLocality;
    private Boolean faxEnabled;

    /**
     * Construct a new MobileReader.
     * 
     * @param pathCountryCode The country_code
     */
    public MobileReader(final String pathCountryCode) {
        this.pathCountryCode = pathCountryCode;
    }

    /**
     * Construct a new MobileReader.
     * 
     * @param pathAccountSid The account_sid
     * @param pathCountryCode The country_code
     */
    public MobileReader(final String pathAccountSid, 
                        final String pathCountryCode) {
        this.pathAccountSid = pathAccountSid;
        this.pathCountryCode = pathCountryCode;
    }

    /**
     * Find phone numbers in the specified area code. (US and Canada only).
     * 
     * @param areaCode Find phone numbers in the specified area code.
     * @return this
     */
    public MobileReader setAreaCode(final Integer areaCode) {
        this.areaCode = areaCode;
        return this;
    }

    /**
     * A pattern on which to match phone numbers. Valid characters are `'*'` and
     * `[0-9a-zA-Z]`. The `'*'` character will match any single digit. See [Example
     * 2](https://www.twilio.com/docs/api/rest/available-phone-numbers#local-get-basic-example-2) and [Example 3](https://www.twilio.com/docs/api/rest/available-phone-numbers#local-get-basic-example-3) below. *NOTE:* Patterns must be at least two characters long..
     * 
     * @param contains A pattern on which to match phone numbers.
     * @return this
     */
    public MobileReader setContains(final String contains) {
        this.contains = contains;
        return this;
    }

    /**
     * This indicates whether the phone numbers can receive text messages. Possible
     * values are `true` or `false`..
     * 
     * @param smsEnabled This indicates whether the phone numbers can receive text
     *                   messages.
     * @return this
     */
    public MobileReader setSmsEnabled(final Boolean smsEnabled) {
        this.smsEnabled = smsEnabled;
        return this;
    }

    /**
     * This indicates whether the phone numbers can receive MMS messages. Possible
     * values are `true` or `false`..
     * 
     * @param mmsEnabled This indicates whether the phone numbers can receive MMS
     *                   messages.
     * @return this
     */
    public MobileReader setMmsEnabled(final Boolean mmsEnabled) {
        this.mmsEnabled = mmsEnabled;
        return this;
    }

    /**
     * This indicates whether the phone numbers can receive calls. Possible values
     * are `true` or `false`..
     * 
     * @param voiceEnabled This indicates whether the phone numbers can receive
     *                     calls.
     * @return this
     */
    public MobileReader setVoiceEnabled(final Boolean voiceEnabled) {
        this.voiceEnabled = voiceEnabled;
        return this;
    }

    /**
     * Indicates whether the response includes phone numbers which require any
     * [Address](https://www.twilio.com/docs/usage/api/addresses). Possible values
     * are `true` or `false`. If not specified, the default is `false`, and results
     * could include phone numbers with an Address required..
     * 
     * @param excludeAllAddressRequired Indicates whether the response includes
     *                                  phone numbers which require any Address.
     * @return this
     */
    public MobileReader setExcludeAllAddressRequired(final Boolean excludeAllAddressRequired) {
        this.excludeAllAddressRequired = excludeAllAddressRequired;
        return this;
    }

    /**
     * Indicates whether the response includes phone numbers which require a local
     * [Address](https://www.twilio.com/docs/usage/api/addresses). Possible values
     * are `true` or `false`. If not specified, the default is `false`, and results
     * could include phone numbers with a local Address required..
     * 
     * @param excludeLocalAddressRequired Indicates whether the response includes
     *                                    phone numbers which require a local
     *                                    Address.
     * @return this
     */
    public MobileReader setExcludeLocalAddressRequired(final Boolean excludeLocalAddressRequired) {
        this.excludeLocalAddressRequired = excludeLocalAddressRequired;
        return this;
    }

    /**
     * Indicates whether the response includes phone numbers which require a foreign
     * [Address](https://www.twilio.com/docs/usage/api/addresses). Possible values
     * are `true` or `false`. If not specified, the default is `false`, and results
     * could include phone numbers with a foreign Address required..
     * 
     * @param excludeForeignAddressRequired Indicates whether the response includes
     *                                      phone numbers which require a foreign
     *                                      Address.
     * @return this
     */
    public MobileReader setExcludeForeignAddressRequired(final Boolean excludeForeignAddressRequired) {
        this.excludeForeignAddressRequired = excludeForeignAddressRequired;
        return this;
    }

    /**
     * Include phone numbers new to the Twilio platform. Possible values are either
     * `true` or `false`. Default is `true`..
     * 
     * @param beta Include phone numbers new to the Twilio platform.
     * @return this
     */
    public MobileReader setBeta(final Boolean beta) {
        this.beta = beta;
        return this;
    }

    /**
     * Given a phone number, find a geographically close number within `Distance`
     * miles. Distance defaults to 25 miles. *Limited to US and Canadian phone
     * numbers.*.
     * 
     * @param nearNumber Given a phone number, find a geographically close number
     *                   within Distance miles. (US/Canada only)
     * @return this
     */
    public MobileReader setNearNumber(final com.twilio.type.PhoneNumber nearNumber) {
        this.nearNumber = nearNumber;
        return this;
    }

    /**
     * Given a phone number, find a geographically close number within `Distance`
     * miles. Distance defaults to 25 miles. *Limited to US and Canadian phone
     * numbers.*.
     * 
     * @param nearNumber Given a phone number, find a geographically close number
     *                   within Distance miles. (US/Canada only)
     * @return this
     */
    public MobileReader setNearNumber(final String nearNumber) {
        return setNearNumber(Promoter.phoneNumberFromString(nearNumber));
    }

    /**
     * Given a latitude/longitude pair `lat,long` find geographically close numbers
     * within `Distance` miles. *Limited to US and Canadian phone numbers.*.
     * 
     * @param nearLatLong Given a latitude/longitude pair lat,long find
     *                    geographically close numbers within Distance miles.
     *                    (US/Canada only)
     * @return this
     */
    public MobileReader setNearLatLong(final String nearLatLong) {
        this.nearLatLong = nearLatLong;
        return this;
    }

    /**
     * Specifies the search radius for a `Near-` query in miles. If not specified
     * this defaults to 25 miles. Maximum searchable distance is 500 miles. *Limited
     * to US and Canadian phone numbers.*.
     * 
     * @param distance Specifies the search radius for a Near- query in miles.
     *                 (US/Canada only)
     * @return this
     */
    public MobileReader setDistance(final Integer distance) {
        this.distance = distance;
        return this;
    }

    /**
     * Limit results to a particular postal code. Given a phone number, search
     * within the same postal code as that number. *Limited to US and Canadian phone
     * numbers.*.
     * 
     * @param inPostalCode Limit results to a particular postal code. (US/Canada
     *                     only)
     * @return this
     */
    public MobileReader setInPostalCode(final String inPostalCode) {
        this.inPostalCode = inPostalCode;
        return this;
    }

    /**
     * Limit results to a particular region (i.e. State/Province). Given a phone
     * number, search within the same Region as that number. *Limited to US and
     * Canadian phone numbers.*.
     * 
     * @param inRegion Limit results to a particular region. (US/Canada only)
     * @return this
     */
    public MobileReader setInRegion(final String inRegion) {
        this.inRegion = inRegion;
        return this;
    }

    /**
     * Limit results to a specific rate center, or given a phone number search
     * within the same rate center as that number. Requires InLata to be set as
     * well. *Limited to US and Canadian phone numbers.*.
     * 
     * @param inRateCenter Limit results to a specific rate center, or given a
     *                     phone number search within the same rate center as that
     *                     number. (US/Canada only)
     * @return this
     */
    public MobileReader setInRateCenter(final String inRateCenter) {
        this.inRateCenter = inRateCenter;
        return this;
    }

    /**
     * Limit results to a specific Local access and transport area
     * ([LATA](http://en.wikipedia.org/wiki/Local_access_and_transport_area)). Given
     * a phone number, search within the same
     * [LATA](http://en.wikipedia.org/wiki/Local_access_and_transport_area) as that
     * number. *Limited to US and Canadian phone numbers.*.
     * 
     * @param inLata Limit results to a specific Local access and transport area.
     *               (US/Canada only)
     * @return this
     */
    public MobileReader setInLata(final String inLata) {
        this.inLata = inLata;
        return this;
    }

    /**
     * Limit results to a particular locality (i.e.  City). Given a phone number,
     * search within the same Locality as that number..
     * 
     * @param inLocality Limit results to a particular locality.
     * @return this
     */
    public MobileReader setInLocality(final String inLocality) {
        this.inLocality = inLocality;
        return this;
    }

    /**
     * This indicates whether the phone numbers can receive faxes. Possible values
     * are `true` or `false`..
     * 
     * @param faxEnabled This indicates whether the phone numbers can receive faxes.
     * @return this
     */
    public MobileReader setFaxEnabled(final Boolean faxEnabled) {
        this.faxEnabled = faxEnabled;
        return this;
    }

    /**
     * Make the request to the Twilio API to perform the read.
     * 
     * @param client TwilioRestClient with which to make the request
     * @return Mobile ResourceSet
     */
    @Override
    public ResourceSet read(final TwilioRestClient client) {
        return new ResourceSet<>(this, client, firstPage(client));
    }

    /**
     * Make the request to the Twilio API to perform the read.
     * 
     * @param client TwilioRestClient with which to make the request
     * @return Mobile ResourceSet
     */
    @Override
    @SuppressWarnings("checkstyle:linelength")
    public Page firstPage(final TwilioRestClient client) {
        this.pathAccountSid = this.pathAccountSid == null ? client.getAccountSid() : this.pathAccountSid;
        Request request = new Request(
            HttpMethod.GET,
            Domains.API.toString(),
            "/2010-04-01/Accounts/" + this.pathAccountSid + "/AvailablePhoneNumbers/" + this.pathCountryCode + "/Mobile.json",
            client.getRegion()
        );

        addQueryParams(request);
        return pageForRequest(client, request);
    }

    /**
     * Retrieve the target page from the Twilio API.
     * 
     * @param targetUrl API-generated URL for the requested results page
     * @param client TwilioRestClient with which to make the request
     * @return Mobile ResourceSet
     */
    @Override
    @SuppressWarnings("checkstyle:linelength")
    public Page getPage(final String targetUrl, final TwilioRestClient client) {
        this.pathAccountSid = this.pathAccountSid == null ? client.getAccountSid() : this.pathAccountSid;
        Request request = new Request(
            HttpMethod.GET,
            targetUrl
        );

        return pageForRequest(client, request);
    }

    /**
     * Retrieve the next page from the Twilio API.
     * 
     * @param page current page
     * @param client TwilioRestClient with which to make the request
     * @return Next Page
     */
    @Override
    public Page nextPage(final Page page, 
                                 final TwilioRestClient client) {
        Request request = new Request(
            HttpMethod.GET,
            page.getNextPageUrl(
                Domains.API.toString(),
                client.getRegion()
            )
        );
        return pageForRequest(client, request);
    }

    /**
     * Retrieve the previous page from the Twilio API.
     * 
     * @param page current page
     * @param client TwilioRestClient with which to make the request
     * @return Previous Page
     */
    @Override
    public Page previousPage(final Page page, 
                                     final TwilioRestClient client) {
        Request request = new Request(
            HttpMethod.GET,
            page.getPreviousPageUrl(
                Domains.API.toString(),
                client.getRegion()
            )
        );
        return pageForRequest(client, request);
    }

    /**
     * Generate a Page of Mobile Resources for a given request.
     * 
     * @param client TwilioRestClient with which to make the request
     * @param request Request to generate a page for
     * @return Page for the Request
     */
    private Page pageForRequest(final TwilioRestClient client, final Request request) {
        Response response = client.request(request);

        if (response == null) {
            throw new ApiConnectionException("Mobile read 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 Page.fromJson(
            "available_phone_numbers",
            response.getContent(),
            Mobile.class,
            client.getObjectMapper()
        );
    }

    /**
     * Add the requested query string arguments to the Request.
     * 
     * @param request Request to add query string arguments to
     */
    private void addQueryParams(final Request request) {
        if (areaCode != null) {
            request.addQueryParam("AreaCode", areaCode.toString());
        }

        if (contains != null) {
            request.addQueryParam("Contains", contains);
        }

        if (smsEnabled != null) {
            request.addQueryParam("SmsEnabled", smsEnabled.toString());
        }

        if (mmsEnabled != null) {
            request.addQueryParam("MmsEnabled", mmsEnabled.toString());
        }

        if (voiceEnabled != null) {
            request.addQueryParam("VoiceEnabled", voiceEnabled.toString());
        }

        if (excludeAllAddressRequired != null) {
            request.addQueryParam("ExcludeAllAddressRequired", excludeAllAddressRequired.toString());
        }

        if (excludeLocalAddressRequired != null) {
            request.addQueryParam("ExcludeLocalAddressRequired", excludeLocalAddressRequired.toString());
        }

        if (excludeForeignAddressRequired != null) {
            request.addQueryParam("ExcludeForeignAddressRequired", excludeForeignAddressRequired.toString());
        }

        if (beta != null) {
            request.addQueryParam("Beta", beta.toString());
        }

        if (nearNumber != null) {
            request.addQueryParam("NearNumber", nearNumber.toString());
        }

        if (nearLatLong != null) {
            request.addQueryParam("NearLatLong", nearLatLong);
        }

        if (distance != null) {
            request.addQueryParam("Distance", distance.toString());
        }

        if (inPostalCode != null) {
            request.addQueryParam("InPostalCode", inPostalCode);
        }

        if (inRegion != null) {
            request.addQueryParam("InRegion", inRegion);
        }

        if (inRateCenter != null) {
            request.addQueryParam("InRateCenter", inRateCenter);
        }

        if (inLata != null) {
            request.addQueryParam("InLata", inLata);
        }

        if (inLocality != null) {
            request.addQueryParam("InLocality", inLocality);
        }

        if (faxEnabled != null) {
            request.addQueryParam("FaxEnabled", faxEnabled.toString());
        }

        if (getPageSize() != null) {
            request.addQueryParam("PageSize", Integer.toString(getPageSize()));
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy