Polish StandardGitHub

This commit is contained in:
Moritz Halbritter 2023-01-12 17:32:24 +01:00
parent aca087e762
commit b3644c1a99

View File

@ -16,17 +16,12 @@
package org.springframework.boot.build.bom.bomr.github; package org.springframework.boot.build.bom.bomr.github;
import java.io.IOException;
import java.util.Arrays;
import java.util.Base64; import java.util.Base64;
import java.util.Collections;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.http.HttpRequest;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.http.client.ClientHttpRequestExecution;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.client.RestTemplate; import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.DefaultUriBuilderFactory; import org.springframework.web.util.DefaultUriBuilderFactory;
@ -51,19 +46,13 @@ final class StandardGitHub implements GitHub {
@Override @Override
public GitHubRepository getRepository(String organization, String name) { public GitHubRepository getRepository(String organization, String name) {
RestTemplate restTemplate = new RestTemplate( RestTemplate restTemplate = new RestTemplate(
Arrays.asList(new MappingJackson2HttpMessageConverter(new ObjectMapper()))); Collections.singletonList(new MappingJackson2HttpMessageConverter(new ObjectMapper())));
restTemplate.getInterceptors().add(new ClientHttpRequestInterceptor() { restTemplate.getInterceptors().add((request, body, execution) -> {
request.getHeaders().add("User-Agent", StandardGitHub.this.username);
@Override request.getHeaders().add("Authorization", "Basic " + Base64.getEncoder()
public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) .encodeToString((StandardGitHub.this.username + ":" + StandardGitHub.this.password).getBytes()));
throws IOException { request.getHeaders().add("Accept", MediaType.APPLICATION_JSON_VALUE);
request.getHeaders().add("User-Agent", StandardGitHub.this.username); return execution.execute(request, body);
request.getHeaders().add("Authorization", "Basic " + Base64.getEncoder().encodeToString(
(StandardGitHub.this.username + ":" + StandardGitHub.this.password).getBytes()));
request.getHeaders().add("Accept", MediaType.APPLICATION_JSON_VALUE);
return execution.execute(request, body);
}
}); });
UriTemplateHandler uriTemplateHandler = new DefaultUriBuilderFactory( UriTemplateHandler uriTemplateHandler = new DefaultUriBuilderFactory(
"https://api.github.com/repos/" + organization + "/" + name + "/"); "https://api.github.com/repos/" + organization + "/" + name + "/");