/**
* @author ishaikh
*
*/
package com.transformuk.bdt.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
import com.google.common.util.concurrent.RateLimiter;
@Component
public class MapItClientService {
// allow a max of 1 call per second i.e. 180 per 3 mins
private static final int NO_OF_PERMITS_PER_SECOND = 1;
@Autowired
MapItMongoService mapItMongoService;
// final
final RateLimiter limiter = RateLimiter.create(NO_OF_PERMITS_PER_SECOND);
@Autowired
RestTemplate restTemplate;
public ResponseEntity<String> callMapIt(String mapItUrl, String postCode) {
// wait until we acquire
limiter.acquire();
try {
return restTemplate.exchange(mapItUrl, HttpMethod.GET,
buildHeader(), String.class, postCode);
} finally {
mapItMongoService.updateCount();
}
}
public ResponseEntity<String> callMapIt(String mapItUrl) {
// wait until we acquire
limiter.acquire();
try {
return restTemplate.exchange(mapItUrl, HttpMethod.GET,
buildHeader(), String.class);
} finally {
mapItMongoService.updateCount();
}
}
private HttpEntity<String> buildHeader() {
HttpHeaders headers = new HttpHeaders();
headers.set("User-Agent", "Department of Business, Innovation & Skills");
return new HttpEntity<String>("parameters", headers);
}
}