Skip to content

Commit

Permalink
Merge pull request #91 from traveltime-dev/time-map/fast
Browse files Browse the repository at this point in the history
Time map/fast
  • Loading branch information
EivydasKoc authored Jan 4, 2023
2 parents d44b07b + ce2c42a commit f5a1af5
Show file tree
Hide file tree
Showing 46 changed files with 647 additions and 80 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
import com.traveltime.sdk.dto.common.Coordinates;
import com.traveltime.sdk.dto.common.Location;
import com.traveltime.sdk.dto.common.Property;
import com.traveltime.sdk.dto.common.transportationfast.Driving;
import com.traveltime.sdk.dto.requests.TimeFilterFastRequest;
import com.traveltime.sdk.dto.requests.timefilterfast.ArrivalSearches;
import com.traveltime.sdk.dto.requests.timefilterfast.transportation.Driving;
import com.traveltime.sdk.dto.requests.timefilterfast.OneToMany;
import lombok.val;

Expand Down
28 changes: 12 additions & 16 deletions src/main/java/com/traveltime/sdk/TravelTimeSDK.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,23 +78,19 @@ private <T> Either<TravelTimeError, T> parseJsonBody(TravelTimeRequest<T> reques

private <T> Either<TravelTimeError, T> deserializeProtoResponse(ProtoRequest<T> request, Response response) {
String url = response.request().url().toString();
switch(response.code()){
case 404: {
RequestError error = new RequestError("Network response is 404 (Not found). Make sure URL " +
url + " is correct.");
return Either.left(error);
}
default: {
Either<TravelTimeError, T> protoResponse = Try
.of(() -> Objects.requireNonNull(response.body()).bytes())
.toEither()
.<TravelTimeError>mapLeft(cause -> new IOError(cause, IO_CONNECTION_ERROR + cause.getMessage()))
.flatMap(request::parseBytes);

response.close();
return protoResponse;
}
if (response.code() == 404) {
RequestError error = new RequestError("Network response is 404 (Not found). Make sure URL " +
url + " is correct.");
return Either.left(error);
}
Either<TravelTimeError, T> protoResponse = Try
.of(() -> Objects.requireNonNull(response.body()).bytes())
.toEither()
.<TravelTimeError>mapLeft(cause -> new IOError(cause, IO_CONNECTION_ERROR + cause.getMessage()))
.flatMap(request::parseBytes);

response.close();
return protoResponse;
}

private <T> Either<TravelTimeError, T> getParsedResponse(TravelTimeRequest<T> request, Response response) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package com.traveltime.sdk.dto.responses.timemap;
package com.traveltime.sdk.dto.common;

import com.traveltime.sdk.dto.common.Coordinates;
import lombok.*;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Value;
import lombok.extern.jackson.Jacksonized;

import java.util.List;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.traveltime.sdk.dto.common.transportationfast;

import lombok.Value;

@Value
public class Cycling implements Transportation { }
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.traveltime.sdk.dto.common.transportationfast;

import lombok.Value;

@Value
public class CyclingAndFerry implements Transportation { }
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.traveltime.sdk.dto.common.transportationfast;

import lombok.Value;

@Value
public class Driving implements Transportation { }
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.traveltime.sdk.dto.common.transportationfast;

import lombok.Value;

@Value
public class DrivingAndFerry implements Transportation { }
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.traveltime.sdk.dto.requests.timefilterfast.transportation;
package com.traveltime.sdk.dto.common.transportationfast;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.traveltime.sdk.dto.requests.timefilterfast.Transportation;
import jakarta.validation.constraints.Positive;
import lombok.AllArgsConstructor;
import lombok.Builder;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.traveltime.sdk.dto.common.transportationfast;

import lombok.Value;

@Value
public class PublicTransport implements Transportation { }
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package com.traveltime.sdk.dto.requests.timefilterfast;
package com.traveltime.sdk.dto.common.transportationfast;

import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.traveltime.sdk.dto.requests.timefilterfast.transportation.*;

@JsonTypeInfo(
use = JsonTypeInfo.Id.NAME,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.traveltime.sdk.dto.common.transportationfast;

import lombok.Value;

@Value
public class Walking implements Transportation { }
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.traveltime.sdk.dto.common.transportationfast;

import lombok.Value;

@Value
public class WalkingAndFerry implements Transportation { }
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import okhttp3.Request;
import okhttp3.RequestBody;

import java.net.URI;
import java.util.List;

public abstract class ProtoRequest<T> {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.traveltime.sdk.dto.requests;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.traveltime.sdk.auth.TravelTimeCredentials;
import com.traveltime.sdk.dto.requests.timemapfast.ArrivalSearches;
import com.traveltime.sdk.dto.responses.TimeMapFastBoxesResponse;
import com.traveltime.sdk.dto.responses.errors.TravelTimeError;
import com.traveltime.sdk.utils.AcceptType;
import com.traveltime.sdk.utils.JsonUtils;
import io.vavr.control.Either;
import lombok.*;
import lombok.extern.jackson.Jacksonized;
import okhttp3.HttpUrl;
import okhttp3.Request;

@Value
@Builder
@Jacksonized
@AllArgsConstructor
@EqualsAndHashCode(callSuper = true)
@JsonInclude(JsonInclude.Include.NON_NULL)
public class TimeMapFastBoxesRequest extends TravelTimeRequest<TimeMapFastBoxesResponse> {
@NonNull
ArrivalSearches arrivalSearches;

@Override
public Either<TravelTimeError, Request> createRequest(HttpUrl baseUri, TravelTimeCredentials credentials) {
val uri = baseUri.newBuilder().addPathSegments("time-map/fast").build();
AcceptType acceptType = AcceptType.APPLICATION_BOUNDING_BOXES_JSON;
return JsonUtils
.toJson(this)
.map(json -> createPostRequest(credentials, uri, json, acceptType));
}

@Override
public Class<TimeMapFastBoxesResponse> responseType() {
return TimeMapFastBoxesResponse.class;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.traveltime.sdk.dto.requests;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.traveltime.sdk.auth.TravelTimeCredentials;
import com.traveltime.sdk.dto.requests.timemapfast.ArrivalSearches;
import com.traveltime.sdk.dto.responses.TimeMapFastGeoJsonResponse;
import com.traveltime.sdk.dto.responses.errors.TravelTimeError;
import com.traveltime.sdk.utils.AcceptType;
import com.traveltime.sdk.utils.JsonUtils;
import io.vavr.control.Either;
import lombok.*;
import lombok.extern.jackson.Jacksonized;
import okhttp3.HttpUrl;
import okhttp3.Request;

@Value
@Builder
@Jacksonized
@AllArgsConstructor
@EqualsAndHashCode(callSuper = true)
@JsonInclude(JsonInclude.Include.NON_NULL)
public class TimeMapFastGeoJsonRequest extends TravelTimeRequest<TimeMapFastGeoJsonResponse> {
@NonNull
ArrivalSearches arrivalSearches;

@Override
public Either<TravelTimeError, Request> createRequest(HttpUrl baseUri, TravelTimeCredentials credentials) {
val uri = baseUri.newBuilder().addPathSegments("time-map/fast").build();
return JsonUtils
.toJson(this)
.map(json -> createPostRequest(credentials, uri, json, AcceptType.APPLICATION_GEO_JSON));
}

@Override
public Class<TimeMapFastGeoJsonResponse> responseType() {
return TimeMapFastGeoJsonResponse.class;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.traveltime.sdk.dto.requests;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.traveltime.sdk.auth.TravelTimeCredentials;
import com.traveltime.sdk.dto.requests.timemapfast.ArrivalSearches;
import com.traveltime.sdk.dto.responses.TimeMapFastResponse;
import com.traveltime.sdk.dto.responses.errors.TravelTimeError;
import com.traveltime.sdk.utils.AcceptType;
import com.traveltime.sdk.utils.JsonUtils;
import io.vavr.control.Either;
import lombok.*;
import lombok.extern.jackson.Jacksonized;
import okhttp3.HttpUrl;
import okhttp3.Request;

@Value
@Builder
@Jacksonized
@AllArgsConstructor
@EqualsAndHashCode(callSuper = true)
@JsonInclude(JsonInclude.Include.NON_NULL)
public class TimeMapFastRequest extends TravelTimeRequest<TimeMapFastResponse> {
@NonNull
ArrivalSearches arrivalSearches;

@Override
public Either<TravelTimeError, Request> createRequest(HttpUrl baseUri, TravelTimeCredentials credentials) {
val uri = baseUri.newBuilder().addPathSegments("time-map/fast").build();
return JsonUtils
.toJson(this)
.map(json -> createPostRequest(credentials, uri, json, AcceptType.APPLICATION_JSON));
}

@Override
public Class<TimeMapFastResponse> responseType() {
return TimeMapFastResponse.class;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.traveltime.sdk.dto.requests;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.traveltime.sdk.auth.TravelTimeCredentials;
import com.traveltime.sdk.dto.requests.timemapfast.ArrivalSearches;
import com.traveltime.sdk.dto.responses.TimeMapFastWktResponse;
import com.traveltime.sdk.dto.responses.errors.TravelTimeError;
import com.traveltime.sdk.utils.AcceptType;
import com.traveltime.sdk.utils.JsonUtils;
import io.vavr.control.Either;
import lombok.*;
import lombok.extern.jackson.Jacksonized;
import okhttp3.HttpUrl;
import okhttp3.Request;

@Value
@Builder
@Jacksonized
@AllArgsConstructor
@EqualsAndHashCode(callSuper = true)
@JsonInclude(JsonInclude.Include.NON_NULL)
public class TimeMapFastWktRequest extends TravelTimeRequest<TimeMapFastWktResponse> {
@NonNull
ArrivalSearches arrivalSearches;
@Builder.Default
boolean withHoles = true;

private AcceptType acceptType() {
return withHoles ? AcceptType.APPLICATION_WKT_JSON : AcceptType.APPLICATION_WKT_NO_HOLES_JSON;
}

@Override
public Either<TravelTimeError, Request> createRequest(HttpUrl baseUri, TravelTimeCredentials credentials) {
val uri = baseUri.newBuilder().addPathSegments("time-map/fast").build();
return JsonUtils
.toJson(this)
.map(json -> createPostRequest(credentials, uri, json, acceptType()));
}

@Override
public Class<TimeMapFastWktResponse> responseType() {
return TimeMapFastWktResponse.class;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,7 @@ public class TimeMapWktRequest extends TravelTimeRequest<TimeMapWktResponse> {
boolean withHoles = true;

public AcceptType acceptType() {
if (withHoles) {
return AcceptType.APPLICATION_WKT_JSON;
} else {
return AcceptType.APPLICATION_WKT_NO_HOLES_JSON;
}
return withHoles ? AcceptType.APPLICATION_WKT_JSON : AcceptType.APPLICATION_WKT_NO_HOLES_JSON;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.traveltime.sdk.dto.requests.timefilterfast;

import com.traveltime.sdk.dto.common.Property;
import com.traveltime.sdk.dto.common.transportationfast.Transportation;
import lombok.*;
import lombok.extern.jackson.Jacksonized;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.traveltime.sdk.dto.requests.timefilterfast;

import com.traveltime.sdk.dto.common.Property;
import com.traveltime.sdk.dto.common.transportationfast.Transportation;
import lombok.*;
import lombok.extern.jackson.Jacksonized;

Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Loading

0 comments on commit f5a1af5

Please sign in to comment.