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

com.bandwidth.voice.models.DynamicResponse Maven / Gradle / Ivy

Go to download

The official client SDK for Bandwidth's Voice, Messaging, MFA, and WebRTC APIs

There is a newer version: 12.0.1
Show newest version
/*
 * BandwidthLib
 *
 * This file was automatically generated by APIMATIC v3.0 ( https://www.apimatic.io ).
 */

package com.bandwidth.voice.models;

import com.bandwidth.ApiHelper;
import com.bandwidth.http.Headers;
import com.bandwidth.http.response.HttpResponse;
import com.bandwidth.http.response.HttpStringResponse;
import java.io.IOException;
import java.io.InputStream;
import java.text.ParseException;
import java.util.Map;

/**
 * Represents dynamic response returned by an API call.
 * Allows user to lazily parse the response as a primitive
 * or a more complex type using parse(). 
 */
public class DynamicResponse {
    private HttpResponse response;
    private String responseString;

    /**
     * Instantiate class.
     * @param responseBody The object of HttpResponse
     */
    public DynamicResponse(HttpResponse responseBody) {
        this.response = responseBody;
    }
    
    /**
     * Parse response as instance of class cls.
     * @param  The type of class to be parsed
     * @param cls Class to be parsed
     * @return Object of type T
     * @throws ParseException Signals if a parse exception occured
     */
    public  T parse(Class cls) throws ParseException {
        try {
            return ApiHelper.deserialize(getResponseString(), cls);
        } catch (Exception e) {
            throw new java.text.ParseException("Could not deserialize dynamic content as given type", 0);
        }
    }

    /**
     * Parse response as boolean.
     * @return Parsed value
     * @throws ParseException Signals if a parse exception occured
     */
    public boolean parseAsBoolean() throws ParseException {
        return this.parse(Boolean.class);
    }

    /**
     * Parse response as boolean.
     * @return Parsed value
     * @throws ParseException Signals if a parse exception occured
     */
    public byte parseAsByte() throws ParseException {
        return this.parse(Byte.class);
    }

    /**
     * Parse response as character.
     * @return Parsed value
     * @throws ParseException Signals if a parse exception occured
     */
    public char parseAsCharacter() throws ParseException {
        return this.parse(Character.class);
    }

    /**
     * Parse response as float.
     * @return Parsed value
     * @throws ParseException Signals if a parse exception occured
     */
    public float parseAsFloat() throws ParseException {
        return this.parse(Float.class);
    }

    /**
     * Parse response as integer.
     * @return Parsed value
     * @throws ParseException Signals if a parse exception occured
     */
    public int parseAsInteger() throws ParseException {
        return this.parse(Integer.class);
    }

    /**
     * Parse response as long.
     * @return Parsed value
     * @throws ParseException Signals if a parse exception occured
     */
    public long parseAsLong() throws ParseException {
        return this.parse(Long.class);
    }

    /**
     * Parse response as short.
     * @return Parsed value
     * @throws ParseException Signals if a parse exception occured
     */
    public short parseAsShort() throws ParseException {
        return this.parse(Short.class);
    }

    /**
     * Parse response as double.
     * @return Parsed value
     * @throws ParseException Signals if a parse exception occured
     */
    public double parseAsDouble() throws ParseException {
        return this.parse(Double.class);
    }
    
    /**
     * Parse response as string.
     * @return Parsed value
     * @throws ParseException Signals if a parse exception occured
     */
    public String parseAsString() throws ParseException {
        try {
            return getResponseString();
        } catch (Throwable e) {
            throw new java.text.ParseException("Could not deserialize dynamic content as given type", 0);
        }
    }

    /**
     * Parse response as a map of keys and values.
     * @return Parsed map
     * @throws ParseException Signals if a parse exception occured
     */
    public Map parseAsDictionary() throws ParseException {
        try {
            return ApiHelper.deserialize(getResponseString());
        } catch (IOException e) {
            throw new java.text.ParseException("Could not deserialize dynamic content as given type", 0);
        }
    }
    
    /**
     * Get the raw stream for the response body.
     * @return Raw body
     */
    public InputStream getRawBody() {
        return response.getRawBody();
    }
    
    /**
     * Get response headers for the HTTP response.
     * @return Headers
     */
    public Headers getHeaders() {
        return response.getHeaders();
    }

    /**
     * Get response as string.
     * @return The Response String
     */
    private String getResponseString() {
        if (responseString == null) {
            responseString = ((HttpStringResponse) response).getBody();
        }
        return responseString;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy