Skip to content

Commit

Permalink
add un-tested change event api.
Browse files Browse the repository at this point in the history
  • Loading branch information
jpe42 committed Sep 21, 2023
1 parent 7bb91ee commit 23c0fe2
Show file tree
Hide file tree
Showing 10 changed files with 453 additions and 225 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package systems.comodal.pagerduty.event.client;

import systems.comodal.pagerduty.event.data.PagerDutyChangeEventPayload;
import systems.comodal.pagerduty.event.data.PagerDutyEventPayload;
import systems.comodal.pagerduty.event.data.PagerDutyEventResponse;
import systems.comodal.pagerduty.event.data.adapters.PagerDutyEventAdapter;
Expand Down Expand Up @@ -44,6 +45,11 @@ CompletableFuture<PagerDutyEventResponse> triggerEvent(final String clientName,
final String routingKey,
final PagerDutyEventPayload payload);

CompletableFuture<PagerDutyEventResponse> changeEvent(final String clientName,
final String clientUrl,
final String routingKey,
final PagerDutyChangeEventPayload payload);

interface Builder {

PagerDutyEventClient create();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,16 @@ final class PagerDutyEventClientBuilder implements PagerDutyEventClient.Builder

PagerDutyEventClientBuilder() {
}

@Override
public PagerDutyEventClient create() {
final var authHeader = "Token token=" + requireNonNull(authToken, "Auth token is required.");
final var pageDutyEventsUri = URI.create(pagerDutyUri).resolve("/v2/enqueue");
final var eventUri = URI.create(pagerDutyUri).resolve("/v2/enqueue");
final var changeEventsUri = URI.create(pagerDutyUri).resolve("/v2/change/enqueue");
return new PagerDutyHttpEventClient(defaultClientName, defaultClientUrl,
defaultRoutingKey,
authHeader,
pageDutyEventsUri,
eventUri, changeEventsUri,
httpClient == null ? HttpClient.newBuilder().version(HTTP_2).build() : httpClient,
responseAdapter == null
? ServiceLoader.load(PagerDutyEventAdapterFactory.class).findFirst().orElseThrow().create()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package systems.comodal.pagerduty.event.client;

import systems.comodal.pagerduty.event.data.PagerDutyChangeEventPayload;
import systems.comodal.pagerduty.event.data.PagerDutyEventPayload;
import systems.comodal.pagerduty.event.data.PagerDutyEventResponse;
import systems.comodal.pagerduty.event.data.PagerDutyImageRef;
import systems.comodal.pagerduty.event.data.PagerDutyLinkRef;
import systems.comodal.pagerduty.event.data.adapters.PagerDutyEventAdapter;

import java.net.URI;
Expand All @@ -22,6 +22,7 @@ record PagerDutyHttpEventClient(String defaultClientName,
String defaultRoutingKey,
String authorizationHeader,
URI eventUri,
URI changeEventUri,
HttpClient httpClient,
PagerDutyEventAdapter adapter) implements PagerDutyEventClient {

Expand All @@ -40,6 +41,19 @@ public String getDefaultRoutingKey() {
return defaultRoutingKey;
}

private HttpRequest createRequest(final URI endpoint, final String jsonBody) {
return HttpRequest.newBuilder(endpoint).headers(
"Authorization", authorizationHeader,
"Accept", "application/json",
"Content-Type", "application/json")
.POST(ofString(jsonBody, UTF_8)).build();
}

private CompletableFuture<PagerDutyEventResponse> createAndSendEventRequest(final URI uri, final String jsonBody) {
return httpClient.sendAsync(createRequest(uri, jsonBody), ofByteArray())
.thenApplyAsync(adapter::adaptResponse);
}

@Override
public CompletableFuture<PagerDutyEventResponse> acknowledgeEvent(final String routingKey, final String dedupKey) {
return eventAction(routingKey, dedupKey, "acknowledge");
Expand All @@ -58,7 +72,7 @@ private CompletableFuture<PagerDutyEventResponse> eventAction(final String routi
final var json = String.format("""
{"routing_key":"%s","dedup_key":"%s","event_action":"%s"}""",
routingKey, dedupKey, eventAction);
return createAndSendRequest(routingKey, json);
return createAndSendEventRequest(eventUri, json);
}

@Override
Expand All @@ -68,42 +82,39 @@ public CompletableFuture<PagerDutyEventResponse> triggerEvent(final String clien
final PagerDutyEventPayload payload) {
Objects.requireNonNull(routingKey, "Routing key is a required field.");
final var payloadJson = payload.getPayloadJson();
final var linksJson = payload.getLinksJson();
final var imagesJson = payload.getImages().isEmpty()
? ""
: payload.getImages().stream().map(PagerDutyImageRef::toJson)
.collect(Collectors.joining(",", ",\"images\":[", "]"));
final var linksJson = payload.getLinks().isEmpty()
? ""
: payload.getLinks().stream().map(PagerDutyLinkRef::toJson)
.collect(Collectors.joining(",", ",\"links\":[", "]"));
final var json = String.format("""
{"event_action":"trigger","routing_key":"%s","dedup_key":"%s","payload":%s,"client":"%s"%s%s%s}""",
routingKey, payload.getDedupKey(), payloadJson, clientName,
(clientUrl == null ? "" : ",\"client_url\":\"" + clientUrl + '"'),
imagesJson, linksJson);
return createAndSendRequest(routingKey, json);
return createAndSendEventRequest(eventUri, json);
}

private HttpRequest createRequest(final String routingKey, final String jsonBody) {
return HttpRequest.newBuilder(eventUri)
.headers(
"Authorization", authorizationHeader,
"Accept", "application/vnd.pagerduty+json;version=2",
"Content-Type", "application/json",
"X-Routing-Key", routingKey)
.POST(ofString(jsonBody, UTF_8)).build();
}

private CompletableFuture<PagerDutyEventResponse> createAndSendRequest(final String routingKey, final String jsonBody) {
return httpClient.sendAsync(createRequest(routingKey, jsonBody), ofByteArray())
.thenApplyAsync(adapter::adaptResponse);
@Override
public CompletableFuture<PagerDutyEventResponse> changeEvent(final String clientName,
final String clientUrl,
final String routingKey,
final PagerDutyChangeEventPayload payload) {
Objects.requireNonNull(routingKey, "Routing key is a required field.");
final var payloadJson = payload.getPayloadJson();
final var linksJson = payload.getLinksJson();
final var json = String.format("""
{"routing_key":"%s","payload":%s%s}""",
routingKey, payloadJson, linksJson);
return createAndSendEventRequest(changeEventUri, json);
}

@Override
public String toString() {
return "PagerDutyHttpEventClient{defaultClientName='" + defaultClientName + '\'' +
", defaultClientUrl='" + defaultClientUrl + '\'' +
", defaultRoutingKey='" + defaultRoutingKey + '\'' +
", eventUri=" + eventUri + '}';
", eventUri=" + eventUri + '\'' +
", changeEventUri=" + changeEventUri + '}';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package systems.comodal.pagerduty.event.data;

import java.time.ZonedDateTime;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public interface PagerDutyChangeEventPayload {

static PagerDutyChangeEventPayload.Builder build() {
return new PagerDutyChangeEventPayloadRecord.PagerDutyChangeEventPayloadBuilder();
}

static PagerDutyChangeEventPayload.Builder build(final PagerDutyChangeEventPayload prototype) {
return prototype == null ? build() : new PagerDutyChangeEventPayloadRecord.PagerDutyChangeEventPayloadBuilder(prototype);
}

ZonedDateTime getTimestamp();

String getSummary();

String getSource();

Map<String, Object> getCustomDetails();

List<PagerDutyLinkRef> getLinks();

default String getLinksJson() {
final var links = getLinks();
return links.isEmpty() ? "" : links.stream().map(PagerDutyLinkRef::toJson)
.collect(Collectors.joining(",", ",\"links\":[", "]"));
}

String getPayloadJson();

interface Builder extends PagerDutyChangeEventPayload {


PagerDutyChangeEventPayload create();

Builder summary(final String summary);

Builder timestamp(final ZonedDateTime timestamp);

Builder source(final String source);

Builder customDetails(final String field, final String fieldValue);

Builder customDetails(final String field, final Boolean fieldValue);

Builder customDetails(final String field, final Number fieldValue);

Builder customDetails(final String field, final Object fieldValue);

Builder link(final PagerDutyLinkRef link);
}
}
Loading

0 comments on commit 23c0fe2

Please sign in to comment.