quartzPro/.svn/pristine/41/41dc938ac9888d7fb2ef95cd6e1...

98 lines
4.2 KiB
Plaintext

package com.ifish.jpush.schedule;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.ifish.jpush.common.ClientConfig;
import com.ifish.jpush.common.ServiceHelper;
import com.ifish.jpush.common.connection.HttpProxy;
import com.ifish.jpush.common.connection.IHttpClient;
import com.ifish.jpush.common.connection.NativeHttpClient;
import com.ifish.jpush.common.resp.APIConnectionException;
import com.ifish.jpush.common.resp.APIRequestException;
import com.ifish.jpush.common.resp.ResponseWrapper;
import com.ifish.jpush.schedule.model.SchedulePayload;
import com.ifish.jpush.utils.Preconditions;
import com.ifish.jpush.utils.StringUtils;
public class ScheduleClient {
private static final Logger LOG = LoggerFactory.getLogger(ScheduleClient.class);
private final NativeHttpClient _httpClient;
private String hostName;
private String schedulePath;
public ScheduleClient(String masterSecret, String appkey) {
this(masterSecret, appkey, IHttpClient.DEFAULT_MAX_RETRY_TIMES, null, ClientConfig.getInstance());
}
public ScheduleClient(String masterSecret, String appKey, int maxRetryTimes) {
this(masterSecret, appKey, maxRetryTimes, null, ClientConfig.getInstance());
}
public ScheduleClient(String masterSecret, String appKey, int maxRetryTimes, HttpProxy proxy) {
this(masterSecret, appKey, maxRetryTimes, proxy, ClientConfig.getInstance());
}
/**
* Create a Schedule Client with custom configuration.
* @param masterSecret API access secret of the appKey.
* @param appKey The KEY of one application on JPush.
* @param maxRetryTimes Max retry times
* @param proxy The proxy, if there is no proxy, should be null.
* @param conf The client configuration. Can use ClientConfig.getInstance() as default.
*/
public ScheduleClient(String masterSecret, String appKey, int maxRetryTimes, HttpProxy proxy, ClientConfig conf) {
ServiceHelper.checkBasic(appKey, masterSecret);
hostName = (String) conf.get(ClientConfig.SCHEDULE_HOST_NAME);
schedulePath = (String) conf.get(ClientConfig.SCHEDULE_PATH);
String authCode = ServiceHelper.getBasicAuthorization(appKey, masterSecret);
this._httpClient = new NativeHttpClient(authCode, maxRetryTimes, proxy);
}
public ScheduleResult createSchedule(SchedulePayload payload) throws APIConnectionException, APIRequestException {
Preconditions.checkArgument(null != payload, "payload should not be null");
ResponseWrapper response = _httpClient.sendPost(hostName + schedulePath, payload.toString());
return ScheduleResult.fromResponse(response, ScheduleResult.class);
}
public ScheduleListResult getScheduleList(int page) throws APIConnectionException, APIRequestException{
Preconditions.checkArgument(page > 0, "page should more than 0.");
ResponseWrapper response = _httpClient.sendGet(hostName + schedulePath + "?page=" + page);
return ScheduleListResult.fromResponse(response, ScheduleListResult.class);
}
public ScheduleResult getSchedule(String scheduleId) throws APIConnectionException, APIRequestException{
Preconditions.checkArgument(StringUtils.isNotEmpty(scheduleId), "scheduleId should not be empty");
ResponseWrapper response = _httpClient.sendGet(hostName + schedulePath + "/" + scheduleId);
return ScheduleResult.fromResponse(response, ScheduleResult.class);
}
public ScheduleResult updateSchedule(String scheduleId, SchedulePayload payload) throws APIConnectionException, APIRequestException{
Preconditions.checkArgument(StringUtils.isNotEmpty(scheduleId), "scheduleId should not be empty");
Preconditions.checkArgument(null != payload, "payload should not be null");
ResponseWrapper response = _httpClient.sendPut(hostName + schedulePath + "/" + scheduleId,
payload.toString());
return ScheduleResult.fromResponse(response, ScheduleResult.class);
}
public void deleteSchedule(String scheduleId) throws APIConnectionException, APIRequestException{
Preconditions.checkArgument(StringUtils.isNotEmpty(scheduleId), "scheduleId should not be empty");
_httpClient.sendDelete(hostName + schedulePath + "/" + scheduleId);
}
}