Skip to content

Commit

Permalink
Merge pull request #159 from xdx54321/master
Browse files Browse the repository at this point in the history
支持file API
  • Loading branch information
xdx54321 authored Mar 27, 2020
2 parents 83d22a5 + 4c7ee46 commit c9d1b4e
Show file tree
Hide file tree
Showing 8 changed files with 268 additions and 3 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

<groupId>cn.jpush.api</groupId>
<artifactId>jpush-client</artifactId>
<version>3.4.3</version>
<version>3.4.4</version>
<packaging>jar</packaging>
<url>https://github.com/jpush/jpush-api-java-client</url>
<name>JPush API Java Client</name>
Expand Down Expand Up @@ -51,7 +51,7 @@
<dependency>
<groupId>cn.jpush.api</groupId>
<artifactId>jiguang-common</artifactId>
<version>1.1.7</version>
<version>1.1.8</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
Expand Down
92 changes: 92 additions & 0 deletions src/main/java/cn/jpush/api/file/FileClient.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package cn.jpush.api.file;

import cn.jiguang.common.ClientConfig;
import cn.jiguang.common.ServiceHelper;
import cn.jiguang.common.connection.ApacheHttpClient;
import cn.jiguang.common.connection.HttpProxy;
import cn.jiguang.common.connection.IHttpClient;
import cn.jiguang.common.connection.NativeHttpClient;
import cn.jiguang.common.resp.APIConnectionException;
import cn.jiguang.common.resp.APIRequestException;
import cn.jiguang.common.resp.ResponseWrapper;
import cn.jiguang.common.utils.Preconditions;
import cn.jiguang.common.utils.StringUtils;
import cn.jpush.api.file.model.FileModel;
import cn.jpush.api.file.model.FileModelPage;
import cn.jpush.api.file.model.FileType;
import cn.jpush.api.file.model.FileUploadResult;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;

/**
* @author daixuan
* @version 2020/2/23 19:38
*/
public class FileClient {

protected static final Logger LOG = LoggerFactory.getLogger(FileClient.class);

private IHttpClient _httpClient;
private String _baseUrl;
private String _filesPath;
private Gson _gson = new Gson();

public FileClient(String masterSecret, String appKey) {
this(masterSecret, appKey, null, ClientConfig.getInstance());
}

public FileClient(String masterSecret, String appKey, HttpProxy proxy, ClientConfig conf) {
_baseUrl = (String) conf.get(ClientConfig.PUSH_HOST_NAME);
_filesPath = (String) conf.get(ClientConfig.V3_FILES_PATH);
String authCode = ServiceHelper.getBasicAuthorization(appKey, masterSecret);
this._httpClient = new NativeHttpClient(authCode, proxy, conf);
}

public FileUploadResult uploadFile(FileType type, String filename)
throws APIConnectionException, APIRequestException {
Preconditions.checkArgument(type != null, "type should not be null");
Preconditions.checkArgument(StringUtils.isNotEmpty(filename), "filename should not be null");
NativeHttpClient client = (NativeHttpClient) _httpClient;
String typeStr = type.value();
String url = _baseUrl + _filesPath + "/" + typeStr;
Map<String, String> fileMap = new HashMap<>();
fileMap.put("filename", filename);
String response = client.formUpload(url, null, fileMap, null);
LOG.info("uploadFile:{}", response);
return _gson.fromJson(response,
new TypeToken<FileUploadResult>() {
}.getType());
}

public FileModelPage queryEffectFiles() throws APIConnectionException, APIRequestException {
String url = _baseUrl + _filesPath + "/";
ResponseWrapper response = _httpClient.sendGet(url);
LOG.info("queryEffFiles:{}", response);
return _gson.fromJson(response.responseContent,
new TypeToken<FileModelPage>() {
}.getType());
}

public FileModel queryFile(String fileId) throws APIConnectionException, APIRequestException {
Preconditions.checkArgument(StringUtils.isNotEmpty(fileId), "fileId should not be null");
String url = _baseUrl + _filesPath + "/" + fileId;
ResponseWrapper response = _httpClient.sendGet(url);
LOG.info("queryFile:{}", response);
return _gson.fromJson(response.responseContent,
new TypeToken<FileModel>() {
}.getType());
}

public ResponseWrapper deleteFile(String fileId) throws APIConnectionException, APIRequestException {
Preconditions.checkArgument(StringUtils.isNotEmpty(fileId), "fileId should not be null");
String url = _baseUrl + _filesPath + "/" + fileId;
ResponseWrapper response = _httpClient.sendDelete(url);
LOG.info("deleteFile:{}", response);
return response;
}
}
21 changes: 21 additions & 0 deletions src/main/java/cn/jpush/api/file/model/FileModel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package cn.jpush.api.file.model;

import com.google.gson.annotations.Expose;
import lombok.Data;

import java.util.Date;

/**
* @author daixuan
* @version 2020/2/23 20:18
*/
@Data
public class FileModel {
@Expose
private String file_id;
@Expose
private String type;
@Expose
private Date create_time;

}
21 changes: 21 additions & 0 deletions src/main/java/cn/jpush/api/file/model/FileModelPage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package cn.jpush.api.file.model;

import com.google.gson.annotations.Expose;
import lombok.Data;

import java.util.Date;
import java.util.List;

/**
* @author daixuan
* @version 2020/2/23 20:18
*/
@Data
public class FileModelPage {

@Expose
private int total_count;
@Expose
private List<FileModel> files;

}
21 changes: 21 additions & 0 deletions src/main/java/cn/jpush/api/file/model/FileType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package cn.jpush.api.file.model;

/**
* @author daixuan
* @version 2020/2/23 20:14
*/
public enum FileType {

ALIAS("alias"),
REGISTRATION_ID("registration_id");

private final String value;

private FileType(final String value) {
this.value = value;
}

public String value() {
return this.value;
}
}
16 changes: 16 additions & 0 deletions src/main/java/cn/jpush/api/file/model/FileUploadResult.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package cn.jpush.api.file.model;

import lombok.Data;

@Data
public class FileUploadResult {
private String file_id;
private Error error;

@Data
public static class Error {
private String message;
private int code;

}
}
2 changes: 1 addition & 1 deletion src/main/java/cn/jpush/api/report/MessageDetailResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

public class MessageDetailResult extends BaseResult {

private static final Type RECEIVED_TYPE = new TypeToken<List<MessageDetailResult.Received>>() {}.getType();
private static final Type RECEIVED_TYPE = new TypeToken<List<Received>>() {}.getType();
private static final long serialVersionUID = 156439166846147394L;

@Expose
Expand Down
94 changes: 94 additions & 0 deletions src/test/java/cn/jpush/api/files/FileClientTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package cn.jpush.api.files;

import cn.jiguang.common.resp.APIConnectionException;
import cn.jiguang.common.resp.APIRequestException;
import cn.jpush.api.BaseTest;
import cn.jpush.api.file.FileClient;
import cn.jpush.api.file.model.FileModel;
import cn.jpush.api.file.model.FileModelPage;
import cn.jpush.api.file.model.FileType;
import cn.jpush.api.file.model.FileUploadResult;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;

/**
* @author daixuan
* @version 2020/2/24 14:01
*/
public class FileClientTest extends BaseTest {

protected static final Logger LOG = LoggerFactory.getLogger(FileClientTest.class);

String fileId = "d4ee2375846bc30fa51334f5-69653861-1408-4d0a-abef-117808632b23";

@Test
public void testUploadFile() {
FileClient fileClient = new FileClient(MASTER_SECRET, APP_KEY);
try {
FileUploadResult result = fileClient.uploadFile(FileType.ALIAS, "README.md");
LOG.info("uploadFile:{}", result);
} catch (APIConnectionException e) {
LOG.error("Connection error. Should retry later. ", e);
} catch (APIRequestException e) {
LOG.error("Error response from JPush server. Should review and fix it. ", e);
LOG.info("HTTP Status: " + e.getStatus());
LOG.info("Error Code: " + e.getErrorCode());
LOG.info("Error Message: " + e.getErrorMessage());
LOG.info("Msg ID: " + e.getMsgId());
}
}

@Test
public void testQueryEffFiles() {
FileClient fileClient = new FileClient(MASTER_SECRET, APP_KEY);
try {
FileModelPage result = fileClient.queryEffectFiles();
LOG.info("queryEffFiles:{}", result);
} catch (APIConnectionException e) {
LOG.error("Connection error. Should retry later. ", e);
} catch (APIRequestException e) {
LOG.error("Error response from JPush server. Should review and fix it. ", e);
LOG.info("HTTP Status: " + e.getStatus());
LOG.info("Error Code: " + e.getErrorCode());
LOG.info("Error Message: " + e.getErrorMessage());
LOG.info("Msg ID: " + e.getMsgId());
}
}

@Test
public void testQueryFile() {
FileClient fileClient = new FileClient(MASTER_SECRET, APP_KEY);
try {
FileModel fileModel = fileClient.queryFile(fileId);
LOG.info("fileModel:{}", fileModel);
} catch (APIConnectionException e) {
LOG.error("Connection error. Should retry later. ", e);
} catch (APIRequestException e) {
LOG.error("Error response from JPush server. Should review and fix it. ", e);
LOG.info("HTTP Status: " + e.getStatus());
LOG.info("Error Code: " + e.getErrorCode());
LOG.info("Error Message: " + e.getErrorMessage());
LOG.info("Msg ID: " + e.getMsgId());
}
}

@Test
public void testDeleteFile() {
FileClient fileClient = new FileClient(MASTER_SECRET, APP_KEY);
try {
fileClient.deleteFile(fileId);
} catch (APIConnectionException e) {
LOG.error("Connection error. Should retry later. ", e);
} catch (APIRequestException e) {
LOG.error("Error response from JPush server. Should review and fix it. ", e);
LOG.info("HTTP Status: " + e.getStatus());
LOG.info("Error Code: " + e.getErrorCode());
LOG.info("Error Message: " + e.getErrorMessage());
LOG.info("Msg ID: " + e.getMsgId());
}
}
}

0 comments on commit c9d1b4e

Please sign in to comment.