Created
October 5, 2016 07:33
-
-
Save nfrankel/99a25739c0cd5eb784f503971e6277f7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.springframework.boot.json.BasicJsonParser; | |
import org.springframework.http.HttpEntity; | |
import org.springframework.http.HttpHeaders; | |
import org.springframework.http.HttpMethod; | |
import org.springframework.http.ResponseEntity; | |
import org.springframework.util.LinkedMultiValueMap; | |
import org.springframework.util.MultiValueMap; | |
import org.springframework.web.bind.annotation.RequestMapping; | |
import org.springframework.web.bind.annotation.RequestParam; | |
import org.springframework.web.bind.annotation.RestController; | |
import org.springframework.web.client.RestTemplate; | |
import java.util.Map; | |
import java.util.Random; | |
import static org.springframework.http.HttpMethod.POST; | |
import static org.springframework.web.bind.annotation.RequestMethod.GET; | |
@RestController | |
public class RandomController { | |
private final Random random; | |
public RandomController(Random random) { | |
this.random = random; | |
} | |
@RequestMapping(value = "/get", method = GET) | |
public int random(@RequestParam(value = "limit", defaultValue = "100") int limit) { | |
String bearerToken = getBearerToken(); | |
int[] bounds = getBounds(bearerToken); | |
return computeRandom(bounds[0], bounds[1]); | |
} | |
private String getBearerToken() { | |
RestTemplate tokenTemplate = new RestTemplate(); | |
MultiValueMap<String, String> body = new LinkedMultiValueMap<>(); | |
body.add("client_id", "my id"); | |
body.add("client_secret", "my secret"); | |
body.add("grant_type", "client_credentials"); | |
HttpHeaders headers = new HttpHeaders(); | |
headers.add("Accept", "application/json"); | |
HttpEntity<?> entity = new HttpEntity<>(body, headers); | |
ResponseEntity<String> res = tokenTemplate.exchange( | |
"https://bearer.token/get", POST, entity, String.class); | |
Map<String, Object> map = new BasicJsonParser().parseMap(res.getBody()); | |
return (String) map.get("access_token"); | |
} | |
private int[] getBounds(String bearerToken) { | |
RestTemplate configurationTemplate = new RestTemplate(); | |
HttpHeaders headers = new HttpHeaders(); | |
headers.add("Authorization", "Bearer " + bearerToken); | |
HttpEntity<?> entity = new HttpEntity<>(headers); | |
ResponseEntity<String> res = configurationTemplate.exchange( | |
"https://configurations.com/bounds", HttpMethod.GET, entity, String.class); | |
Map<String, Object> map = new BasicJsonParser().parseMap(res.getBody()); | |
Map<String, Long> value = (Map<String, Long>) map.get("value"); | |
int lowerBound = value.get("lower").intValue(); | |
int upperBound = value.get("upper").intValue(); | |
return new int[]{lowerBound, upperBound}; | |
} | |
private int computeRandom(int lowerBound, int upperBound) { | |
int difference = upperBound - lowerBound; | |
int raw = random.nextInt(difference); | |
return raw + lowerBound; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment