Skip to content

Commit

Permalink
Implement using interceptor pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
testower committed Jul 31, 2024
1 parent 8614bde commit 7ffcacd
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 20 deletions.
36 changes: 36 additions & 0 deletions src/main/java/org/entur/gbfs/GbfsSubscriptionManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,26 @@ public String subscribeV2(
return subscribe(new GbfsV2Subscription(options, consumer));
}

/**
* Start a subscription on a GBFS v2.x feed
* <p>
* Since v2.x is backwards-compatible with v1.x, v1.x feeds can also be
* consumed with this subscription.
* </p>
*
* @param options Options
* @param consumer A consumer that will handle receiving updates from the loader
* @param updateInterceptor
* @return A string identifier
*/
public String subscribeV2(
GbfsSubscriptionOptions options,
Consumer<GbfsV2Delivery> consumer,
SubscriptionUpdateInterceptor updateInterceptor
) {
return subscribe(new GbfsV2Subscription(options, consumer, updateInterceptor));
}

/**
* Start a subscription on a GBFS v3.x feed
*
Expand All @@ -80,6 +100,22 @@ public String subscribeV3(
return subscribe(new GbfsV3Subscription(options, consumer));
}

/**
* Start a subscription on a GBFS v3.x feed
*
* @param options Options
* @param consumer A consumer that will handle receiving updates from the loader}
* @param updateInterceptor
* @return A string identifier
*/
public String subscribeV3(
GbfsSubscriptionOptions options,
Consumer<GbfsV3Delivery> consumer,
SubscriptionUpdateInterceptor updateInterceptor
) {
return subscribe(new GbfsV3Subscription(options, consumer, updateInterceptor));
}

/**
* Update all subscriptions
*/
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/org/entur/gbfs/GbfsSubscriptionOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,5 @@ public record GbfsSubscriptionOptions(
@Nullable Map<String, String> headers,
@Nullable RequestAuthenticator requestAuthenticator,
@Nullable Long timeout,
@Nullable Boolean enableValidation,
@Nullable String systemId
@Nullable Boolean enableValidation
) {}
23 changes: 21 additions & 2 deletions src/main/java/org/entur/gbfs/loader/v2/GbfsV2Subscription.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.util.Map;
import java.util.function.Consumer;
import org.entur.gbfs.GbfsSubscriptionOptions;
import org.entur.gbfs.SubscriptionUpdateInterceptor;
import org.entur.gbfs.loader.GbfsSubscription;
import org.entur.gbfs.validation.GbfsValidator;
import org.entur.gbfs.validation.GbfsValidatorFactory;
Expand All @@ -51,6 +52,7 @@ public class GbfsV2Subscription implements GbfsSubscription {

private final GbfsSubscriptionOptions subscriptionOptions;
private final Consumer<GbfsV2Delivery> consumer;
private final SubscriptionUpdateInterceptor updateInterceptor;
private GbfsV2Loader loader;

public GbfsV2Subscription(
Expand All @@ -59,6 +61,17 @@ public GbfsV2Subscription(
) {
this.subscriptionOptions = subscriptionOptions;
this.consumer = consumer;
this.updateInterceptor = null;
}

public GbfsV2Subscription(
GbfsSubscriptionOptions subscriptionOptions,
Consumer<GbfsV2Delivery> consumer,
SubscriptionUpdateInterceptor updateInterceptor
) {
this.subscriptionOptions = subscriptionOptions;
this.consumer = consumer;
this.updateInterceptor = updateInterceptor;
}

/**
Expand Down Expand Up @@ -88,7 +101,10 @@ public boolean getSetupComplete() {
* to the consumer if the update had changes
*/
public void update() {
MDC.put("systemId", subscriptionOptions.systemId());
if (updateInterceptor != null) {
updateInterceptor.beforeUpdate();
}

if (loader.update()) {
GbfsV2Delivery delivery = new GbfsV2Delivery(
loader.getDiscoveryFeed(),
Expand All @@ -110,7 +126,10 @@ public void update() {
);
consumer.accept(delivery);
}
MDC.remove("systemId");

if (updateInterceptor != null) {
updateInterceptor.afterUpdate();
}
}

private ValidationResult validateFeeds() {
Expand Down
23 changes: 21 additions & 2 deletions src/main/java/org/entur/gbfs/loader/v3/GbfsV3Subscription.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.util.Map;
import java.util.function.Consumer;
import org.entur.gbfs.GbfsSubscriptionOptions;
import org.entur.gbfs.SubscriptionUpdateInterceptor;
import org.entur.gbfs.loader.GbfsSubscription;
import org.entur.gbfs.validation.GbfsValidator;
import org.entur.gbfs.validation.GbfsValidatorFactory;
Expand All @@ -49,6 +50,7 @@ public class GbfsV3Subscription implements GbfsSubscription {

private final GbfsSubscriptionOptions subscriptionOptions;
private final Consumer<GbfsV3Delivery> consumer;
private final SubscriptionUpdateInterceptor updateInterceptor;
private GbfsV3Loader loader;

public GbfsV3Subscription(
Expand All @@ -57,6 +59,17 @@ public GbfsV3Subscription(
) {
this.subscriptionOptions = subscriptionOptions;
this.consumer = consumer;
this.updateInterceptor = null;
}

public GbfsV3Subscription(
GbfsSubscriptionOptions subscriptionOptions,
Consumer<GbfsV3Delivery> consumer,
SubscriptionUpdateInterceptor updateInterceptor
) {
this.subscriptionOptions = subscriptionOptions;
this.consumer = consumer;
this.updateInterceptor = updateInterceptor;
}

/**
Expand Down Expand Up @@ -85,7 +98,10 @@ public boolean getSetupComplete() {
* to the consumer if the update had changes
*/
public void update() {
MDC.put("systemId", subscriptionOptions.systemId());
if (updateInterceptor != null) {
updateInterceptor.beforeUpdate();
}

if (loader.update()) {
GbfsV3Delivery delivery = new GbfsV3Delivery(
loader.getDiscoveryFeed(),
Expand All @@ -105,7 +121,10 @@ public void update() {
);
consumer.accept(delivery);
}
MDC.remove("systemId");

if (updateInterceptor != null) {
updateInterceptor.afterUpdate();
}
}

private ValidationResult validateFeeds() {
Expand Down
14 changes: 2 additions & 12 deletions src/test/java/org/entur/gbfs/GBFSSubscriptionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,7 @@ GbfsSubscriptionOptions getTestOptions(String url, String languageCode)
null,
null,
null,
true,
"testSystem"
true
);
}

Expand All @@ -93,15 +92,6 @@ Consumer<GbfsV3Delivery> getV3TestConsumer() {
}

GbfsSubscriptionOptions getV3TestOptions(String url) throws URISyntaxException {
return new GbfsSubscriptionOptions(
new URI(url),
null,
null,
null,
null,
null,
true,
null
);
return new GbfsSubscriptionOptions(new URI(url), null, null, null, null, null, true);
}
}
3 changes: 1 addition & 2 deletions src/test/java/org/entur/gbfs/GbfsAuthenticationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,7 @@ GbfsSubscriptionOptions getTestOptions(
null,
requestAuthenticator,
null,
null,
"testSystem"
null
);
}

Expand Down

0 comments on commit 7ffcacd

Please sign in to comment.