Skip to content

Commit

Permalink
Merge pull request #105 from traveltime-dev/feature/x-correlation-id
Browse files Browse the repository at this point in the history
Added x-correlation-id to proto requests
  • Loading branch information
bartosz-witkowski authored Apr 14, 2023
2 parents 05bc324 + ee8bac2 commit c9ec45a
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ public abstract class ProtoRequest<T> {

public abstract Either<TravelTimeError, T> parseBytes(byte[] body);

public abstract String getCorrelationId();

protected Either<TravelTimeError, TimeFilterFastResponseOuterClass.TimeFilterFastResponse> getProtoResponse(byte[] body) {
return Try
.of(() -> TimeFilterFastResponseOuterClass.TimeFilterFastResponse.parseFrom(body))
Expand All @@ -45,6 +47,7 @@ protected Request createProtobufRequest(
.headers(credentials.getBasicCredentialsHeaders())
.addHeader("Content-Type", AcceptType.APPLICATION_OCTET_STREAM.getValue())
.addHeader("User-Agent", "Travel Time Java SDK " + Version.getVersion())
.addHeader("X-Correlation-ID", getCorrelationId())
.post(RequestBody.create(requestBody))
.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ public class TimeFilterFastProtoDistanceRequest extends ProtoRequest<TimeFilterF
@NonNull
Integer travelTime;

String correlationId;

private byte[] createByteArray() {

RequestsCommon.Coords departure = RequestsCommon
Expand Down Expand Up @@ -145,6 +147,15 @@ public TimeFilterFastProtoDistanceResponse merge(List<TimeFilterFastProtoDistanc
return new TimeFilterFastProtoDistanceResponse(times, distances);
}

@Override
public String getCorrelationId() {
if (correlationId == null) {
return "no-x-correlation-id";
} else {
return correlationId;
}
}

/**
* @param originCoordinate The coordinates of location we should start the search from.
* @param destinationCoordinates The coordinates of locations we run the search to. If the class implementing this list
Expand All @@ -167,5 +178,26 @@ public TimeFilterFastProtoDistanceRequest(
this.travelTime = travelTime;
this.transportation = transportation;
this.country = country;
this.correlationId = null;
}

public TimeFilterFastProtoDistanceRequest(
@NonNull Coordinates originCoordinate,
@NonNull List<Coordinates> destinationCoordinates,
@NonNull Integer travelTime,
@NonNull Transportation transportation,
@NonNull Country country,
@NonNull String correlationId
) {
this.originCoordinate = originCoordinate;
if (destinationCoordinates instanceof RandomAccess) {
this.destinationCoordinates = destinationCoordinates;
} else {
this.destinationCoordinates = new ArrayList<>(destinationCoordinates);
}
this.travelTime = travelTime;
this.transportation = transportation;
this.country = country;
this.correlationId = correlationId;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ public class TimeFilterFastProtoRequest extends ProtoRequest<TimeFilterFastProto
@NonNull
OneToMany oneToMany;

String correlationId;

private byte[] createByteArray() {
Coordinates origin = oneToMany.getOriginCoordinate();

Expand Down Expand Up @@ -125,6 +127,15 @@ private Either<TravelTimeError, TimeFilterFastProtoResponse> parseResponse(
return Either.right(new TimeFilterFastProtoResponse(response.getProperties().getTravelTimesList()));
}

@Override
public String getCorrelationId() {
if (correlationId == null) {
return "no-x-correlation-id";
} else {
return correlationId;
}
}

@Override
public Either<TravelTimeError, Request> createRequest(HttpUrl baseUri, TravelTimeCredentials credentials) {
String countryCode = oneToMany.getCountry().getValue();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,6 @@ public TimeFilterFastProtoRequest oneToMany(
Country.UNITED_KINGDOM
);

return new TimeFilterFastProtoRequest(oneToMany);
return new TimeFilterFastProtoRequest(oneToMany, null);
}
}

0 comments on commit c9ec45a

Please sign in to comment.