001/*
002 * Copyright (c) 2011-2018 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.verify;
023
024import com.nexmo.client.HttpWrapper;
025import com.nexmo.client.NexmoClientException;
026
027import java.io.IOException;
028import java.util.ArrayList;
029import java.util.List;
030
031public class SearchEndpoint {
032    private SearchMethod searchMethod;
033
034    SearchEndpoint(HttpWrapper httpWrapper) {
035        this.searchMethod = new SearchMethod(httpWrapper);
036    }
037
038    SearchResult[] search(String... requestIds) throws IOException, NexmoClientException {
039        return translateFromSearchVerifyResponse(this.searchMethod.execute(new SearchRequest(requestIds)));
040    }
041
042    private SearchResult[] translateFromSearchVerifyResponse(SearchVerifyResponse response) {
043        // TODO: Remove in 4.0
044        List<SearchResult> resultList = new ArrayList<>();
045
046        if (response.getStatus() != VerifyStatus.OK) {
047            resultList.add(new SearchResult(response.getStatus().getVerifyStatus(),
048                    response.getErrorText(),
049                    response.getStatus().isTemporaryError()));
050        } else {
051            for (VerifyDetails details : response.getVerificationRequests()) {
052                resultList.add(new SearchResult(response.getStatus().getVerifyStatus(),
053                        details.getRequestId(),
054                        details.getAccountId(),
055                        translateFromVerifyDetailsStatus(details.getStatus()),
056                        details.getNumber(),
057                        details.getPrice() != null ? details.getPrice().floatValue() : 0,
058                        details.getCurrency(),
059                        details.getSenderId(),
060                        details.getDateSubmitted(),
061                        details.getDateFinalized(),
062                        details.getFirstEventDate(),
063                        details.getLastEventDate(),
064                        translateFromVerifyCheck(details.getChecks()),
065                        response.getErrorText(),
066                        false));
067            }
068        }
069
070        return resultList.toArray(new SearchResult[0]);
071    }
072
073    private SearchResult.VerificationStatus translateFromVerifyDetailsStatus(VerifyDetails.Status status) {
074        // TODO: Remove in 4.0
075        // This operates in the same way the XML endpoint does without logging the error.
076        try {
077            return status != null ? SearchResult.VerificationStatus.valueOf(status.name()) : null;
078        } catch (IllegalArgumentException e) {
079            return null;
080        }
081    }
082
083    private List<SearchResult.VerifyCheck> translateFromVerifyCheck(List<VerifyCheck> checks) {
084        // TODO: Remove in 4.0
085        List<SearchResult.VerifyCheck> resultChecks = new ArrayList<>();
086
087        for (VerifyCheck check : checks) {
088            resultChecks.add(new SearchResult.VerifyCheck(check.getDate(),
089                    check.getCode(),
090                    SearchResult.VerifyCheck.Status.valueOf(check.getStatus().name()),
091                    check.getIpAddress()));
092        }
093
094        return resultChecks;
095    }
096}