Java Code Examples for cn.jiguang.common.resp.APIRequestException

The following examples show how to use cn.jiguang.common.resp.APIRequestException. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source Project: jiguang-java-client-common   Author: jpush   File: NativeHttpClient.java    License: MIT License 6 votes vote down vote up
public ResponseWrapper doRequest(String url, String content, 
        RequestMethod method) throws APIConnectionException, APIRequestException {
    ResponseWrapper response = null;
    for (int retryTimes = 0; ; retryTimes++) {
        try {
            response = _doRequest(url, content, method);
            break;
        } catch (SocketTimeoutException e) {
            if (KEYWORDS_READ_TIMED_OUT.equals(e.getMessage())) {
                // Read timed out.  For push, maybe should not re-send.
                throw new APIConnectionException(READ_TIMED_OUT_MESSAGE, e, true);
            } else {    // connect timed out
                if (retryTimes >= _maxRetryTimes) {
                    throw new APIConnectionException(CONNECT_TIMED_OUT_MESSAGE, e, retryTimes);
                } else {
                    LOG.debug("connect timed out - retry again - " + (retryTimes + 1));
                }
            }
        }
    }
    return response;
}
 
Example #2
Source Project: jmessage-api-java-client   Author: jpush   File: UserClient.java    License: MIT License 5 votes vote down vote up
/**
 * Delete friends
 *
 * @param username Friends you want to delete to
 * @param users    username to be delete
 * @return No content
 * @throws APIConnectionException connect exception
 * @throws APIRequestException    request exception
 */
public ResponseWrapper deleteFriends(String username, String... users)
        throws APIConnectionException, APIRequestException {
    StringUtils.checkUsername(username);
    Preconditions.checkArgument(null != users && users.length > 0, "friend list should not be empty");

    JsonArray array = new JsonArray();
    for (String user : users) {
        array.add(new JsonPrimitive(user));
    }
    return _httpClient.sendDelete(_baseUrl + userPath + "/" + username + "/friends", array.toString());
}
 
Example #3
Source Project: jmessage-api-java-client   Author: jpush   File: UserClient.java    License: MIT License 5 votes vote down vote up
/**
 * Update friends' note information. The size is limit to 500.
 *
 * @param username Necessary
 * @param array    FriendNote array
 * @return No content
 * @throws APIConnectionException connect exception
 * @throws APIRequestException    request exception
 */
public ResponseWrapper updateFriendsNote(String username, FriendNote[] array)
        throws APIConnectionException, APIRequestException {
    StringUtils.checkUsername(username);
    FriendNotePayload payload = new FriendNotePayload.Builder()
            .setFriendNotes(array)
            .build();
    Preconditions.checkArgument(null != payload, "FriendNotePayload should not be null");
    return _httpClient.sendPut(_baseUrl + userPath + "/" + username + "/friends", payload.toString());
}
 
Example #4
Source Project: jmessage-api-java-client   Author: jpush   File: JMessageClient.java    License: MIT License 5 votes vote down vote up
public String registerUsers(RegisterInfo[] users)
        throws APIConnectionException, APIRequestException {
    RegisterPayload payload = RegisterPayload.newBuilder()
            .addUsers(users)
            .build();

    return _userClient.registerUsers(payload).responseContent;
}
 
Example #5
Source Project: jmessage-api-java-client   Author: jpush   File: ChatRoomClient.java    License: MIT License 5 votes vote down vote up
/**
 * Get chat room information by room ids
 *
 * @param roomIds Array of room id
 * @return {@link ChatRoomListResult}
 * @throws APIConnectionException connect exception
 * @throws APIRequestException    request exception
 */
public ChatRoomListResult getBatchChatRoomInfo(long... roomIds) throws APIConnectionException, APIRequestException {
    Preconditions.checkArgument(roomIds != null && roomIds.length > 0, "Room ids should not be null");
    JsonArray array = new JsonArray();
    for (long id : roomIds) {
        array.add(new JsonPrimitive(id));
    }
    ResponseWrapper responseWrapper = _httpClient.sendPost(_baseUrl + mChatRoomPath + "/batch", array.toString());
    return ChatRoomListResult.fromResponse(responseWrapper);
}
 
Example #6
Source Project: jmessage-api-java-client   Author: jpush   File: UserClient.java    License: MIT License 5 votes vote down vote up
public ResponseWrapper removeBlackList(String username, String... users)
        throws APIConnectionException, APIRequestException {
    StringUtils.checkUsername(username);
    Preconditions.checkArgument(null != users && users.length > 0, "black list should not be empty");
    JsonArray array = new JsonArray();
    for (String user : users) {
        array.add(new JsonPrimitive(user));
    }

    return _httpClient.sendDelete(_baseUrl + userPath + "/" + username + "/blacklist", array.toString());
}
 
Example #7
Source Project: jsms-api-java-client   Author: jpush   File: SMSClient.java    License: MIT License 5 votes vote down vote up
/**
 * Send SMS verification code to server, to verify if the code valid
 *
 * @param msgId The message id of the verification code
 * @param code  Verification code
 * @return return ValidSMSResult includes is_valid
 * @throws APIConnectionException connection exception
 * @throws APIRequestException    request exception
 */
public ValidSMSResult sendValidSMSCode(String msgId, String code)
        throws APIConnectionException, APIRequestException {
    Preconditions.checkArgument(null != msgId, "Message id should not be null");
    Pattern codePattern = Pattern.compile("^[0-9]{6}");
    Preconditions.checkArgument(codePattern.matcher(code).matches(), "The verification code shoude be consist of six number");
    JsonObject json = new JsonObject();
    json.addProperty(SMS_CODE, code);

    ResponseWrapper response = _httpClient.sendPost(_baseUrl + _smsCodePath + "/" + msgId + _validPath, json.toString());
    return ValidSMSResult.fromResponse(response, ValidSMSResult.class);
}
 
Example #8
Source Project: jmessage-api-java-client   Author: jpush   File: JMessageClient.java    License: MIT License 5 votes vote down vote up
public String registerAdmins(String username, String password)
        throws APIConnectionException, APIRequestException {
    RegisterInfo payload = RegisterInfo.newBuilder()
            .setUsername(username)
            .setPassword(password)
            .build();

    return _userClient.registerAdmins(payload).responseContent;
}
 
Example #9
Source Project: jsms-api-java-client   Author: jpush   File: SMSClient.java    License: MIT License 5 votes vote down vote up
/**
 * create sign
 * 其实两个接口可以合并 但没时间搞
 *
 * @param payload {@link SignPayload }
 * @return SignResult
 * @throws APIConnectionException connect exception
 * @throws APIRequestException    request exception
 */
public SignResult createSign(SignPayload payload) throws APIConnectionException, APIRequestException {
    Preconditions.checkArgument(null != payload, "sign payload should not be null");
    Preconditions.checkArgument(payload.getType() > 0 && payload.getType() <= 7,
            "type should be between 1 and 7");
    Map<String, Object> params = new HashMap<String, Object>();
    params.put(SignPayload.getSIGN(), payload.getSign());
    if (!StringUtils.isEmpty(payload.getRemark())) {
        Preconditions.checkArgument(payload.getRemark().length() <= 100,
                "remark too long");
        params.put(SignPayload.getREMARK(), payload.getRemark());
    }
    params.put(SignPayload.getTYPE(), payload.getType());
    Map<String, byte[]> fileParams = new HashMap<String, byte[]>();
    File[] images = payload.getImages();
    if (images != null && images.length > 0) {
        for (File file : payload.getImages()) {
            fileParams.put(file.getName(), getBytes(file));
        }
    }
    String url = _baseUrl + _signPath;
    try {
        ResponseWrapper wrapper = doPostSign(url, params, fileParams, SignPayload.getIMAGES());
        if (wrapper.responseCode != 200) {
            throw new APIRequestException(wrapper);
        }
        return SignResult.fromResponse(wrapper, SignResult.class);
    } catch (IOException e) {
        e.printStackTrace();
        throw new IllegalArgumentException("fail to creat sign");
    }
}
 
Example #10
Source Project: jsms-api-java-client   Author: jpush   File: SMSClient.java    License: MIT License 5 votes vote down vote up
/**
 * update sign
 *
 * @param payload {@link SignPayload }
 * @param signId necessary
 * @return SignResult
 * @throws APIConnectionException connect exception
 * @throws APIRequestException    request exception
 */
public SignResult updateSign(SignPayload payload, int signId) throws APIConnectionException, APIRequestException {
    Preconditions.checkArgument(null != payload, "sign payload should not be null");
    Preconditions.checkArgument(payload.getType() > 0 && payload.getType() <= 7,
            "type should be between 1 and 7");

    Map<String, Object> params = new HashMap<String, Object>();
    params.put(SignPayload.getSIGN(), payload.getSign());
    if (!StringUtils.isEmpty(payload.getRemark())) {
        Preconditions.checkArgument(payload.getRemark().length() <= 100,
                "remark too long");
        params.put(SignPayload.getREMARK(), payload.getRemark());
    }
    params.put(SignPayload.getTYPE(), payload.getType());
    Map<String, byte[]> fileParams = new HashMap<String, byte[]>();
    File[] images = payload.getImages();
    if (images != null && images.length > 0) {
        for (File file : payload.getImages()) {
            fileParams.put(file.getName(), getBytes(file));
        }
    }
    String url = _baseUrl + _signPath + "/" + signId;
    try {
        ResponseWrapper wrapper = doPostSign(url, params, fileParams, SignPayload.getIMAGES());
        if (wrapper.responseCode != 200) {
            throw new APIRequestException(wrapper);
        }
        return SignResult.fromResponse(wrapper, SignResult.class);
    } catch (IOException e) {
        e.printStackTrace();
        throw new IllegalArgumentException("fail to update sign");
    }
}
 
Example #11
Source Project: jmessage-api-java-client   Author: jpush   File: UserClient.java    License: MIT License 5 votes vote down vote up
public UserInfoResult getUserInfo(String username)
        throws APIConnectionException, APIRequestException {

    StringUtils.checkUsername(username);
    ResponseWrapper response = _httpClient.sendGet(_baseUrl + userPath + "/" + username);
    return UserInfoResult.fromResponse(response, UserInfoResult.class);
}
 
Example #12
Source Project: jmessage-api-java-client   Author: jpush   File: ReportClient.java    License: MIT License 5 votes vote down vote up
/**
 * Get user's message list with cursor, the cursor will effective in 120 seconds.
 * And will return same count of messages as first request.
 * @param username Necessary parameter.
 * @param cursor First request will return cursor
 * @return MessageListResult
 * @throws APIConnectionException connect exception
 * @throws APIRequestException request exception
 */
public MessageListResult v2GetUserMessagesByCursor(String username, String cursor)
        throws APIConnectionException, APIRequestException {
    StringUtils.checkUsername(username);
    if (null != cursor) {
        String requestUrl = mBaseReportPath + mV2UserPath + "/" + username + "/messages?cursor=" + cursor;
        ResponseWrapper response = _httpClient.sendGet(requestUrl);
        return MessageListResult.fromResponse(response, MessageListResult.class);
    } else {
        throw new IllegalArgumentException("the cursor parameter should not be null");
    }
}
 
Example #13
Source Project: jmessage-api-java-client   Author: jpush   File: ReportClient.java    License: MIT License 5 votes vote down vote up
/**
 * Get user statistic, now time unit only supports DAY
 * @param startTime start time, format: yyyy-MM-dd
 * @param duration 0-60
 * @return {@link UserStatListResult}
 * @throws APIConnectionException connect exception
 * @throws APIRequestException request exception
 */
public UserStatListResult getUserStatistic(String startTime, int duration)
        throws APIConnectionException, APIRequestException {
    Preconditions.checkArgument(verifyDateFormat("DAY", startTime), "Illegal date format");
    Preconditions.checkArgument(0 <= duration && duration <= 60, " 0 <= duration <= 60");
    String url = mBaseReportPath + mV2StatisticPath + "/users?time_unit=DAY&start=" + startTime + "&duration=" + duration;
    ResponseWrapper responseWrapper = _httpClient.sendGet(url);
    return UserStatListResult.fromResponse(responseWrapper);
}
 
Example #14
Source Project: jmessage-api-java-client   Author: jpush   File: ReportClient.java    License: MIT License 5 votes vote down vote up
/**
 * Get message statistic. Detail instructions please refer to https://docs.jiguang.cn/jmessage/server/rest_api_im_report_v2/#_6
 * @param timeUnit MUST be one of HOUR, DAY, MONTH
 * @param start start time, when timeUnit is HOUR, format: yyyy-MM-dd HH,
 * @param duration depends on timeUnit, the duration has limit
 * @return {@link MessageStatListResult}
 * @throws APIConnectionException connect exception
 * @throws APIRequestException request exception
 */
public MessageStatListResult getMessageStatistic(String timeUnit, String start, int duration)
        throws APIConnectionException, APIRequestException {
    Preconditions.checkArgument(verifyDateFormat(timeUnit, start), "Time format error, please check your argument");
    if (timeUnit.equals("HOUR")) {
        Preconditions.checkArgument(0 <= duration && duration <= 24, "time unit is HOUR, duration must between 0 and 24 ");
    } else if (timeUnit.equals("DAY")) {
        Preconditions.checkArgument(0 <= duration && duration <= 60, "time unit is DAY, duration must between 0 and 60");
    } else if (timeUnit.equals("MONTH")) {
        Preconditions.checkArgument(0 <= duration && duration <= 2, "time unit is MONTH, duration must between 0 and 2");
    } else throw new IllegalArgumentException("Time unit error");
    String url = mBaseReportPath + mV2StatisticPath + "/messages?time_unit=" + timeUnit + "&start=" + start + "&duration=" + duration;
    ResponseWrapper responseWrapper = _httpClient.sendGet(url);
    return MessageStatListResult.fromResponse(responseWrapper, MessageStatListResult.class);
}
 
Example #15
Source Project: jmessage-api-java-client   Author: jpush   File: MessageClient.java    License: MIT License 5 votes vote down vote up
public SendMessageResult sendMessage(MessagePayload payload)
        throws APIConnectionException, APIRequestException {
    Preconditions.checkArgument(!(null == payload), "Message payload should not be null");

    ResponseWrapper response = _httpClient.sendPost(_baseUrl + messagePath, payload.toString());
    return SendMessageResult.fromResponse(response, SendMessageResult.class);
}
 
Example #16
Source Project: jmessage-api-java-client   Author: jpush   File: MessageClient.java    License: MIT License 5 votes vote down vote up
/**
 * Please use {@link cn.jmessage.api.reportv2.ReportClient#v2GetUserMessagesByCursor(String, String)}
 * Get message list with cursor, the cursor will effective in 120 seconds. And will
 * return same count of messages as first request.
 * @param cursor First request will return cursor
 * @return MessageListResult
 * @throws APIConnectionException connect exception
 * @throws APIRequestException request exception
 */
@Deprecated
public MessageListResult getMessageListByCursor(String cursor)
        throws APIConnectionException, APIRequestException {
    if (null != cursor) {
        String requestUrl = reportBaseUrl + v2_messagePath + "?cursor=" + cursor;
        ResponseWrapper response = _httpClient.sendGet(requestUrl);
        return MessageListResult.fromResponse(response, MessageListResult.class);
    } else {
        throw new IllegalArgumentException("the cursor parameter should not be null");
    }
}
 
Example #17
Source Project: jmessage-api-java-client   Author: jpush   File: MessageClient.java    License: MIT License 5 votes vote down vote up
/**
 * Please use {@link cn.jmessage.api.reportv2.ReportClient#v2GetUserMessagesByCursor}
 * Get user's message list with cursor, the cursor will effective in 120 seconds.
 * And will return same count of messages as first request.
 * @param username Necessary parameter.
 * @param cursor First request will return cursor
 * @return MessageListResult
 * @throws APIConnectionException connect exception
 * @throws APIRequestException request exception
 */
@Deprecated
public MessageListResult getUserMessagesByCursor(String username, String cursor)
        throws APIConnectionException, APIRequestException {
    StringUtils.checkUsername(username);
    if (null != cursor) {
        String requestUrl = reportBaseUrl + v2_userPath + "/" + username + "/messages?cursor=" + cursor;
        ResponseWrapper response = _httpClient.sendGet(requestUrl);
        return MessageListResult.fromResponse(response, MessageListResult.class);
    } else {
        throw new IllegalArgumentException("the cursor parameter should not be null");
    }
}
 
Example #18
Source Project: jmessage-api-java-client   Author: jpush   File: MessageClient.java    License: MIT License 5 votes vote down vote up
/**
 * get chatroom history message
 * @param chatroomid
 * @param cursor must greater than zero and less than 1000
 * @return
 */
public ChatRoomHistoryResult getChatRoomHistory(Long chatroomid, String cursor)
    throws APIConnectionException, APIRequestException {
    Preconditions.checkArgument(cursor != null && cursor.length() > 0, "count is invalid");
    String requestUrl = reportBaseUrl + v2_chatroomPath + "/" + chatroomid + "/" + "messages" + "?cursor=" + cursor;
    ResponseWrapper response = _httpClient.sendGet(requestUrl);
    return ChatRoomHistoryResult.fromResponse(response, ChatRoomHistoryResult.class);
}
 
Example #19
Source Project: jmessage-api-java-client   Author: jpush   File: ResourceClient.java    License: MIT License 5 votes vote down vote up
/**
 * Download file with mediaId, will return DownloadResult which include url.
 * @param mediaId Necessary
 * @return DownloadResult
 * @throws APIConnectionException connect exception
 * @throws APIRequestException request exception
 */
public DownloadResult downloadFile(String mediaId)
        throws APIConnectionException, APIRequestException {
    Preconditions.checkArgument(null != mediaId, "mediaId is necessary");

    ResponseWrapper response = _httpClient.sendGet(_baseUrl + resourcePath + "?mediaId=" + mediaId);
    return DownloadResult.fromResponse(response, DownloadResult.class);
}
 
Example #20
Source Project: jmessage-api-java-client   Author: jpush   File: GroupClient.java    License: MIT License 5 votes vote down vote up
public GroupInfoResult getGroupInfo(long gid)
        throws APIConnectionException, APIRequestException {
    Preconditions.checkArgument(gid > 0, "gid should more than 0.");

    ResponseWrapper response = _httpClient.sendGet(_baseUrl + groupPath + "/" + gid);

    return GroupInfoResult.fromResponse(response, GroupInfoResult.class);
}
 
Example #21
Source Project: jmessage-api-java-client   Author: jpush   File: GroupClient.java    License: MIT License 5 votes vote down vote up
public GroupListResult getGroupListByAppkey(int start, int count)
        throws APIConnectionException, APIRequestException {
    if (start < 0 || count <= 0 || count > 500) {
        throw new IllegalArgumentException("negative index or count must more than 0 and less than 501");
    }
    ResponseWrapper response = _httpClient.sendGet(_baseUrl + groupPath + "?start=" + start + "&count=" + count);
    return GroupListResult.fromResponse(response, GroupListResult.class);
}
 
Example #22
Source Project: jmessage-api-java-client   Author: jpush   File: GroupClient.java    License: MIT License 5 votes vote down vote up
public CreateGroupResult createGroup(GroupPayload payload)
        throws APIConnectionException, APIRequestException {
    Preconditions.checkArgument(!(null == payload), "group payload should not be null");

    ResponseWrapper response = _httpClient.sendPost(_baseUrl + groupPath, payload.toString());
    return CreateGroupResult.fromResponse(response, CreateGroupResult.class);
}
 
Example #23
Source Project: jmessage-api-java-client   Author: jpush   File: UserClient.java    License: MIT License 5 votes vote down vote up
/**
 * Add Users to black list
 *
 * @param username The owner of the black list
 * @param users    The users that will add to black list
 * @return add users to black list
 * @throws APIConnectionException connect exception
 * @throws APIRequestException    request exception
 */
public ResponseWrapper addBlackList(String username, String... users)
        throws APIConnectionException, APIRequestException {
    StringUtils.checkUsername(username);
    Preconditions.checkArgument(null != users && users.length > 0, "black list should not be empty");

    JsonArray array = new JsonArray();
    for (String user : users) {
        array.add(new JsonPrimitive(user));
    }
    return _httpClient.sendPut(_baseUrl + userPath + "/" + username + "/blacklist", array.toString());
}
 
Example #24
Source Project: jmessage-api-java-client   Author: jpush   File: JMessageClient.java    License: MIT License 4 votes vote down vote up
public void updateUserInfo(String username, UserPayload payload)
        throws APIConnectionException, APIRequestException {
    _userClient.updateUserInfo(username, payload);
}
 
Example #25
Source Project: jmessage-api-java-client   Author: jpush   File: UserClient.java    License: MIT License 4 votes vote down vote up
public ResponseWrapper deleteUser(String username)
        throws APIConnectionException, APIRequestException {
    StringUtils.checkUsername(username);
    return _httpClient.sendDelete(_baseUrl + userPath + "/" + username);
}
 
Example #26
Source Project: jiguang-java-client-common   Author: jpush   File: IHttpClient.java    License: MIT License 4 votes vote down vote up
public ResponseWrapper sendGet(String url, String content)
throws APIConnectionException, APIRequestException;
 
Example #27
Source Project: jiguang-java-client-common   Author: jpush   File: IHttpClient.java    License: MIT License 4 votes vote down vote up
public ResponseWrapper sendDelete(String url)
throws APIConnectionException, APIRequestException;
 
Example #28
Source Project: jiguang-java-client-common   Author: jpush   File: IHttpClient.java    License: MIT License 4 votes vote down vote up
public ResponseWrapper sendDelete(String url, String content)
throws APIConnectionException, APIRequestException;
 
Example #29
Source Project: jiguang-java-client-common   Author: jpush   File: IHttpClient.java    License: MIT License 4 votes vote down vote up
public ResponseWrapper sendPost(String url, String content)
throws APIConnectionException, APIRequestException;
 
Example #30
Source Project: jmessage-api-java-client   Author: jpush   File: GroupClient.java    License: MIT License 4 votes vote down vote up
public ResponseWrapper setGroupMemberSilence(long gid, boolean status, String[] usernames)
    throws APIConnectionException, APIRequestException {
    Preconditions.checkArgument(gid > 0, "gid should more than 0.");
    Preconditions.checkArgument(usernames != null && usernames.length > 0, "username array is invalid");
    return _httpClient.sendPut(_baseUrl + groupPath + "/messages/" + gid + "/silence?status=" + (status ? "true" : "false"), new Gson().toJson(usernames));
}