85 lines
2.8 KiB
Java
85 lines
2.8 KiB
Java
|
package org.hso.ecommerce.action.shop;
|
||
|
|
||
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
||
|
import com.fasterxml.jackson.databind.JsonNode;
|
||
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||
|
import org.hso.ecommerce.controller.intern.customers.CustomerOrderController;
|
||
|
import org.hso.ecommerce.entities.shop.CustomerOrder;
|
||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||
|
import org.springframework.boot.web.client.RestTemplateBuilder;
|
||
|
import org.springframework.http.*;
|
||
|
|
||
|
import org.springframework.stereotype.Service;
|
||
|
import org.springframework.web.client.RestTemplate;
|
||
|
import org.springframework.web.util.UriComponentsBuilder;
|
||
|
|
||
|
import java.util.*;
|
||
|
|
||
|
@Service
|
||
|
public class RestServiceForDelivery {
|
||
|
|
||
|
@Autowired
|
||
|
private RestTemplateBuilder restTemplateBuilder;
|
||
|
|
||
|
|
||
|
public String getDeliveryID(CustomerOrder customerOrder)
|
||
|
{
|
||
|
String url = "http://[::1]:8082/newDelivery";
|
||
|
|
||
|
RestTemplate restTemplate = restTemplateBuilder.build();
|
||
|
|
||
|
HttpHeaders headers = new HttpHeaders();
|
||
|
headers.setContentType(MediaType.APPLICATION_JSON);
|
||
|
|
||
|
Map<String, String> requestBody = new HashMap<>();
|
||
|
|
||
|
requestBody.put("name",customerOrder.destination.name);
|
||
|
requestBody.put("address",customerOrder.destination.addressString);
|
||
|
|
||
|
HttpEntity<Map<String, String>> entity = new HttpEntity<>(requestBody,headers);
|
||
|
|
||
|
ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.POST,entity, String.class);
|
||
|
|
||
|
if (response.getStatusCode() == HttpStatus.OK)
|
||
|
{
|
||
|
return response.getBody();
|
||
|
} else {
|
||
|
return null;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public CustomerOrderController.DeliveryData getDeliveryData(UUID trackingID) {
|
||
|
|
||
|
String url = "http://[::1]:8082/status";
|
||
|
|
||
|
RestTemplate restTemplate = restTemplateBuilder.build();
|
||
|
|
||
|
HttpHeaders headers = new HttpHeaders();
|
||
|
headers.setContentType(MediaType.APPLICATION_JSON);
|
||
|
|
||
|
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url).queryParam("trackingID",trackingID.toString());
|
||
|
|
||
|
HttpEntity<?> entity = new HttpEntity<>(headers);
|
||
|
|
||
|
ResponseEntity<String> response = restTemplate.exchange(builder.toUriString(), HttpMethod.GET, entity, String.class);
|
||
|
|
||
|
ObjectMapper mapper = new ObjectMapper();
|
||
|
JsonNode node = null;
|
||
|
|
||
|
try {
|
||
|
node = mapper.readTree(response.getBody());
|
||
|
} catch (JsonProcessingException e) {
|
||
|
e.printStackTrace();
|
||
|
}
|
||
|
|
||
|
if (response.getStatusCode() == HttpStatus.OK)
|
||
|
{
|
||
|
return new CustomerOrderController.DeliveryData(node.get("status").asText(),node.get("estimatedArrival").asText());
|
||
|
} else {
|
||
|
return null;
|
||
|
}
|
||
|
|
||
|
|
||
|
}
|
||
|
}
|