001package com.box.sdk;
002
003import com.eclipsesource.json.JsonObject;
004import com.eclipsesource.json.JsonValue;
005import java.text.ParseException;
006import java.util.Date;
007
008
009/**
010 * AI response to a user request.
011 */
012public class BoxAIResponse extends BoxJSONObject {
013    private String answer;
014    private String completionReason;
015    private Date createdAt;
016
017    /**
018     * Constructs a BoxAIResponse object.
019     */
020    public BoxAIResponse(String answer, String completionReason, Date createdAt) {
021        super();
022        this.answer = answer;
023        this.completionReason = completionReason;
024        this.createdAt = createdAt;
025    }
026
027    /**
028     * Constructs a BoxAIResponse from a JSON string.
029     *
030     * @param json the JSON encoded upload email.
031     */
032    public BoxAIResponse(String json) {
033        super(json);
034    }
035
036    /**
037     * Constructs an BoxAIResponse object using an already parsed JSON object.
038     *
039     * @param jsonObject the parsed JSON object.
040     */
041    BoxAIResponse(JsonObject jsonObject) {
042        super(jsonObject);
043    }
044
045    /**
046     * Gets the answer of the AI.
047     *
048     * @return the answer of the AI.
049     */
050    public String getAnswer() {
051        return answer;
052    }
053
054    /**
055     * Gets reason the response finishes.
056     *
057     * @return the reason the response finishes.
058     */
059    public String getCompletionReason() {
060        return completionReason;
061    }
062
063    /**
064     * Gets the ISO date formatted timestamp of when the answer to the prompt was created.
065     *
066     * @return The ISO date formatted timestamp of when the answer to the prompt was created.
067     */
068    public Date getCreatedAt() {
069        return createdAt;
070    }
071
072
073    /**
074     * {@inheritDoc}
075     */
076    @Override
077    void parseJSONMember(JsonObject.Member member) {
078        JsonValue value = member.getValue();
079        String memberName = member.getName();
080        try {
081            if (memberName.equals("answer")) {
082                this.answer = value.asString();
083            } else if (memberName.equals("completion_reason")) {
084                this.completionReason = value.asString();
085            } else if (memberName.equals("created_at")) {
086                this.createdAt = BoxDateFormat.parse(value.asString());
087            }
088        } catch (ParseException e) {
089            assert false : "A ParseException indicates a bug in the SDK.";
090        }
091    }
092}