@Override public CreateEmoApiKeyResponse createApiKey(String apiKey, CreateEmoApiKeyRequest request) throws EmoApiKeyNotFoundException { checkNotNull(request, "request"); checkArgument(!Strings.isNullOrEmpty(request.getOwner()), "Non-empty owner is required"); try { URI uri = _uac.clone() .segment("api-key") .build(); EmoResource resource = _client.resource(uri); for (Map.Entry<String, String> customQueryParam : request.getCustomRequestParameters().entries()) { resource = resource.queryParam(customQueryParam.getKey(), customQueryParam.getValue()); } return resource .type(APPLICATION_X_CREATE_API_KEY_TYPE) .accept(MediaType.APPLICATION_JSON_TYPE) .header(ApiKeyRequest.AUTHENTICATION_HEADER, apiKey) .post(CreateEmoApiKeyResponse.class, JsonHelper.asJson(request)); } catch (EmoClientException e) { throw convertException(e); } }
@Override public void dropTable(String apiKey, String table, Audit audit) throws UnknownTableException { checkNotNull(table, "table"); checkNotNull(audit, "audit"); URI uri = _dataStore.clone() .segment("_table", table) .build(); EmoResponse response = _client.resource(uri) .queryParam("audit", RisonHelper.asORison(audit)) .accept(MediaType.APPLICATION_JSON_TYPE) .header(ApiKeyRequest.AUTHENTICATION_HEADER, apiKey) .delete(EmoResponse.class); if (response.getStatus() != Response.Status.OK.getStatusCode()) { throw convertException(new EmoClientException(response)); } }
@Override public void dropTable(String apiKey, String table, Audit audit) throws UnknownTableException { checkNotNull(table, "table"); checkNotNull(audit, "audit"); URI uri = _dataStore.clone() .segment("_table", table) .build(); EmoResponse response = _client.resource(uri) .queryParam("audit", RisonHelper.asORison(audit)) .accept(MediaType.APPLICATION_JSON_TYPE) .header(ApiKeyRequest.AUTHENTICATION_HEADER, apiKey) .delete(EmoResponse.class); if (response.getStatus() != Response.Status.OK.getStatusCode()) { throw convertException(new EmoClientException(response)); } }
@Override public String migrateApiKey(String apiKey, MigrateEmoApiKeyRequest request) { checkNotNull(request, "request"); String id = checkNotNull(request.getId(), "id"); try { URI uri = _uac.clone() .segment("api-key") .segment(id) .segment("migrate") .build(); EmoResource resource = _client.resource(uri); for (Map.Entry<String, String> customQueryParam : request.getCustomRequestParameters().entries()) { resource = resource.queryParam(customQueryParam.getKey(), customQueryParam.getValue()); } CreateEmoApiKeyResponse response = resource .accept(MediaType.APPLICATION_JSON_TYPE) .header(ApiKeyRequest.AUTHENTICATION_HEADER, apiKey) .post(CreateEmoApiKeyResponse.class, null); return response.getKey(); } catch (EmoClientException e) { throw convertException(e); } }
@Override public PollResult poll(String apiKey, @PartitionKey String subscription, Duration claimTtl, int limit) { checkNotNull(subscription, "subscription"); checkNotNull(claimTtl, "claimTtl"); URI uri = getPollUriBuilder(subscription, claimTtl, limit).build(); EmoResponse response = _client.resource(uri) .queryParam("includeTags", "true") .accept(MediaType.APPLICATION_JSON_TYPE) .header(ApiKeyRequest.AUTHENTICATION_HEADER, apiKey) .get(EmoResponse.class); if (response.getStatus() != Response.Status.OK.getStatusCode()) { throw convertException(new EmoClientException(response)); } Iterator<Event> events = response.getEntity(new TypeReference<Iterator<Event>>() {}); boolean moreEvents; String databusEmpty = response.getFirstHeader(POLL_DATABUS_EMPTY_HEADER); if (databusEmpty != null) { // Use the header value from the server to determine if the databus subscription is empty moreEvents = !Boolean.parseBoolean(databusEmpty); } else { // Must be polling an older version of Emo which did not include this header. Infer whether the queue // is empty based on whether any results were returned. moreEvents = events.hasNext(); } return new PollResult(events, limit, moreEvents); }