Skip to content

Instantly share code, notes, and snippets.

@audtjddld
Forked from keesun/ApiClientUtils.java
Created February 4, 2016 05:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save audtjddld/23226c60b27b3ade7ff2 to your computer and use it in GitHub Desktop.
Save audtjddld/23226c60b27b3ade7ff2 to your computer and use it in GitHub Desktop.
API 서버 사용 예제 코드
package herma.common;
import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.web.client.RestTemplate;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
/**
*
* API Server 사용시 참고용 코드
*
* @author Keesun Baik
*/
public class ApiClientUtils {
RestTemplate restTemplate = new RestTemplate();
ObjectMapper objectMapper = new ObjectMapper();
private String getApiServerUrl(String domain, int apiPort) {
return "http://" + domain + ":" + apiPort;
}
/**
* 보낼 메시지 크기가 큰 경우, 혹은 한글이 포함된 경우 POST로 보내는 것이 안전합니다.
*
* @param domain 애플리케이션에 할당된 도메인
* @param apiPort API Port 번호
* @param appId 애플리케이션 아이디
* @param authKey 애플리케이션의 Auth Key
* @param channelName 이벤트와 메시지를 보낼 채널
* @param event 이벤트
* @param message 메시지
*/
public void sendByPost(String domain, int apiPort, String appId, String authKey, String channelName, String event, String message) {
String serverUrl = getApiServerUrl(domain, apiPort);
Map<String, Object> params = new HashMap<>();
params.put("channels", Arrays.asList(channelName));
params.put("event", event);
params.put("data", message);
String body = null;
try {
body = objectMapper.writeValueAsString(params);
} catch (IOException e) {
throw new RuntimeException(e);
}
if(body != null) {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(new MediaType("application", "json", Charset.forName("UTF-8")));
HttpEntity entity = new HttpEntity(body, headers);
restTemplate.postForEntity(
serverUrl + "/channel/{app}?auth_key={auth_key}",
entity,
String.class,
appId, authKey);
}
}
/**
* 메시지가 짧거나 한글 메시지가 아닌 경우라면 GET으로도 보낼 수 있습니다.
*
* @param domain 애플리케이션에 할당된 도메인
* @param apiPort API Port 번호
* @param appId 애플리케이션 아이디
* @param authKey 애플리케이션의 Auth Key
* @param channelName 이벤트와 메시지를 보낼 채널
* @param event 이벤트
* @param message 메시지
*/
public void sendByGet(String domain, int apiPort, String appId, String authKey, String channelName, String event, String message) {
String serverUrl = getApiServerUrl(domain, apiPort);
Map<String, Object> params = new HashMap<>();
params.put("channels", channelName);
params.put("event", event);
params.put("auth_key", authKey);
params.put("data", message);
params.put("app_id", appId);
restTemplate.getForObject(
serverUrl + "/channel/{app_id}?channels={channels}&event={event}&auth_key={auth_key}&data={data}",
String.class,
params);
}
/**
* 접속할 Push Server URL을 받아옵니다.
*
* @param domain 애플리케이션에 할당된 도메인
* @param apiPort API Port 번호
* @param appId 애플리케이션 아이디
* @param authKey 애플리케이션의 Auth Key 또는 Access Key
* @return Push Server URL
*/
public String getServerUrlByGet(String domain, int apiPort, String appId, String authKey){
String accessUrl = getApiServerUrl(domain, apiPort);
accessUrl += "/access/" + appId + "?auth_key=" + authKey;
String result = restTemplate.getForObject(accessUrl, String.class, null);
Map<String, Object> jsonMap = null;
try {
jsonMap = objectMapper.readValue(result, Map.class);
} catch (IOException e) {
throw new RuntimeException(e);
}
return jsonMap != null ? (String) jsonMap.get("url") : null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment