Skip to content

Instantly share code, notes, and snippets.

@user20161119
Created September 6, 2017 00:01
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 user20161119/84681489c04ef912d3646ab6ef05c357 to your computer and use it in GitHub Desktop.
Save user20161119/84681489c04ef912d3646ab6ef05c357 to your computer and use it in GitHub Desktop.
dropbox preview api test
@Test
public final void testPreview() throws UnsupportedEncodingException {
DropboxContentReq req = new DropboxContentReq(DropboxAPI.PREVIEW);
req.setEnd_point(DropboxAPIConfig.content_endpoint);
req.setHttp_method(HttpMethod.POST);
DropboxPathReq path_req = new DropboxPathReq("");
String path = "/blackdog/中文prd.docx";
//convert Chinese to Unicode /blackdog/\\u4E2D\\u6587prd.docx
path = StringEscapeUtils.escapeJava(path);
path_req.setPath(path);
String header_json = JsonUtils.writeValue(path_req);
LoggerUtils.logInfo(this.getClass(), header_json);
req.getHeaders().add("Dropbox-API-Arg",header_json);
String json = dropbox_api_service.callDropbox(req);
LoggerUtils.logInfo(this.getClass(), json);
}
//resttemplate with httpclient to send http request to dropbox api
package com.future.auction.agent.service;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.http.HttpEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import com.future.auction.agent.model.dropbox.DropboxContentReq;
import com.future.auction.agent.model.dropbox.DropboxReq;
import com.future.auction.utils.LoggerUtils;
@Service
public class DropboxAPIService {
private RestTemplate restTemplate;
public DropboxAPIService(RestTemplateBuilder restTemplateBuilder) {
this.restTemplate = restTemplateBuilder.build();
}
public String callDropbox(DropboxReq req) {
HttpEntity<DropboxReq> requestEntity = new HttpEntity<DropboxReq>(req, req.getHeaders());
if(req instanceof DropboxContentReq){
requestEntity = new HttpEntity<DropboxReq>(req.getHeaders());
}
ResponseEntity<String> resp_entity;
try {
resp_entity = restTemplate.exchange(req.buildReqURL(), req.getHttp_method(),
requestEntity, String.class);
LoggerUtils.logInfo(this.getClass(), resp_entity);
return resp_entity.getBody();
} catch (Exception e) {
LoggerUtils.logException(this.getClass(), "dropbox api service exception " + req.buildReqURL(),e);
}
return "";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment