mirror of
https://github.com/jlengrand/github-api.git
synced 2026-03-26 08:21:23 +00:00
162 lines
7.9 KiB
HTML
162 lines
7.9 KiB
HTML
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" lang="en"><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/><link rel="stylesheet" href="../jacoco-resources/report.css" type="text/css"/><link rel="shortcut icon" href="../jacoco-resources/report.gif" type="image/gif"/><title>Requester.java</title><link rel="stylesheet" href="../jacoco-resources/prettify.css" type="text/css"/><script type="text/javascript" src="../jacoco-resources/prettify.js"></script></head><body onload="window['PR_TAB_WIDTH']=4;prettyPrint()"><div class="breadcrumb" id="breadcrumb"><span class="info"><a href="../jacoco-sessions.html" class="el_session">Sessions</a></span><a href="../index.html" class="el_report">GitHub API for Java</a> > <a href="index.source.html" class="el_package">org.kohsuke.github</a> > <span class="el_source">Requester.java</span></div><h1>Requester.java</h1><pre class="source lang-java linenums">/*
|
|
* The MIT License
|
|
*
|
|
* Copyright (c) 2010, Kohsuke Kawaguchi
|
|
*
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
* in the Software without restriction, including without limitation the rights
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
* furnished to do so, subject to the following conditions:
|
|
*
|
|
* The above copyright notice and this permission notice shall be included in
|
|
* all copies or substantial portions of the Software.
|
|
*
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
* THE SOFTWARE.
|
|
*/
|
|
package org.kohsuke.github;
|
|
|
|
import edu.umd.cs.findbugs.annotations.NonNull;
|
|
import org.apache.commons.io.IOUtils;
|
|
import org.kohsuke.github.function.InputStreamFunction;
|
|
|
|
import java.io.ByteArrayInputStream;
|
|
import java.io.IOException;
|
|
import java.io.InputStream;
|
|
import java.net.MalformedURLException;
|
|
import java.util.Iterator;
|
|
import java.util.function.Consumer;
|
|
|
|
import javax.annotation.Nonnull;
|
|
|
|
/**
|
|
* A thin helper for {@link GitHubRequest.Builder} that includes {@link GitHubClient}.
|
|
*
|
|
* @author Kohsuke Kawaguchi
|
|
*/
|
|
class Requester extends GitHubRequest.Builder<Requester> {
|
|
/* private */ final transient GitHubClient client;
|
|
|
|
<span class="fc" id="L47"> Requester(GitHubClient client) {</span>
|
|
<span class="fc" id="L48"> this.client = client;</span>
|
|
<span class="fc" id="L49"> this.withApiUrl(client.getApiUrl());</span>
|
|
<span class="fc" id="L50"> }</span>
|
|
|
|
/**
|
|
* Sends a request to the specified URL and checks that it is sucessful.
|
|
*
|
|
* @throws IOException
|
|
* the io exception
|
|
*/
|
|
public void send() throws IOException {
|
|
// Send expects there to be some body response, but doesn't care what it is.
|
|
// If there isn't a body, this will throw.
|
|
<span class="fc" id="L61"> client.sendRequest(this, (responseInfo) -> responseInfo.getBodyAsString());</span>
|
|
<span class="fc" id="L62"> }</span>
|
|
|
|
/**
|
|
* Sends a request and parses the response into the given type via databinding.
|
|
*
|
|
* @param <T>
|
|
* the type parameter
|
|
* @param type
|
|
* the type
|
|
* @return an instance of {@link T}
|
|
* @throws IOException
|
|
* if the server returns 4xx/5xx responses.
|
|
*/
|
|
public <T> T fetch(@Nonnull Class<T> type) throws IOException {
|
|
<span class="fc" id="L76"> return client.sendRequest(this, (responseInfo) -> GitHubResponse.parseBody(responseInfo, type)).body();</span>
|
|
}
|
|
|
|
/**
|
|
* Like {@link #fetch(Class)} but updates an existing object instead of creating a new instance.
|
|
*
|
|
* @param <T>
|
|
* the type parameter
|
|
* @param existingInstance
|
|
* the existing instance
|
|
* @return the updated instance
|
|
* @throws IOException
|
|
* the io exception
|
|
*/
|
|
public <T> T fetchInto(@Nonnull T existingInstance) throws IOException {
|
|
<span class="fc" id="L91"> return client.sendRequest(this, (responseInfo) -> GitHubResponse.parseBody(responseInfo, existingInstance))</span>
|
|
<span class="fc" id="L92"> .body();</span>
|
|
}
|
|
|
|
/**
|
|
* Makes a request and just obtains the HTTP status code. Method does not throw exceptions for many status codes
|
|
* that would otherwise throw.
|
|
*
|
|
* @return the int
|
|
* @throws IOException
|
|
* the io exception
|
|
*/
|
|
public int fetchHttpStatusCode() throws IOException {
|
|
<span class="fc" id="L104"> return client.sendRequest(build(), null).statusCode();</span>
|
|
}
|
|
|
|
/**
|
|
* Response input stream. There are scenarios where direct stream reading is needed, however it is better to use
|
|
* {@link #fetch(Class)} where possible.
|
|
*
|
|
* @throws IOException
|
|
* the io exception
|
|
*/
|
|
public <T> T fetchStream(@Nonnull InputStreamFunction<T> handler) throws IOException {
|
|
<span class="fc" id="L115"> return client.sendRequest(this, (responseInfo) -> handler.apply(responseInfo.bodyStream())).body();</span>
|
|
}
|
|
|
|
/**
|
|
* Helper function to make it easy to pull streams.
|
|
*
|
|
* Copies an input stream to an in-memory input stream. The performance on this is not great but
|
|
* {@link GitHubResponse.ResponseInfo#bodyStream()} is closed at the end of every call to
|
|
* {@link GitHubClient#sendRequest(GitHubRequest, GitHubResponse.BodyHandler)}, so any reads to the original input
|
|
* stream must be completed before then. There are a number of deprecated methods that return {@link InputStream}.
|
|
* This method keeps all of them using the same code path.
|
|
*
|
|
* @param inputStream
|
|
* the input stream to be copied
|
|
* @return an in-memory copy of the passed input stream
|
|
* @throws IOException
|
|
* if an error occurs while copying the stream
|
|
*/
|
|
@NonNull
|
|
public static InputStream copyInputStream(InputStream inputStream) throws IOException {
|
|
<span class="fc" id="L135"> return new ByteArrayInputStream(IOUtils.toByteArray(inputStream));</span>
|
|
}
|
|
|
|
/**
|
|
* Creates {@link PagedIterable <R>} from this builder using the provided {@link Consumer<R>}.
|
|
* <p>
|
|
* This method and the {@link PagedIterable <R>} do not actually begin fetching data until {@link Iterator#next()}
|
|
* or {@link Iterator#hasNext()} are called.
|
|
* </p>
|
|
*
|
|
* @param type
|
|
* the type of the pages to retrieve.
|
|
* @param itemInitializer
|
|
* the consumer to execute on each paged item retrieved.
|
|
* @param <R>
|
|
* the element type for the pages returned from
|
|
* @return the {@link PagedIterable} for this builder.
|
|
*/
|
|
public <R> PagedIterable<R> toIterable(Class<R[]> type, Consumer<R> itemInitializer) {
|
|
try {
|
|
<span class="fc" id="L155"> return new GitHubPageContentsIterable<>(client, build(), type, itemInitializer);</span>
|
|
<span class="nc" id="L156"> } catch (MalformedURLException e) {</span>
|
|
<span class="nc" id="L157"> throw new GHException(e.getMessage(), e);</span>
|
|
}
|
|
|
|
}
|
|
}
|
|
</pre><div class="footer"><span class="right">Created with <a href="http://www.jacoco.org/jacoco">JaCoCo</a> 0.8.7.202105040129</span></div></body></html> |