2020-06-10 17:03:59 +02:00
package org.hso.ecommerce.api ;
2020-06-10 14:51:12 +02:00
2020-06-21 16:49:35 +02:00
import org.hso.ecommerce.app.config.AppSettings ;
2020-06-10 14:51:12 +02:00
import org.hso.ecommerce.entities.shop.CustomerOrder ;
2020-06-17 17:23:29 +02:00
import org.hso.ecommerce.uimodel.DeliveryData ;
2020-06-18 00:50:43 +02:00
import org.hso.ecommerce.uimodel.DeliveryDataEnum ;
2020-06-10 14:51:12 +02:00
import org.springframework.beans.factory.annotation.Autowired ;
import org.springframework.boot.web.client.RestTemplateBuilder ;
import org.springframework.http.* ;
import org.springframework.stereotype.Service ;
2020-06-13 19:37:34 +02:00
import org.springframework.web.client.ResourceAccessException ;
2020-06-10 14:51:12 +02:00
import org.springframework.web.client.RestTemplate ;
import org.springframework.web.util.UriComponentsBuilder ;
2020-06-20 22:25:52 +02:00
import java.util.HashMap ;
import java.util.Map ;
2020-06-10 14:51:12 +02:00
@Service
public class RestServiceForDelivery {
@Autowired
2020-06-10 17:03:59 +02:00
private final RestTemplateBuilder restTemplateBuilder = null ;
2020-06-21 16:49:35 +02:00
@Autowired
private final AppSettings settings = null ;
private String getDeliveryEndpoint ( ) {
return settings . getParcelServiceApiURL ( ) ;
}
2020-06-18 00:50:43 +02:00
2020-06-10 14:51:12 +02:00
2020-06-20 22:51:18 +02:00
public String getDeliveryID ( CustomerOrder customerOrder ) throws ResourceAccessException {
2020-06-21 16:49:35 +02:00
String url = getDeliveryEndpoint ( ) + " /newDelivery " ;
2020-06-10 14:51:12 +02:00
RestTemplate restTemplate = restTemplateBuilder . build ( ) ;
HttpHeaders headers = new HttpHeaders ( ) ;
headers . setContentType ( MediaType . APPLICATION_JSON ) ;
Map < String , String > requestBody = new HashMap < > ( ) ;
2020-06-20 22:51:18 +02:00
requestBody . put ( " name " , customerOrder . destination . name ) ;
requestBody . put ( " address " , customerOrder . destination . addressString ) ;
2020-06-10 14:51:12 +02:00
2020-06-20 22:51:18 +02:00
HttpEntity < Map < String , String > > entity = new HttpEntity < > ( requestBody , headers ) ;
2020-06-10 14:51:12 +02:00
2020-06-20 22:51:18 +02:00
ResponseEntity < String > response = restTemplate . exchange ( url , HttpMethod . POST , entity , String . class ) ;
2020-06-10 14:51:12 +02:00
2020-06-20 22:51:18 +02:00
if ( response . getStatusCode ( ) = = HttpStatus . OK ) {
2020-06-10 14:51:12 +02:00
return response . getBody ( ) ;
} else {
2020-06-13 19:37:34 +02:00
throw new ResourceAccessException ( " Http Status wrong " ) ;
2020-06-10 14:51:12 +02:00
}
}
2020-06-20 22:25:52 +02:00
public DeliveryData getDeliveryData ( String trackingID ) {
2020-06-10 14:51:12 +02:00
2020-06-21 16:49:35 +02:00
String url = getDeliveryEndpoint ( ) + " /status " ;
2020-06-10 14:51:12 +02:00
RestTemplate restTemplate = restTemplateBuilder . build ( ) ;
HttpHeaders headers = new HttpHeaders ( ) ;
headers . setContentType ( MediaType . APPLICATION_JSON ) ;
2020-06-20 22:25:52 +02:00
UriComponentsBuilder builder = UriComponentsBuilder . fromHttpUrl ( url ) . queryParam ( " trackingID " , trackingID ) ;
2020-06-10 14:51:12 +02:00
HttpEntity < ? > entity = new HttpEntity < > ( headers ) ;
2020-06-13 21:59:18 +02:00
ResponseEntity < DeliveryData > response ;
2020-06-10 14:51:12 +02:00
2020-06-20 22:51:18 +02:00
try {
2020-06-13 21:59:18 +02:00
response = restTemplate . exchange ( builder . toUriString ( ) , HttpMethod . GET , entity , DeliveryData . class ) ;
2020-06-20 22:51:18 +02:00
if ( response . getStatusCode ( ) = = HttpStatus . OK ) {
2020-06-13 19:37:34 +02:00
return response . getBody ( ) ;
} else {
2020-06-18 00:50:43 +02:00
2020-06-20 22:51:18 +02:00
return new DeliveryData ( " " , " " , DeliveryDataEnum . NO_DATA ) ;
2020-06-13 19:37:34 +02:00
}
2020-06-20 22:51:18 +02:00
} catch ( Exception e ) {
return new DeliveryData ( " " , " " , DeliveryDataEnum . NO_DATA ) ;
2020-06-10 14:51:12 +02:00
}
}
}