|
| 1 | +package cn.jpush.api.admin; |
| 2 | + |
| 3 | +import cn.jiguang.common.ServiceHelper; |
| 4 | +import cn.jiguang.common.connection.HttpProxy; |
| 5 | +import cn.jiguang.common.connection.IHttpClient; |
| 6 | +import cn.jiguang.common.connection.NativeHttpClient; |
| 7 | +import cn.jiguang.common.resp.APIConnectionException; |
| 8 | +import cn.jiguang.common.resp.APIRequestException; |
| 9 | +import cn.jiguang.common.resp.DefaultResult; |
| 10 | +import cn.jiguang.common.resp.ResponseWrapper; |
| 11 | +import cn.jiguang.common.utils.Preconditions; |
| 12 | +import cn.jpush.api.JPushConfig; |
| 13 | +import com.google.gson.Gson; |
| 14 | +import com.google.gson.JsonObject; |
| 15 | + |
| 16 | +/** |
| 17 | + * Admin APIs |
| 18 | + * https://docs.jiguang.cn/jpush/server/push/rest_api_admin_api_v1/ |
| 19 | + */ |
| 20 | +public class AdminClient { |
| 21 | + |
| 22 | + private IHttpClient mHttpClient; |
| 23 | + private String mBasePath; |
| 24 | + private String mV1AppPath; |
| 25 | + private Gson mGson = new Gson(); |
| 26 | + |
| 27 | + /** |
| 28 | + * Create a Push Client. |
| 29 | + * |
| 30 | + * @param appKey The KEY of one application on JPush. |
| 31 | + * @param masterSecret API access secret of the appKey. |
| 32 | + */ |
| 33 | + public AdminClient(String appKey, String masterSecret) { |
| 34 | + this(appKey, masterSecret, null, JPushConfig.getInstance()); |
| 35 | + } |
| 36 | + |
| 37 | + public AdminClient(String appKey, String masterSecret, HttpProxy proxy) { |
| 38 | + this(appKey, masterSecret, proxy, JPushConfig.getInstance()); |
| 39 | + } |
| 40 | + |
| 41 | + |
| 42 | + public AdminClient(String appKey, String masterSecret, HttpProxy proxy, JPushConfig conf) { |
| 43 | + ServiceHelper.checkBasic(appKey, masterSecret); |
| 44 | + mBasePath = (String) conf.get(JPushConfig.ADMIN_HOST_NAME); |
| 45 | + mV1AppPath = (String) conf.get(JPushConfig.V1_APP_PATH); |
| 46 | + String authCode = ServiceHelper.getBasicAuthorization(appKey, masterSecret); |
| 47 | + this.mHttpClient = new NativeHttpClient(authCode, proxy, conf.getClientConfig()); |
| 48 | + } |
| 49 | + |
| 50 | + public void setHttpClient(IHttpClient client) { |
| 51 | + this.mHttpClient = client; |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Create an app under developer account |
| 56 | + * @param appName app name |
| 57 | + * @param packageName android package name |
| 58 | + * @param groupName developer app group name |
| 59 | + * @return {@link CreateAppResult} |
| 60 | + * @throws APIConnectionException connect exception |
| 61 | + * @throws APIRequestException request exception |
| 62 | + */ |
| 63 | + public CreateAppResult createApp(String appName, String packageName, String groupName) |
| 64 | + throws APIConnectionException, APIRequestException { |
| 65 | + Preconditions.checkArgument(null != appName, "app name should not be null"); |
| 66 | + Preconditions.checkArgument(null != packageName, "package name should not be null"); |
| 67 | + JsonObject jsonObject = new JsonObject(); |
| 68 | + jsonObject.addProperty("app_name", appName); |
| 69 | + jsonObject.addProperty("android_package", packageName); |
| 70 | + if (null != groupName) { |
| 71 | + jsonObject.addProperty("group_name", groupName); |
| 72 | + } |
| 73 | + ResponseWrapper responseWrapper = mHttpClient.sendPost(mBasePath + mV1AppPath, mGson.toJson(jsonObject)); |
| 74 | + return CreateAppResult.fromResponse(responseWrapper, CreateAppResult.class); |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Delete app by app key |
| 79 | + * @param appKey app key |
| 80 | + * @return {@link AppResult} |
| 81 | + * @throws APIConnectionException connect exception |
| 82 | + * @throws APIRequestException request exception |
| 83 | + */ |
| 84 | + public AppResult deleteApp(String appKey) throws APIConnectionException, APIRequestException { |
| 85 | + ResponseWrapper responseWrapper = mHttpClient.sendDelete(mBasePath + mV1AppPath + "/" + appKey + "/delete"); |
| 86 | + return DefaultResult.fromResponse(responseWrapper, AppResult.class); |
| 87 | + } |
| 88 | + |
| 89 | +// public AppResult uploadCertificate(String appKey) throws APIConnectionException, APIRequestException { |
| 90 | +// |
| 91 | +// } |
| 92 | +} |
0 commit comments