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

com.smartling.api.sdk.file.response.Response Maven / Gradle / Ivy

There is a newer version: 4.0.25
Show newest version
package com.smartling.api.sdk.file.response;

import com.smartling.api.sdk.exceptions.SmartlingApiException;
import com.smartling.web.api.v2.ResponseCode;
import com.smartling.web.api.v2.ResponseData;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Response {
    private ResponseCode code;
    private List errors;
    private T data;

    public Response() {
    }

    public Response(ResponseCode code, List errors, T data) {
        this.code = code;
        this.errors = errors;
        this.data = data;
    }

    public ResponseCode getCode() {
        return this.code;
    }

    public void setCode(ResponseCode code) {
        this.code = code;
    }

    public List getErrors() {
        return this.errors;
    }

    public void setErrors(List errors) {
        this.errors = errors;
    }

    public T getData()
    {
        return data;
    }

    public T retrieveData() throws SmartlingApiException
    {
        if (this.code != null && this.code != ResponseCode.SUCCESS && this.code != ResponseCode.ACCEPTED) {
            List errors = this.errors != null ? this.errors : Collections.emptyList();

            List messages = new ArrayList<>(errors.size());
            for(Error error : errors)
            {
                messages.add(error.toString());
            }

            throw new SmartlingApiException(code.toString()+ '\n' + StringUtils.join(messages, '\n'), errors);
        }
        return this.data;
    }

    public void setData(T data) {
        this.data = data;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy