Skip to content

Instantly share code, notes, and snippets.

@AppLoidx
Created August 8, 2019 10:29
Show Gist options
  • Save AppLoidx/b28775028c4af59e6aafb83c54b69166 to your computer and use it in GitHub Desktop.
Save AppLoidx/b28775028c4af59e6aafb83c54b69166 to your computer and use it in GitHub Desktop.
Wrapper for RestTemplate
package com.apploidxxx.heliosbackend.rest.util;
import com.apploidxxx.heliosbackend.config.ExternalSourcesConfig;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;
import java.util.HashMap;
import java.util.Map;
/**
* @author Arthur Kupriyanov
*/
public class Request {
public <T> ResponseEntity<T> get(String path, Class<T> model, Map<String, String> vars) {
return new RestTemplate().getForEntity(generatePathWithParams(path, vars), model, vars);
}
public <T> ResponseEntity<T> get(String path, Class<T> model, String... vars) {
return get(path, model, getMap(vars));
}
public <T> ResponseEntity<T> post(String path, Object object, Class<T> model, Map<String, String> vars) {
return new RestTemplate().postForEntity(generatePathWithParams(path, vars), object, model, vars);
}
public <T> ResponseEntity<T> post(String path, Object object, Class<T> model, String... vars) {
return post(path, object, model, getMap(vars));
}
public <T> ResponseEntity<T> post(String path, Class<T> model, Map<String, String> vars) {
return new RestTemplate().postForEntity(generatePathWithParams(path, vars), null, model, vars);
}
public <T> ResponseEntity<T> post(String path, Class<T> model, String... vars) {
return post(path, null, model, getMap(vars));
}
public static <T> void delete(String path, Class<T> model, Map<String, String> vars) {
new RestTemplate().delete(generatePathWithParams(path, vars), model, vars);
}
public static <T> void delete(String path, Class<T> model, String... vars) {
delete(path, model, getMap(vars));
}
public static void put(String path, Object object, Map<String, String> vars) {
new RestTemplate().put(generatePathWithParams(path, vars), object, vars);
}
public static void put(String path, Object object, String... vars) {
put(path, object, getMap(vars));
}
private static Map<String, String> getMap(String... vars) {
Map<String, String> map = new HashMap<>();
for (int i = vars.length; i > 1; i = i - 2) { map.put(vars[i - 2], vars[i - 1]); }
return map;
}
private static String generatePathWithParams(String path, Map<String, String> map){
StringBuilder sb = new StringBuilder();
for (String k : map.keySet()){
sb.append(k).append("={").append(k).append("}&");
}
return path + "?" + sb.toString().substring(0, sb.length() - 1);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment