@Override public AlertsChannel create(AlertsChannel channel) { return client .target(CHANNELS_URL) .request(APPLICATION_JSON_TYPE) .post(Entity.entity(new AlertsChannelWrapper(channel), APPLICATION_JSON_TYPE), AlertsChannelList.class) .getSingle() .orElseThrow(() -> new IllegalStateException("Failed to create channel: empty list returned")); }
public NewRelicClient(String hostUrl, String apiKey) { this.hostUrl = hostUrl; client = ClientBuilder .newClient() .register(createMapper()) .register(createInterceptor(apiKey)); }
/** * NewRelic API constructor. * * @param restApiUrl NewRelic REST API URL, for example https://api.newrelic.com * @param syntheticsApiUrl NewRelic Synthetics API URL * @param apiKey API Key for given NewRelic account */ public NewRelicInternalApi(String restApiUrl, String syntheticsApiUrl, String apiKey) { NewRelicClient client = new NewRelicClient(restApiUrl, apiKey); applicationsApi = new DefaultApplicationsApi(client); alertsChannelsApi = new DefaultAlertsChannelsApi(client); alertsPoliciesApi = new DefaultAlertsPoliciesApi(client); alertsConditionsApi = new DefaultAlertsConditionsApi(client); alertsExternalServiceConditionsApi = new DefaultAlertsExternalServiceConditionsApi(client); alertsNrqlConditionsApi = new DefaultAlertsNrqlConditionsApi(client); alertsSyntheticsConditionApi = new DefaultAlertsSyntheticsConditionsApi(client); keyTransactionsApi = new DefaultKeyTransactionsApi(client); deploymentsApi = new DefaultDeploymentsApi(client); serversApi = new DefaultServersApi(client); usersApi = new DefaultUsersApi(client); NewRelicClient syntheticsClient = new NewRelicClient(syntheticsApiUrl, apiKey); syntheticsMonitorsApi = new DefaultSyntheticsMonitorsApi(syntheticsClient); } }
@Override public List<AlertsExternalServiceCondition> list(int policyId) { return getPageable( client.target(CONDITIONS_URL).queryParam("policy_id", policyId).request(APPLICATION_JSON_TYPE), AlertsExternalServiceConditionList.class) .getList(); }
@Override public KeyTransaction getById(int keyTransactionId) { return client .target(KEY_TRANSACTION_URL) .resolveTemplate("key_transaction_id", keyTransactionId) .request(APPLICATION_JSON_TYPE) .get(KeyTransactionWrapper.class) .getKeyTransaction(); } }
private <T, LT extends ObjectList<T, LT>> LT followPageable(Link next, LT responseBody, Class<LT> responseType) { while (next != null) { Response nextResponse = client.target(next).request(APPLICATION_JSON_TYPE).get(); LT nextResponseBody = nextResponse.readEntity(responseType); responseBody = responseBody.merge(nextResponseBody); next = nextResponse.getLink("next"); } return responseBody; } }
@Override public Optional<KeyTransaction> getByName(String keyTransactionName) { String keyTransactionNameEncoded = UriComponent.encode(keyTransactionName, QUERY_PARAM_SPACE_ENCODED); return client .target(KEY_TRANSACTIONS_URL) .queryParam("filter[name]", keyTransactionNameEncoded) .request(APPLICATION_JSON_TYPE) .get(KeyTransactionList.class) .getSingle(); }
@Override public AlertsChannel deleteFromPolicy(int policyId, int channelId) { return client .target(POLICY_CHANNELS_URL) .queryParam("policy_id", policyId) .queryParam("channel_id", channelId) .request(APPLICATION_JSON_TYPE) .delete(AlertsChannelWrapper.class) .getChannel(); } }
@Override public Server getById(int serverId) { return client .target(SERVER_URL) .resolveTemplate("server_id", serverId) .request(APPLICATION_JSON_TYPE) .get(ServerWrapper.class) .getServer(); } }
@Override public AlertsPolicy create(AlertsPolicy policy) { return client .target(POLICIES_URL) .request(APPLICATION_JSON_TYPE) .post(Entity.entity(new AlertsPolicyWrapper(policy), APPLICATION_JSON_TYPE), AlertsPolicyWrapper.class) .getPolicy(); }
@Override public List<AlertsSyntheticsCondition> list(int policyId) { return getPageable( client.target(CONDITIONS_URL).queryParam("policy_id", policyId).request(APPLICATION_JSON_TYPE), AlertsSyntheticsConditionList.class) .getList(); }
@Override public List<AlertsCondition> list(int policyId) { return getPageable( client.target(CONDITIONS_URL).queryParam("policy_id", policyId).request(APPLICATION_JSON_TYPE), AlertsConditionList.class) .getList(); }
@Override public List<AlertsNrqlCondition> list(int policyId) { return getPageable( client.target(CONDITIONS_URL).queryParam("policy_id", policyId).request(APPLICATION_JSON_TYPE), AlertsNrqlConditionList.class) .getList(); }
@Override public User getById(int userId) { return client .target(USER_URL) .resolveTemplate("user_id", userId) .request(APPLICATION_JSON_TYPE) .get(UserWrapper.class) .getUser(); } }
@Override public List<AlertsChannel> list() { return getPageable( client.target(CHANNELS_URL).request(APPLICATION_JSON_TYPE), AlertsChannelList.class) .getList(); }
@Override public AlertsSyntheticsCondition create(int policyId, AlertsSyntheticsCondition syntheticsCondition) { return client .target(CONDITION_POLICY_URL) .resolveTemplate("policy_id", policyId) .request(APPLICATION_JSON_TYPE) .post(Entity.entity(new AlertsSyntheticsConditionWrapper(syntheticsCondition), APPLICATION_JSON_TYPE), AlertsSyntheticsConditionWrapper.class) .getSyntheticsCondition(); }
@Override public AlertsExternalServiceCondition create(int policyId, AlertsExternalServiceCondition condition) { return client .target(CONDITION_POLICY_URL) .resolveTemplate("policy_id", policyId) .request(APPLICATION_JSON_TYPE) .post(Entity.entity(new AlertsExternalServiceConditionWrapper(condition), APPLICATION_JSON_TYPE), AlertsExternalServiceConditionWrapper.class) .getExternalServiceCondition(); }
@Override public Deployment create(int applicationId, Deployment deployment) { return client .target(DEPLOYMENTS_URL) .resolveTemplate("application_id", applicationId) .request(APPLICATION_JSON_TYPE) .post(Entity.entity(new DeploymentWrapper(deployment), APPLICATION_JSON_TYPE), DeploymentWrapper.class) .getDeployment(); }
@Override public AlertsNrqlCondition create(int policyId, AlertsNrqlCondition nrqlCondition) { return client .target(CONDITION_POLICY_URL) .resolveTemplate("policy_id", policyId) .request(APPLICATION_JSON_TYPE) .post(Entity.entity(new AlertsNrqlConditionWrapper(nrqlCondition), APPLICATION_JSON_TYPE), AlertsNrqlConditionWrapper.class) .getNrqlCondition(); }
@Override public AlertsCondition create(int policyId, AlertsCondition condition) { return client .target(CONDITION_POLICY_URL) .resolveTemplate("policy_id", policyId) .request(APPLICATION_JSON_TYPE) .post(Entity.entity(new AlertsConditionWrapper(condition), APPLICATION_JSON_TYPE), AlertsConditionWrapper.class) .getCondition(); }