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);
}
}
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();
}
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;
}
}
}
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();
}
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());
}
Aggregations