001/*
002 *   Copyright 2024 Vonage
003 *
004 *   Licensed under the Apache License, Version 2.0 (the "License");
005 *   you may not use this file except in compliance with the License.
006 *   You may obtain a copy of the License at
007 *
008 *        http://www.apache.org/licenses/LICENSE-2.0
009 *
010 *   Unless required by applicable law or agreed to in writing, software
011 *   distributed under the License is distributed on an "AS IS" BASIS,
012 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *   See the License for the specific language governing permissions and
014 *   limitations under the License.
015 */
016package com.vonage.client.verify;
017
018import com.fasterxml.jackson.annotation.JsonCreator;
019import com.fasterxml.jackson.annotation.JsonProperty;
020import com.fasterxml.jackson.databind.ObjectMapper;
021import com.fasterxml.jackson.databind.module.SimpleModule;
022import com.vonage.client.Jsonable;
023import com.vonage.client.JsonableBaseObject;
024import com.vonage.client.VonageResponseParseException;
025import java.io.IOException;
026import java.text.SimpleDateFormat;
027import java.util.List;
028
029public class SearchVerifyResponse extends JsonableBaseObject {
030    private VerifyStatus status;
031    private List<VerifyDetails> verificationRequests;
032    private String errorText;
033
034    @JsonCreator
035    SearchVerifyResponse() {
036        this(null);
037    }
038
039    SearchVerifyResponse(List<VerifyDetails> verificationRequests) {
040        status = VerifyStatus.OK;
041        this.verificationRequests = verificationRequests;
042    }
043
044    SearchVerifyResponse(VerifyStatus status, String errorText) {
045        this.status = status;
046        this.errorText = errorText;
047    }
048
049    @JsonProperty("status")
050    public VerifyStatus getStatus() {
051        return status;
052    }
053
054    @JsonProperty("verification_requests")
055    public List<VerifyDetails> getVerificationRequests() {
056        return verificationRequests;
057    }
058
059    /**
060     * @return If status is not SUCCESS, this message explains the issue encountered.
061     */
062    @JsonProperty("error_text")
063    public String getErrorText() {
064        return errorText;
065    }
066
067    @Override
068    public void updateFromJson(String json) {
069        try {
070            ObjectMapper mapper = Jsonable.createDefaultObjectMapper();
071            mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
072            SimpleModule module = new SimpleModule();
073            module.addDeserializer(SearchVerifyResponse.class, new SearchVerifyResponseDeserializer());
074            mapper.registerModule(module);
075            SearchVerifyResponse parsed = mapper.readValue(json, SearchVerifyResponse.class);
076            status = parsed.status;
077            verificationRequests = parsed.verificationRequests;
078            errorText = parsed.errorText;
079        }
080        catch (IOException jme) {
081            throw new VonageResponseParseException("Failed to produce SearchVerifyResponse from json.", jme);
082        }
083    }
084
085    public static SearchVerifyResponse fromJson(String json) {
086        return Jsonable.fromJson(json);
087    }
088}