Search in sources :

Example 1 with SendMessageApiResponse

use of com.github.jamesnetherton.zulip.client.api.message.response.SendMessageApiResponse in project zulip-java-client by jamesnetherton.

the class SendMessageApiRequest method execute.

/**
 * Executes the Zulip API request for sending a message.
 *
 * @return                      The id of the message
 * @throws ZulipClientException if the request was not successful
 */
@Override
public Long execute() throws ZulipClientException {
    final Map<String, Object> params = getParams();
    final Map<String, Object> modified = new HashMap<>();
    if (params.get(TYPE) != null) {
        for (Map.Entry<String, Object> entry : params.entrySet()) {
            if (!entry.getKey().equals(TO_PRIVATE) && !entry.getKey().equals(TO_STREAM)) {
                modified.put(entry.getKey(), entry.getValue());
            }
        }
        String type = (String) params.get(TYPE);
        if (type.equals(MessageType.PRIVATE.toString())) {
            try {
                modified.put(TO, JsonUtils.getMapper().writeValueAsString(params.get(TO_PRIVATE)));
            } catch (JsonProcessingException e) {
                throw new ZulipClientException(e);
            }
        } else {
            modified.put(TO, params.get(TO_STREAM));
        }
    }
    SendMessageApiResponse response = client().post(MESSAGES_API_PATH, modified, SendMessageApiResponse.class);
    return response.getId();
}
Also used : ZulipClientException(com.github.jamesnetherton.zulip.client.exception.ZulipClientException) HashMap(java.util.HashMap) SendMessageApiResponse(com.github.jamesnetherton.zulip.client.api.message.response.SendMessageApiResponse) Map(java.util.Map) HashMap(java.util.HashMap) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Aggregations

JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 SendMessageApiResponse (com.github.jamesnetherton.zulip.client.api.message.response.SendMessageApiResponse)1 ZulipClientException (com.github.jamesnetherton.zulip.client.exception.ZulipClientException)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1