|
| 1 | +package br.com.developers.github.http.service.request.impl; |
| 2 | + |
| 3 | +import org.apache.log4j.Logger; |
| 4 | +import org.springframework.http.HttpEntity; |
| 5 | +import org.springframework.http.HttpHeaders; |
| 6 | +import org.springframework.http.HttpMethod; |
| 7 | +import org.springframework.http.ResponseEntity; |
| 8 | +import org.springframework.stereotype.Component; |
| 9 | +import org.springframework.web.client.RestTemplate; |
| 10 | +import org.springframework.web.util.UriComponents; |
| 11 | +import br.com.developers.github.http.resource.developer.Developer; |
| 12 | +import br.com.developers.github.http.service.request.Send; |
| 13 | +import lombok.AccessLevel; |
| 14 | +import lombok.AllArgsConstructor; |
| 15 | + |
| 16 | +@AllArgsConstructor(access = AccessLevel.PACKAGE) |
| 17 | +@Component |
| 18 | +public class SendImpl implements Send<Developer> { |
| 19 | + |
| 20 | + private static final Logger LOG = Logger.getLogger(SendImpl.class); |
| 21 | + |
| 22 | + private RestTemplate restTemplate; |
| 23 | + |
| 24 | + public ResponseEntity<Developer> sendRequest(UriComponents uri) { |
| 25 | + LOG.info("Sending request to URI [ " + uri + " ]"); |
| 26 | + HttpHeaders headers = createHttpHeaders(); |
| 27 | + HttpEntity<Developer> entity = createHttpEntity(headers); |
| 28 | + return this.restTemplate.exchange(uri.toUri(), HttpMethod.POST, entity, Developer.class); |
| 29 | + } |
| 30 | + |
| 31 | + private HttpHeaders createHttpHeaders() { |
| 32 | + HttpHeaders headers = new HttpHeaders(); |
| 33 | + headers.set("Accept", "application/vnd.github.v3+json"); |
| 34 | + return headers; |
| 35 | + } |
| 36 | + |
| 37 | + private HttpEntity<Developer> createHttpEntity(HttpHeaders headers) { |
| 38 | + return new HttpEntity<Developer>(headers); |
| 39 | + } |
| 40 | + |
| 41 | +} |
0 commit comments