/app/src/main/java/coo/app/baseline/fax/HttpClient.java
Java | 91 lines | 72 code | 19 blank | 0 comment | 1 complexity | c2afce348b6947fb416d464a860be3a0 MD5 | raw file
- package coo.app.baseline.fax;
- import com.alibaba.fastjson.JSON;
- import com.ringcentral.RestException;
- import java.io.IOException;
- import java.lang.reflect.Type;
- import okhttp3.FormBody;
- import okhttp3.HttpUrl;
- import okhttp3.MediaType;
- import okhttp3.MultipartBody;
- import okhttp3.Request;
- import okhttp3.RequestBody;
- import okhttp3.ResponseBody;
- public abstract class HttpClient {
- private static final MediaType jsonMediaType = MediaType.parse("application/json; charset=utf-8");
- protected String server;
- public abstract ResponseBody request(Request.Builder builder) throws IOException, com.ringcentral.RestException;
- private HttpUrl buildUrl(String endpoint, QueryParameter... queryParameters) {
- HttpUrl.Builder urlBuilder = HttpUrl.parse(server).newBuilder(endpoint);
- for (QueryParameter queryParameter : queryParameters) {
- urlBuilder = urlBuilder.addQueryParameter(queryParameter.key, queryParameter.value);
- }
- return urlBuilder.build();
- }
- public ResponseBody get(String endpoint, QueryParameter... queryParameters) throws IOException, com.ringcentral.RestException {
- HttpUrl url = buildUrl(endpoint, queryParameters);
- return request(new Request.Builder().url(url));
- }
- public ResponseBody post(String endpoint, Object object, QueryParameter... queryParameters) throws IOException, com.ringcentral.RestException {
- RequestBody body = RequestBody.create(jsonMediaType, JSON.toJSONString(object));
- HttpUrl url = buildUrl(endpoint, queryParameters);
- return request(new Request.Builder().url(url).post(body));
- }
- public ResponseBody post(String endpoint, RequestBody requestBody, QueryParameter... queryParameters) throws IOException, com.ringcentral.RestException {
- HttpUrl url = buildUrl(endpoint, queryParameters);
- return request(new Request.Builder().url(url).post(requestBody));
- }
- public ResponseBody put(String endpoint, Object object, QueryParameter... queryParameters) throws IOException, com.ringcentral.RestException {
- RequestBody body = RequestBody.create(jsonMediaType, JSON.toJSONString(object));
- HttpUrl url = buildUrl(endpoint, queryParameters);
- return request(new Request.Builder().url(url).put(body));
- }
- public ResponseBody delete(String endpoint, QueryParameter... queryParameters) throws IOException, com.ringcentral.RestException {
- HttpUrl url = buildUrl(endpoint, queryParameters);
- return request(new Request.Builder().url(url).delete());
- }
- public ResponseBody postBinary(String endpoint, String name, String fileName, String mediaType, byte[] fileContent) throws IOException, com.ringcentral.RestException {
- RequestBody requestBody = new MultipartBody.Builder().setType(MultipartBody.FORM)
- .addFormDataPart(name, fileName, RequestBody.create(MediaType.parse(mediaType), fileContent))
- .build();
- return request(new Request.Builder().url(server + endpoint).post(requestBody));
- }
- public <T> T get(String endpoint, Type type, QueryParameter... queryParameters) throws IOException, com.ringcentral.RestException {
- return JSON.parseObject(get(endpoint, queryParameters).string(), type);
- }
- public <T> T post(String endpoint, FormBody formBody, Type type, QueryParameter... queryParameters) throws IOException, com.ringcentral.RestException {
- return JSON.parseObject(post(endpoint, formBody, queryParameters).string(), type);
- }
- public <T> T post(String endpoint, Object object, Type type, QueryParameter... queryParameters) throws IOException, com.ringcentral.RestException {
- return JSON.parseObject(post(endpoint, object, queryParameters).string(), type);
- }
- public <T> T put(String endpoint, Object object, Type type, QueryParameter... queryParameters) throws IOException, RestException {
- return JSON.parseObject(put(endpoint, object, queryParameters).string(), type);
- }
- public static final class QueryParameter {
- String key;
- String value;
- QueryParameter(String key, String value) {
- this.key = key;
- this.value = value;
- }
- }
- }