Search in sources :

Example 6 with ZulipClientException

use of com.github.jamesnetherton.zulip.client.exception.ZulipClientException in project zulip-java-client by jamesnetherton.

the class ZulipCommonsHttpClient method doRequest.

private <T extends ZulipApiResponse> T doRequest(HttpUriRequest request, Class<T> responseAs) throws ZulipClientException {
    try {
        ResponseHolder response = client.execute(request, new ResponseHandler<ResponseHolder>() {

            @Override
            public ResponseHolder handleResponse(HttpResponse response) throws IOException {
                Header header = response.getFirstHeader("x-ratelimit-reset");
                int status = response.getStatusLine().getStatusCode();
                if ((status >= 200 && status < 300) || (status == 400)) {
                    HttpEntity entity = response.getEntity();
                    if (entity != null) {
                        String json = EntityUtils.toString(entity);
                        ZulipApiResponse zulipApiResponse = JsonUtils.getMapper().readValue(json, responseAs);
                        return new ResponseHolder(zulipApiResponse, status, header);
                    } else {
                        return new ResponseHolder(null, status, header);
                    }
                } else if (status == 429) {
                    return new ResponseHolder(null, status, header);
                } else {
                    throw new ClientProtocolException("Unexpected response status: " + status);
                }
            }
        }, context);
        if (response.getStatusCode() == 429) {
            ZulipRateLimitExceededException rateLimitExceededException = new ZulipRateLimitExceededException(response.getRateLimitReset());
            throw new ZulipClientException(rateLimitExceededException);
        }
        ZulipApiResponse zulipApiResponse = response.getResponse();
        if (zulipApiResponse == null) {
            throw new ZulipClientException("Response was empty");
        }
        if (!zulipApiResponse.isSuccess()) {
            throw new ZulipClientException(zulipApiResponse.getMessage(), zulipApiResponse.getCode());
        }
        return responseAs.cast(zulipApiResponse);
    } catch (IOException e) {
        throw new ZulipClientException(e);
    }
}
Also used : ZulipClientException(com.github.jamesnetherton.zulip.client.exception.ZulipClientException) Header(org.apache.http.Header) HttpEntity(org.apache.http.HttpEntity) HttpResponse(org.apache.http.HttpResponse) ZulipRateLimitExceededException(com.github.jamesnetherton.zulip.client.exception.ZulipRateLimitExceededException) IOException(java.io.IOException) ZulipApiResponse(com.github.jamesnetherton.zulip.client.api.core.ZulipApiResponse) ClientProtocolException(org.apache.http.client.ClientProtocolException)

Example 7 with ZulipClientException

use of com.github.jamesnetherton.zulip.client.exception.ZulipClientException 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)

Example 8 with ZulipClientException

use of com.github.jamesnetherton.zulip.client.exception.ZulipClientException in project zulip-java-client by jamesnetherton.

the class EventPoller method stop.

/**
 * Stops message polling.
 */
public synchronized void stop() {
    if (status.equals(Status.STARTING) || status.equals(Status.STARTED)) {
        try {
            LOG.info("EventPoller stopping");
            status = Status.STOPPING;
            executor.shutdown();
            DeleteEventQueueApiRequest deleteQueue = new DeleteEventQueueApiRequest(this.client, queue.getQueueId());
            deleteQueue.execute();
        } catch (ZulipClientException e) {
            LOG.warning("Error deleting event queue - " + e.getMessage());
        } finally {
            LOG.info("EventPoller stopped");
            executor = null;
            status = Status.STOPPED;
        }
    }
}
Also used : ZulipClientException(com.github.jamesnetherton.zulip.client.exception.ZulipClientException) DeleteEventQueueApiRequest(com.github.jamesnetherton.zulip.client.api.event.request.DeleteEventQueueApiRequest)

Example 9 with ZulipClientException

use of com.github.jamesnetherton.zulip.client.exception.ZulipClientException in project zulip-java-client by jamesnetherton.

the class ZulipIntegrationTestBase method beforeAll.

@BeforeAll
public static void beforeAll() throws ZulipClientException {
    File zuliprc = new File("./zuliprc");
    assumeTrue(zuliprc.exists() && zuliprc.canRead());
    configuration = ZulipConfiguration.fromZuliprc(zuliprc);
    boolean zulipAvailable = false;
    try {
        InetAddress address = InetAddress.getByName(configuration.getZulipUrl().getHost());
        zulipAvailable = address.isReachable(5000);
    } catch (Exception e) {
    // Host not available
    }
    assumeTrue(zulipAvailable);
    zulip = new ThrottledZulip(new Zulip(configuration));
    ownUser = zulip.users().getOwnUser().execute();
}
Also used : Zulip(com.github.jamesnetherton.zulip.client.Zulip) File(java.io.File) InetAddress(java.net.InetAddress) ZulipClientException(com.github.jamesnetherton.zulip.client.exception.ZulipClientException) BeforeAll(org.junit.jupiter.api.BeforeAll)

Example 10 with ZulipClientException

use of com.github.jamesnetherton.zulip.client.exception.ZulipClientException in project zulip-java-client by jamesnetherton.

the class ZulipServerIT method emoji.

@Test
public void emoji() throws ZulipClientException {
    File file = new File("./src/test/resources/com/github/jamesnetherton/zulip/client/api/server/emoji/smile.png");
    String emojiName = UUID.randomUUID().toString().split("-")[0];
    zulip.server().uploadEmoji(emojiName, file).execute();
    List<CustomEmoji> emojis = zulip.server().getEmoji().execute();
    Optional<CustomEmoji> optional = emojis.stream().filter(e -> e.getName().equals(emojiName)).findFirst();
    assertTrue(optional.isPresent());
    CustomEmoji emoji = optional.get();
    assertEquals(emojiName, emoji.getName());
    assertTrue(emoji.getSourceUrl().endsWith(emoji.getId() + ".png"));
    assertTrue(emoji.getAuthorId() > 0);
    assertTrue(emoji.getId() > 0);
    assertFalse(emoji.isDeactivated());
}
Also used : CustomEmoji(com.github.jamesnetherton.zulip.client.api.server.CustomEmoji) Assertions.fail(org.junit.jupiter.api.Assertions.fail) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) ZulipClientException(com.github.jamesnetherton.zulip.client.exception.ZulipClientException) CustomEmoji(com.github.jamesnetherton.zulip.client.api.server.CustomEmoji) ZulipIntegrationTestBase(com.github.jamesnetherton.zulip.client.api.integration.ZulipIntegrationTestBase) ProfileField(com.github.jamesnetherton.zulip.client.api.server.ProfileField) UUID(java.util.UUID) AuthenticationSettings(com.github.jamesnetherton.zulip.client.api.server.AuthenticationSettings) ServerSettings(com.github.jamesnetherton.zulip.client.api.server.ServerSettings) File(java.io.File) ArrayList(java.util.ArrayList) Linkifier(com.github.jamesnetherton.zulip.client.api.server.Linkifier) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.jupiter.api.Test) List(java.util.List) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) ProfileFieldType(com.github.jamesnetherton.zulip.client.api.server.ProfileFieldType) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) Map(java.util.Map) Optional(java.util.Optional) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) Collections(java.util.Collections) File(java.io.File) Test(org.junit.jupiter.api.Test)

Aggregations

ZulipClientException (com.github.jamesnetherton.zulip.client.exception.ZulipClientException)12 List (java.util.List)4 Test (org.junit.jupiter.api.Test)4 ZulipIntegrationTestBase (com.github.jamesnetherton.zulip.client.api.integration.ZulipIntegrationTestBase)3 Message (com.github.jamesnetherton.zulip.client.api.message.Message)3 Stream (com.github.jamesnetherton.zulip.client.api.stream.Stream)3 URL (java.net.URL)3 ArrayList (java.util.ArrayList)3 Map (java.util.Map)3 UUID (java.util.UUID)3 Assertions.assertEquals (org.junit.jupiter.api.Assertions.assertEquals)3 EventPoller (com.github.jamesnetherton.zulip.client.api.event.EventPoller)2 MessageEventListener (com.github.jamesnetherton.zulip.client.api.event.MessageEventListener)2 MessageService (com.github.jamesnetherton.zulip.client.api.message.MessageService)2 Narrow (com.github.jamesnetherton.zulip.client.api.narrow.Narrow)2 ProfileField (com.github.jamesnetherton.zulip.client.api.server.ProfileField)2 StreamService (com.github.jamesnetherton.zulip.client.api.stream.StreamService)2 StreamSubscriptionRequest (com.github.jamesnetherton.zulip.client.api.stream.StreamSubscriptionRequest)2 ZulipRateLimitExceededException (com.github.jamesnetherton.zulip.client.exception.ZulipRateLimitExceededException)2 File (java.io.File)2