Skip to content

Commit c9d1b4e

Browse files
authored
Merge pull request #159 from xdx54321/master
支持file API
2 parents 83d22a5 + 4c7ee46 commit c9d1b4e

File tree

8 files changed

+268
-3
lines changed

8 files changed

+268
-3
lines changed

pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
<groupId>cn.jpush.api</groupId>
55
<artifactId>jpush-client</artifactId>
6-
<version>3.4.3</version>
6+
<version>3.4.4</version>
77
<packaging>jar</packaging>
88
<url>https://github.com/jpush/jpush-api-java-client</url>
99
<name>JPush API Java Client</name>
@@ -51,7 +51,7 @@
5151
<dependency>
5252
<groupId>cn.jpush.api</groupId>
5353
<artifactId>jiguang-common</artifactId>
54-
<version>1.1.7</version>
54+
<version>1.1.8</version>
5555
</dependency>
5656
<dependency>
5757
<groupId>org.apache.httpcomponents</groupId>
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package cn.jpush.api.file;
2+
3+
import cn.jiguang.common.ClientConfig;
4+
import cn.jiguang.common.ServiceHelper;
5+
import cn.jiguang.common.connection.ApacheHttpClient;
6+
import cn.jiguang.common.connection.HttpProxy;
7+
import cn.jiguang.common.connection.IHttpClient;
8+
import cn.jiguang.common.connection.NativeHttpClient;
9+
import cn.jiguang.common.resp.APIConnectionException;
10+
import cn.jiguang.common.resp.APIRequestException;
11+
import cn.jiguang.common.resp.ResponseWrapper;
12+
import cn.jiguang.common.utils.Preconditions;
13+
import cn.jiguang.common.utils.StringUtils;
14+
import cn.jpush.api.file.model.FileModel;
15+
import cn.jpush.api.file.model.FileModelPage;
16+
import cn.jpush.api.file.model.FileType;
17+
import cn.jpush.api.file.model.FileUploadResult;
18+
import com.google.gson.Gson;
19+
import com.google.gson.reflect.TypeToken;
20+
import org.slf4j.Logger;
21+
import org.slf4j.LoggerFactory;
22+
23+
import java.util.HashMap;
24+
import java.util.Map;
25+
26+
/**
27+
* @author daixuan
28+
* @version 2020/2/23 19:38
29+
*/
30+
public class FileClient {
31+
32+
protected static final Logger LOG = LoggerFactory.getLogger(FileClient.class);
33+
34+
private IHttpClient _httpClient;
35+
private String _baseUrl;
36+
private String _filesPath;
37+
private Gson _gson = new Gson();
38+
39+
public FileClient(String masterSecret, String appKey) {
40+
this(masterSecret, appKey, null, ClientConfig.getInstance());
41+
}
42+
43+
public FileClient(String masterSecret, String appKey, HttpProxy proxy, ClientConfig conf) {
44+
_baseUrl = (String) conf.get(ClientConfig.PUSH_HOST_NAME);
45+
_filesPath = (String) conf.get(ClientConfig.V3_FILES_PATH);
46+
String authCode = ServiceHelper.getBasicAuthorization(appKey, masterSecret);
47+
this._httpClient = new NativeHttpClient(authCode, proxy, conf);
48+
}
49+
50+
public FileUploadResult uploadFile(FileType type, String filename)
51+
throws APIConnectionException, APIRequestException {
52+
Preconditions.checkArgument(type != null, "type should not be null");
53+
Preconditions.checkArgument(StringUtils.isNotEmpty(filename), "filename should not be null");
54+
NativeHttpClient client = (NativeHttpClient) _httpClient;
55+
String typeStr = type.value();
56+
String url = _baseUrl + _filesPath + "/" + typeStr;
57+
Map<String, String> fileMap = new HashMap<>();
58+
fileMap.put("filename", filename);
59+
String response = client.formUpload(url, null, fileMap, null);
60+
LOG.info("uploadFile:{}", response);
61+
return _gson.fromJson(response,
62+
new TypeToken<FileUploadResult>() {
63+
}.getType());
64+
}
65+
66+
public FileModelPage queryEffectFiles() throws APIConnectionException, APIRequestException {
67+
String url = _baseUrl + _filesPath + "/";
68+
ResponseWrapper response = _httpClient.sendGet(url);
69+
LOG.info("queryEffFiles:{}", response);
70+
return _gson.fromJson(response.responseContent,
71+
new TypeToken<FileModelPage>() {
72+
}.getType());
73+
}
74+
75+
public FileModel queryFile(String fileId) throws APIConnectionException, APIRequestException {
76+
Preconditions.checkArgument(StringUtils.isNotEmpty(fileId), "fileId should not be null");
77+
String url = _baseUrl + _filesPath + "/" + fileId;
78+
ResponseWrapper response = _httpClient.sendGet(url);
79+
LOG.info("queryFile:{}", response);
80+
return _gson.fromJson(response.responseContent,
81+
new TypeToken<FileModel>() {
82+
}.getType());
83+
}
84+
85+
public ResponseWrapper deleteFile(String fileId) throws APIConnectionException, APIRequestException {
86+
Preconditions.checkArgument(StringUtils.isNotEmpty(fileId), "fileId should not be null");
87+
String url = _baseUrl + _filesPath + "/" + fileId;
88+
ResponseWrapper response = _httpClient.sendDelete(url);
89+
LOG.info("deleteFile:{}", response);
90+
return response;
91+
}
92+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package cn.jpush.api.file.model;
2+
3+
import com.google.gson.annotations.Expose;
4+
import lombok.Data;
5+
6+
import java.util.Date;
7+
8+
/**
9+
* @author daixuan
10+
* @version 2020/2/23 20:18
11+
*/
12+
@Data
13+
public class FileModel {
14+
@Expose
15+
private String file_id;
16+
@Expose
17+
private String type;
18+
@Expose
19+
private Date create_time;
20+
21+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package cn.jpush.api.file.model;
2+
3+
import com.google.gson.annotations.Expose;
4+
import lombok.Data;
5+
6+
import java.util.Date;
7+
import java.util.List;
8+
9+
/**
10+
* @author daixuan
11+
* @version 2020/2/23 20:18
12+
*/
13+
@Data
14+
public class FileModelPage {
15+
16+
@Expose
17+
private int total_count;
18+
@Expose
19+
private List<FileModel> files;
20+
21+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package cn.jpush.api.file.model;
2+
3+
/**
4+
* @author daixuan
5+
* @version 2020/2/23 20:14
6+
*/
7+
public enum FileType {
8+
9+
ALIAS("alias"),
10+
REGISTRATION_ID("registration_id");
11+
12+
private final String value;
13+
14+
private FileType(final String value) {
15+
this.value = value;
16+
}
17+
18+
public String value() {
19+
return this.value;
20+
}
21+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package cn.jpush.api.file.model;
2+
3+
import lombok.Data;
4+
5+
@Data
6+
public class FileUploadResult {
7+
private String file_id;
8+
private Error error;
9+
10+
@Data
11+
public static class Error {
12+
private String message;
13+
private int code;
14+
15+
}
16+
}

src/main/java/cn/jpush/api/report/MessageDetailResult.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
public class MessageDetailResult extends BaseResult {
1515

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

1919
@Expose
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
package cn.jpush.api.files;
2+
3+
import cn.jiguang.common.resp.APIConnectionException;
4+
import cn.jiguang.common.resp.APIRequestException;
5+
import cn.jpush.api.BaseTest;
6+
import cn.jpush.api.file.FileClient;
7+
import cn.jpush.api.file.model.FileModel;
8+
import cn.jpush.api.file.model.FileModelPage;
9+
import cn.jpush.api.file.model.FileType;
10+
import cn.jpush.api.file.model.FileUploadResult;
11+
import org.junit.Test;
12+
import org.slf4j.Logger;
13+
import org.slf4j.LoggerFactory;
14+
15+
import java.util.HashMap;
16+
import java.util.Map;
17+
18+
/**
19+
* @author daixuan
20+
* @version 2020/2/24 14:01
21+
*/
22+
public class FileClientTest extends BaseTest {
23+
24+
protected static final Logger LOG = LoggerFactory.getLogger(FileClientTest.class);
25+
26+
String fileId = "d4ee2375846bc30fa51334f5-69653861-1408-4d0a-abef-117808632b23";
27+
28+
@Test
29+
public void testUploadFile() {
30+
FileClient fileClient = new FileClient(MASTER_SECRET, APP_KEY);
31+
try {
32+
FileUploadResult result = fileClient.uploadFile(FileType.ALIAS, "README.md");
33+
LOG.info("uploadFile:{}", result);
34+
} catch (APIConnectionException e) {
35+
LOG.error("Connection error. Should retry later. ", e);
36+
} catch (APIRequestException e) {
37+
LOG.error("Error response from JPush server. Should review and fix it. ", e);
38+
LOG.info("HTTP Status: " + e.getStatus());
39+
LOG.info("Error Code: " + e.getErrorCode());
40+
LOG.info("Error Message: " + e.getErrorMessage());
41+
LOG.info("Msg ID: " + e.getMsgId());
42+
}
43+
}
44+
45+
@Test
46+
public void testQueryEffFiles() {
47+
FileClient fileClient = new FileClient(MASTER_SECRET, APP_KEY);
48+
try {
49+
FileModelPage result = fileClient.queryEffectFiles();
50+
LOG.info("queryEffFiles:{}", result);
51+
} catch (APIConnectionException e) {
52+
LOG.error("Connection error. Should retry later. ", e);
53+
} catch (APIRequestException e) {
54+
LOG.error("Error response from JPush server. Should review and fix it. ", e);
55+
LOG.info("HTTP Status: " + e.getStatus());
56+
LOG.info("Error Code: " + e.getErrorCode());
57+
LOG.info("Error Message: " + e.getErrorMessage());
58+
LOG.info("Msg ID: " + e.getMsgId());
59+
}
60+
}
61+
62+
@Test
63+
public void testQueryFile() {
64+
FileClient fileClient = new FileClient(MASTER_SECRET, APP_KEY);
65+
try {
66+
FileModel fileModel = fileClient.queryFile(fileId);
67+
LOG.info("fileModel:{}", fileModel);
68+
} catch (APIConnectionException e) {
69+
LOG.error("Connection error. Should retry later. ", e);
70+
} catch (APIRequestException e) {
71+
LOG.error("Error response from JPush server. Should review and fix it. ", e);
72+
LOG.info("HTTP Status: " + e.getStatus());
73+
LOG.info("Error Code: " + e.getErrorCode());
74+
LOG.info("Error Message: " + e.getErrorMessage());
75+
LOG.info("Msg ID: " + e.getMsgId());
76+
}
77+
}
78+
79+
@Test
80+
public void testDeleteFile() {
81+
FileClient fileClient = new FileClient(MASTER_SECRET, APP_KEY);
82+
try {
83+
fileClient.deleteFile(fileId);
84+
} catch (APIConnectionException e) {
85+
LOG.error("Connection error. Should retry later. ", e);
86+
} catch (APIRequestException e) {
87+
LOG.error("Error response from JPush server. Should review and fix it. ", e);
88+
LOG.info("HTTP Status: " + e.getStatus());
89+
LOG.info("Error Code: " + e.getErrorCode());
90+
LOG.info("Error Message: " + e.getErrorMessage());
91+
LOG.info("Msg ID: " + e.getMsgId());
92+
}
93+
}
94+
}

0 commit comments

Comments
 (0)