001/*
002 * Copyright (c) 2011-2017 Nexmo Inc
003 *
004 * Permission is hereby granted, free of charge, to any person obtaining a copy
005 * of this software and associated documentation files (the "Software"), to deal
006 * in the Software without restriction, including without limitation the rights
007 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008 * copies of the Software, and to permit persons to whom the Software is
009 * furnished to do so, subject to the following conditions:
010 *
011 * The above copyright notice and this permission notice shall be included in
012 * all copies or substantial portions of the Software.
013 *
014 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
020 * THE SOFTWARE.
021 */
022package com.nexmo.client.insight;
023
024public class AdvancedInsightRequest extends BaseInsightRequest {
025    private boolean async;
026    private String callback;
027
028    private AdvancedInsightRequest(Builder builder) {
029        this.number = builder.number;
030        this.country = builder.country;
031        this.cnam = builder.cnam;
032        this.ipAddress = builder.ipAddress;
033        this.async = builder.async;
034        this.callback = builder.callback;
035    }
036
037    public static Builder builder(String number) {
038        return new Builder(number);
039    }
040
041    public Boolean getCnam() {
042        return cnam;
043    }
044
045    public String getIpAddress() {
046        return ipAddress;
047    }
048
049    public boolean isAsync() {
050        return async;
051    }
052
053    public String getCallback() {
054        return callback;
055    }
056
057    /**
058     * Construct a AdvancedInsightRequest with a number.
059     *
060     * @param number A single phone number that you need insight about in national or international format.
061     *
062     * @return A new {@link AdvancedInsightRequest} object.
063     */
064    public static AdvancedInsightRequest withNumber(String number) {
065        return new Builder(number).build();
066    }
067
068    /**
069     * Construct a AdvancedInsightRequest with a number and country.
070     *
071     * @param number  A single phone number that you need insight about in national or international format.
072     * @param country If a number does not have a country code or it is uncertain, set the two-character country code.
073     *
074     * @return A new {@link AdvancedInsightRequest} object.
075     */
076    public static AdvancedInsightRequest withNumberAndCountry(String number, String country) {
077        return new Builder(number).country(country).build();
078    }
079
080    public static class Builder {
081        protected String number;
082        protected String country;
083        protected Boolean cnam;
084        protected String ipAddress;
085        protected boolean async;
086        protected String callback;
087
088        /**
089         * @param number A single phone number that you need insight about in national or international format.
090         */
091        public Builder(String number) {
092            this.number = number;
093        }
094
095        /**
096         * @param number A single phone number that you need insight about in national or international format.
097         *
098         * @return The {@link Builder} to keep building.
099         */
100        public Builder number(String number) {
101            this.number = number;
102            return this;
103        }
104
105        /**
106         * @param country If a number does not have a country code or it is uncertain, set the two-character country
107         *                code.
108         *
109         * @return The {@link Builder} to keep building.
110         */
111        public Builder country(String country) {
112            this.country = country;
113            return this;
114        }
115
116        /**
117         * @param cnam Indicates if the name of the person who owns the phone number should also be looked up and
118         *             returned. Set to true to receive phone number owner name in the response. This is only available
119         *             for US numbers and incurs an additional charge.
120         *
121         * @return The {@link Builder} to keep building.
122         */
123        public Builder cnam(Boolean cnam) {
124            this.cnam = cnam;
125            return this;
126        }
127
128        /**
129         * @param ipAddress The IP address of the user. If supplied, we will compare this to the country the user's
130         *                  phone is located in and return an error if it does not match.
131         *
132         * @return The {@link Builder} to keep building.
133         */
134        public Builder ipAddress(String ipAddress) {
135            this.ipAddress = ipAddress;
136            return this;
137        }
138
139        /**
140         * @param async True if the call should be done asynchronously. When setting this value to true, the {@link
141         *              Builder#callback(String)} parameter must also be set.
142         *
143         * @return The {@link Builder} to keep building.
144         */
145        public Builder async(boolean async) {
146            this.async = async;
147            return this;
148        }
149
150        /**
151         * @param url The URL that Nexmo will send a request to when the insight lookup is finished.
152         *
153         * @return The {@link Builder} to keep building.
154         */
155        public Builder callback(String url) {
156            this.callback = url;
157            return this;
158        }
159
160        /**
161         * @return A new {@link AdvancedInsightRequest} object from the stored builder options.
162         */
163        public AdvancedInsightRequest build() {
164            if (this.async && (this.callback == null || this.callback.isEmpty())) {
165                throw new IllegalStateException("You must define a callback url when using asyncronous insights.");
166            }
167            return new AdvancedInsightRequest(this);
168        }
169    }
170}