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

com.twilio.rest.lookups.v1.PhoneNumberFetcher Maven / Gradle / Ivy

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

package com.twilio.rest.lookups.v1;

import com.twilio.base.Fetcher;
import com.twilio.converter.PrefixedCollapsibleMap;
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;

import java.util.List;
import java.util.Map;

public class PhoneNumberFetcher extends Fetcher {
    private final com.twilio.type.PhoneNumber pathPhoneNumber;
    private String countryCode;
    private List type;
    private List addOns;
    private Map addOnsData;

    /**
     * Construct a new PhoneNumberFetcher.
     * 
     * @param pathPhoneNumber The phone_number
     */
    public PhoneNumberFetcher(final com.twilio.type.PhoneNumber pathPhoneNumber) {
        this.pathPhoneNumber = pathPhoneNumber;
    }

    /**
     * Optional [ISO country code](http://en.wikipedia.org/wiki/ISO_3166-1_alpha-2)
     * of the phone number. This is used to specify the country when the number is
     * provided in a national format..
     * 
     * @param countryCode Optional ISO country code of the phone number.
     * @return this
     */
    public PhoneNumberFetcher setCountryCode(final String countryCode) {
        this.countryCode = countryCode;
        return this;
    }

    /**
     * Indicates the type of information you would like returned with your request.
     * Possible values are `carrier` or `caller-name`. If not specified, the default
     * is null.  Carrier information costs $0.005 per phone number looked up. 
     * Caller Name information costs $0.01 per phone number looked up, and is
     * currently ONLY available in the US.  You can retrieve both types of
     * information by including two `Type` arguments or making two separate
     * requests..
     * 
     * @param type Indicates the type of information you would like returned with
     *             your request.
     * @return this
     */
    public PhoneNumberFetcher setType(final List type) {
        this.type = type;
        return this;
    }

    /**
     * Indicates the type of information you would like returned with your request.
     * Possible values are `carrier` or `caller-name`. If not specified, the default
     * is null.  Carrier information costs $0.005 per phone number looked up. 
     * Caller Name information costs $0.01 per phone number looked up, and is
     * currently ONLY available in the US.  You can retrieve both types of
     * information by including two `Type` arguments or making two separate
     * requests..
     * 
     * @param type Indicates the type of information you would like returned with
     *             your request.
     * @return this
     */
    public PhoneNumberFetcher setType(final String type) {
        return setType(Promoter.listOfOne(type));
    }

    /**
     * Indicates the particular Add-on you would like to use to get more
     * information. Possible values are the *Add-on Unique Names* of Add-ons
     * installed on your account. You can specify multiple instances of this
     * parameter to invoke different Add-ons. See [Add-ons
     * documentation](https://www.twilio.com/docs/api/addons) for information on
     * installing Add-ons. Add-on pricing is available in your list of Installed
     * Add-ons in the Console..
     * 
     * @param addOns Indicates the particular Add-on you would like to use to get
     *               more information.
     * @return this
     */
    public PhoneNumberFetcher setAddOns(final List addOns) {
        this.addOns = addOns;
        return this;
    }

    /**
     * Indicates the particular Add-on you would like to use to get more
     * information. Possible values are the *Add-on Unique Names* of Add-ons
     * installed on your account. You can specify multiple instances of this
     * parameter to invoke different Add-ons. See [Add-ons
     * documentation](https://www.twilio.com/docs/api/addons) for information on
     * installing Add-ons. Add-on pricing is available in your list of Installed
     * Add-ons in the Console..
     * 
     * @param addOns Indicates the particular Add-on you would like to use to get
     *               more information.
     * @return this
     */
    public PhoneNumberFetcher setAddOns(final String addOns) {
        return setAddOns(Promoter.listOfOne(addOns));
    }

    /**
     * The add_ons_data.
     * 
     * @param addOnsData The add_ons_data
     * @return this
     */
    public PhoneNumberFetcher setAddOnsData(final Map addOnsData) {
        this.addOnsData = addOnsData;
        return this;
    }

    /**
     * Make the request to the Twilio API to perform the fetch.
     * 
     * @param client TwilioRestClient with which to make the request
     * @return Fetched PhoneNumber
     */
    @Override
    @SuppressWarnings("checkstyle:linelength")
    public PhoneNumber fetch(final TwilioRestClient client) {
        Request request = new Request(
            HttpMethod.GET,
            Domains.LOOKUPS.toString(),
            "/v1/PhoneNumbers/" + this.pathPhoneNumber + "",
            client.getRegion()
        );

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

        if (response == null) {
            throw new ApiConnectionException("PhoneNumber fetch 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 PhoneNumber.fromJson(response.getStream(), 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 (countryCode != null) {
            request.addQueryParam("CountryCode", countryCode);
        }

        if (type != null) {
            for (String prop : type) {
                request.addQueryParam("Type", prop);
            }
        }

        if (addOns != null) {
            for (String prop : addOns) {
                request.addQueryParam("AddOns", prop);
            }
        }

        if (addOnsData != null) {
            Map params = PrefixedCollapsibleMap.serialize(addOnsData, "AddOns");
            for (Map.Entry entry : params.entrySet()) {
                request.addQueryParam(entry.getKey(), entry.getValue());
            }
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy