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

com.asana.requests.ItemRequest Maven / Gradle / Ivy

There is a newer version: 1.0.0
Show newest version
package com.asana.requests;

import com.asana.Json;
import com.asana.models.ResultBody;
import com.asana.resources.Resource;
import com.google.api.client.http.HttpContent;
import com.google.api.client.http.HttpHeaders;
import com.google.api.client.http.HttpResponse;
import com.google.common.reflect.TypeParameter;
import com.google.common.reflect.TypeToken;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.Map;

public class ItemRequest extends Request {
    public ItemRequest(Resource resource, Class elementClass, String path, String method) {
        super(resource, elementClass, path, method);
    }

    /**
     * Executes the request, returning the requested single item
     *
     * @return requested item
     * @throws IOException
     */
    public T execute() throws IOException {
        ResultBody result = this.executeRaw();
        return result.data;
    }

    /**
     * Executes the request, returning the full response body
     *
     * @return Body containing the "data" object and other metadata
     * @throws IOException
     */
    public ResultBody executeRaw() throws IOException {
        HttpResponse response = this.client.request(this);

        if (this.client.logAsanaChangeWarnings) {
            HttpHeaders reqHeaders = new HttpHeaders();
            reqHeaders.putAll(this.client.headers);
            handleAsanaChangeHeader(reqHeaders, response.getHeaders());
        }

        return Json.getInstance().fromJson(
                new BufferedReader(new InputStreamReader(response.getContent(), StandardCharsets.UTF_8)),
                new TypeToken>() {
                }.where(
                        new TypeParameter() {
                        },
                        this.elementClass
                ).getType()
        );
    }

    public ItemRequest query(Map object) {
        return (ItemRequest) super.query(object);
    }

    public ItemRequest query(String key, Object value) {
        return (ItemRequest) super.query(key, value);
    }

    public ItemRequest data(HttpContent content) {
        return (ItemRequest) super.data(content);
    }

    public ItemRequest data(Map object) {
        return (ItemRequest) super.data(object);
    }

    public ItemRequest data(String key, Object value) {
        return (ItemRequest) super.data(key, value);
    }

    public ItemRequest option(String key, Object value) {
        return (ItemRequest) super.option(key, value);
    }

    public ItemRequest header(String key, String value) {
        return (ItemRequest) super.header(key, value);
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy