Subscribe to capacity events Proposal
Use the OpenCapacity client to subscribe to changes in the answer to a requirement or a signal, receive events at a webhook, verify their signature, and read or stream events.
Prerequisites#
- An
OpenCapacityclient for a provider that supports subscriptions. To check, callcapabilities().supports(ConformanceClass.SUBSCRIPTIONS). See Get started with the OpenCapacity client. - For webhook delivery: an HTTPS endpoint of your own that the provider can reach.
Subscribe with webhook delivery#
The following example creates a client and a subscription that sends an event to a webhook when a grade changes:
import com.eqixiac.opencapacity.OpenCapacity;
import com.eqixiac.opencapacity.Subscription;
import com.eqixiac.opencapacity.model.CapacityRequirement;
import com.eqixiac.opencapacity.model.DensityRange;
import com.eqixiac.opencapacity.model.Location;
import com.eqixiac.opencapacity.model.NotifyOn;
OpenCapacity capacity = OpenCapacity.builder()
.endpoint("https://provider.example")
.credentials("CLIENT_ID", "CLIENT_SECRET")
.build();
CapacityRequirement requirement = CapacityRequirement.define()
.addLocation(Location.metro("FR"))
.cabinets(40)
.density(DensityRange.kva().target(8).create())
.create();
Subscription webhook = capacity.subscriptions().define(requirement)
.name("Frankfurt 2027")
.notifyOn(NotifyOn.GRADE_CHANGE)
.webhook("https://customer.example/hooks/capacity")
.create();
String signingSecret = webhook.signingSecret().orElseThrow();
System.out.println(webhook.id() + " " + webhook.state());A new webhook subscription has the state PENDING_VERIFICATION until your endpoint echoes the challenge of the provider.
To follow one of your signals, pass the Signal to define(...) in place of the requirement.
Handle an event at your webhook#
Your endpoint receives each event as the body of a POST, with an OpenCapacity-Signature header. The following handler takes the header value and the raw body. It verifies the signature, answers the verification event, and prints an answer event. It returns the response body, or null for an event that needs no body:
import com.eqixiac.opencapacity.Events;
import com.eqixiac.opencapacity.WebhookSignature;
import com.eqixiac.opencapacity.model.Event;
import java.util.function.BinaryOperator;
BinaryOperator<String> handle = (signatureHeader, rawBody) -> {
if (!WebhookSignature.verify(signingSecret, signatureHeader, rawBody, Instant.now(), Duration.ofMinutes(5))) {
throw new SecurityException("signature does not match");
}
Event received = Events.parse(rawBody);
if (Event.SUBSCRIPTION_VERIFICATION.equals(received.type())) {
return Events.challengeResponse(received); // respond 200 with this body
}
if (received.isAnswerUpdate()) {
System.out.println(received.sequence() + " " + received.data().reason()
+ " " + received.data().answer().grade());
}
return null; // respond with any 2xx status
};The last argument of verify is the tolerance for the age of the timestamp in the header, five minutes in this example.
Pass the body exactly as you received it. The signature covers the raw bytes.
Delivery is at least once. De-duplicate on event.id(), and order by event.sequence(). Any 2xx response acknowledges the event.
Subscribe with pull delivery#
With pull delivery, you need no endpoint, and the subscription is ACTIVE at once. The following example creates a pull subscription and reads the first page of events:
Subscription pull = capacity.subscriptions().define(requirement).pull().create();
for (Event event : pull.events(0, 50)) { // after sequence 0, at most 50 events
System.out.println(event.sequence() + " " + event.type());
}The first event has the current answer, with the reason INITIAL.
Stream events#
stream reads events over one open connection and blocks. The following example prints events until the handler has seen a grade change:
import com.eqixiac.opencapacity.model.EventReason;
long last = pull.stream(0, event -> {
System.out.println(event.sequence() + " " + event.type());
return event.data().reason() != EventReason.GRADE_CHANGE; // false stops the stream
});stream returns the sequence of the last event that the handler received. It returns when the handler returns false or when the provider closes the stream. To continue, call it again with that sequence.
Pause, resume, and delete a subscription#
The following example pauses a subscription, resumes it, changes its webhook URL, and deletes it:
webhook.pause();
webhook.resume();
webhook.update().webhook("https://customer.example/hooks/capacity-v2").save();
System.out.println(webhook.state());
webhook.delete();
pull.delete();A change of the webhook URL returns the state to PENDING_VERIFICATION.
Related pages#
- For subscription states and delivery rules, see Signals and subscriptions.
- For the event types, see Event types.
- For each event as it appears on the wire, and a worked signature, see Subscription and event examples.