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

com.heroku.api.request.key.KeyList Maven / Gradle / Ivy

package com.heroku.api.request.key;

import com.heroku.api.Heroku;
import com.heroku.api.Key;
import com.heroku.api.exception.RequestFailedException;
import com.heroku.api.http.Http;
import com.heroku.api.parser.XmlParser;
import com.heroku.api.request.Request;

import javax.xml.bind.annotation.XmlRootElement;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import static com.heroku.api.http.HttpUtil.noBody;

/**
 * TODO: Javadoc
 *
 * @author Naaman Newbold
 */
public class KeyList implements Request> {

    @Override
    public Http.Method getHttpMethod() {
        return Http.Method.GET;
    }

    @Override
    public String getEndpoint() {
        return Heroku.Resource.Keys.value;
    }

    @Override
    public boolean hasBody() {
        return false;
    }

    @Override
    public String getBody() {
        throw noBody();
    }

    @Override
    public Http.Accept getResponseType() {
        return Http.Accept.XML;
    }

    @Override
    public Map getHeaders() {
        return Collections.emptyMap();
    }

    @Override
    public List getResponse(byte[] bytes, int status) {
        if (status == Http.Status.OK.statusCode) {
            if (new String(bytes).contains("nil-classes")) {
                return new ArrayList();
            }
            Keys keys = new XmlParser().parse(bytes, Keys.class);
            return keys.getKey();
        } else {
            throw new RequestFailedException("Unable to list keys.", status, bytes);
        }
    }

    @XmlRootElement
    static class Keys {
        List keys;

        public List getKey() {
            return keys;
        }

        public void setKey(List keylist) {
            this.keys = keylist;
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy