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

org.sourcelab.github.client.request.Request Maven / Gradle / Ivy

The newest version!

package org.sourcelab.github.client.request;

import org.sourcelab.github.client.exception.RequestParsingException;
import org.sourcelab.github.client.exception.ResponseParsingException;
import org.sourcelab.github.client.http.HttpResult;
import org.sourcelab.github.client.response.parser.ResponseParser;

import java.io.IOException;
import java.util.Collections;

/**
 * Defines an API request.
 * @param  The parsed return type representing the response.
 */
public interface Request {
    /**
     * Which HTTP Method to make the request using.  GET, POST, PUT, etc...
     * @return Which HTTP Method to make the request using.  GET, POST, PUT, etc...
     */
    HttpMethod getMethod();

    /**
     * REST Endpoint Path to request.  Example "/v2/access-token"
     * @return REST Endpoint Path to request.  Example "/v2/access-token"
     */
    String getPath();

    /**
     * KeyValue pairs of Request Parameters.
     * @return RequestParameters associated with request.
     */
    default RequestParameters getRequestParameters() {
        return new RequestParameters(Collections.emptyList());
    }

    /**
     * The Request body value.
     * @return null if no request body is needed, or the request body as a String.
     * @throws RequestParsingException if unable to generate request body due to some error.
     */
    default String getRequestBody() throws RequestParsingException {
        return null;
    }

    /**
     * Defines how to parse the APIs response into a concrete object.
     * @return Defines how to parse the APIs response into a concrete object.
     */
    ResponseParser getResponseParser();

    /**
     * Parses the API's response into a concrete object.
     *
     * @param result The API's response.
     * @return Parsed response object.
     * @throws ResponseParsingException if unable to properly parse a response from the API.
     */
    default T parseResponse(final HttpResult result) {
        try {
            return getResponseParser().parseResponse(result);
        } catch (final IOException parseException) {
            final String message = "Unable to parse response from API: " + parseException.getMessage() + "\n"
                    + "Response From API: \n"
                    + result.getContent();
            throw new ResponseParsingException(message, parseException);
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy