Skip to content

Commit fb378dd

Browse files
author
derklaro
committed
feat: add api client for brouter
1 parent 955a294 commit fb378dd

File tree

10 files changed

+578
-3
lines changed

10 files changed

+578
-3
lines changed

external-api-client/src/main/java/tools/simrail/backend/external/FeignClientProvider.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,13 @@
3737
import feign.ExceptionPropagationPolicy;
3838
import feign.Feign;
3939
import feign.Logger;
40-
import feign.http2client.Http2Client;
4140
import feign.jackson.JacksonDecoder;
4241
import feign.jackson.JacksonEncoder;
4342
import feign.slf4j.Slf4jLogger;
4443
import java.util.Set;
4544
import org.jetbrains.annotations.NotNull;
45+
import tools.simrail.backend.external.feign.CustomFieldQueryMapEncoder;
46+
import tools.simrail.backend.external.feign.FeignJava11Client;
4647

4748
public final class FeignClientProvider {
4849

@@ -56,9 +57,10 @@ private FeignClientProvider() {
5657
public static @NotNull Feign.Builder prepareFeignInstance() {
5758
var callingClass = CLASS_REF_RETAINING_STACK_WALKER.getCallerClass();
5859
return Feign.builder()
59-
.client(new Http2Client())
6060
.logLevel(Logger.Level.FULL)
61+
.client(new FeignJava11Client())
6162
.logger(new Slf4jLogger(callingClass))
63+
.queryMapEncoder(new CustomFieldQueryMapEncoder())
6264
.exceptionPropagationPolicy(ExceptionPropagationPolicy.NONE);
6365
}
6466

@@ -81,11 +83,12 @@ private FeignClientProvider() {
8183
// create base feign instance
8284
var callingClass = CLASS_REF_RETAINING_STACK_WALKER.getCallerClass();
8385
return Feign.builder()
84-
.client(new Http2Client())
8586
.logLevel(Logger.Level.FULL)
87+
.client(new FeignJava11Client())
8688
.logger(new Slf4jLogger(callingClass))
8789
.encoder(new JacksonEncoder(bodyMapper))
8890
.decoder(new JacksonDecoder(bodyMapper))
91+
.queryMapEncoder(new CustomFieldQueryMapEncoder())
8992
.exceptionPropagationPolicy(ExceptionPropagationPolicy.NONE);
9093
}
9194
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* This file is part of simrail-tools-backend, licensed under the MIT License (MIT).
3+
*
4+
* Copyright (c) 2024-2025 Pasqual Koschmieder and contributors
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
25+
package tools.simrail.backend.external.brouter;
26+
27+
import feign.QueryMap;
28+
import feign.RequestLine;
29+
import org.jetbrains.annotations.Contract;
30+
import org.jetbrains.annotations.NotNull;
31+
import tools.simrail.backend.external.FeignClientProvider;
32+
import tools.simrail.backend.external.brouter.request.BRouterRouteRequest;
33+
34+
public interface BRouterApiClient {
35+
36+
@Contract("-> new")
37+
static @NotNull BRouterApiClient create() {
38+
return FeignClientProvider.prepareFeignInstance()
39+
.target(BRouterApiClient.class, "https://brouter.de");
40+
}
41+
42+
/**
43+
* Requests the routing along the given waypoints.
44+
*
45+
* @param request the request holding the routing options.
46+
* @return a stream of the routing output data.
47+
*/
48+
@RequestLine("GET /brouter")
49+
String route(@QueryMap BRouterRouteRequest request);
50+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* This file is part of simrail-tools-backend, licensed under the MIT License (MIT).
3+
*
4+
* Copyright (c) 2024-2025 Pasqual Koschmieder and contributors
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
25+
package tools.simrail.backend.external.brouter.feign;
26+
27+
import feign.Param;
28+
import org.jetbrains.annotations.NotNull;
29+
import tools.simrail.backend.external.brouter.request.BRouterRouteRequest;
30+
31+
/**
32+
* Expander for the alternative routing mode.
33+
*/
34+
public final class BRouterAlternativeModeExpander implements Param.Expander {
35+
36+
@Override
37+
public @NotNull String expand(@NotNull Object value) {
38+
var mode = (BRouterRouteRequest.AlternativeMode) value;
39+
return Integer.toString(mode.ordinal());
40+
}
41+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* This file is part of simrail-tools-backend, licensed under the MIT License (MIT).
3+
*
4+
* Copyright (c) 2024-2025 Pasqual Koschmieder and contributors
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
25+
package tools.simrail.backend.external.brouter.feign;
26+
27+
import feign.Param;
28+
import java.util.List;
29+
import java.util.StringJoiner;
30+
import org.jetbrains.annotations.NotNull;
31+
import tools.simrail.backend.external.brouter.request.BRouterRouteRequest;
32+
33+
/**
34+
* Param expander for a list of geo positions.
35+
*/
36+
public final class BRouterLonLatExpander implements Param.Expander {
37+
38+
@Override
39+
@SuppressWarnings("unchecked")
40+
public @NotNull String expand(@NotNull Object value) {
41+
var positionsJoiner = new StringJoiner("|");
42+
var positionsList = (List<BRouterRouteRequest.GeoPosition>) value;
43+
for (var position : positionsList) {
44+
var formattedPosition = String.format("%s,%s", position.longitude(), position.latitude());
45+
positionsJoiner.add(formattedPosition);
46+
}
47+
48+
return positionsJoiner.toString();
49+
}
50+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* This file is part of simrail-tools-backend, licensed under the MIT License (MIT).
3+
*
4+
* Copyright (c) 2024-2025 Pasqual Koschmieder and contributors
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
25+
package tools.simrail.backend.external.brouter.feign;
26+
27+
import feign.Param;
28+
import org.jetbrains.annotations.NotNull;
29+
import tools.simrail.backend.external.brouter.request.BRouterRouteRequest;
30+
31+
/**
32+
* Expander for the output format of a routing request.
33+
*/
34+
public final class BRouterOutputFormatExpander implements Param.Expander {
35+
36+
@Override
37+
public @NotNull String expand(@NotNull Object value) {
38+
var format = (BRouterRouteRequest.OutputFormat) value;
39+
return switch (format) {
40+
case GPX -> "gpx";
41+
case KML -> "kml";
42+
case GEOJSON -> "geojson";
43+
};
44+
}
45+
}
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
/*
2+
* This file is part of simrail-tools-backend, licensed under the MIT License (MIT).
3+
*
4+
* Copyright (c) 2024-2025 Pasqual Koschmieder and contributors
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
25+
package tools.simrail.backend.external.brouter.request;
26+
27+
import feign.Param;
28+
import java.util.List;
29+
import lombok.AllArgsConstructor;
30+
import lombok.Data;
31+
import lombok.NoArgsConstructor;
32+
import lombok.With;
33+
import tools.simrail.backend.external.brouter.feign.BRouterAlternativeModeExpander;
34+
import tools.simrail.backend.external.brouter.feign.BRouterLonLatExpander;
35+
import tools.simrail.backend.external.brouter.feign.BRouterOutputFormatExpander;
36+
37+
@Data
38+
@With
39+
@AllArgsConstructor
40+
@NoArgsConstructor(staticName = "create")
41+
public final class BRouterRouteRequest {
42+
43+
/**
44+
* The positions of the waypoints on the route.
45+
*/
46+
@Param(value = "lonlats", expander = BRouterLonLatExpander.class)
47+
private List<GeoPosition> positions;
48+
49+
/**
50+
* The profile to use for routing.
51+
*/
52+
@Param(value = "profile")
53+
private String profile;
54+
55+
/**
56+
* The alternative routing engine to use for the request.
57+
*/
58+
@Param(value = "alternativeidx", expander = BRouterAlternativeModeExpander.class)
59+
private AlternativeMode alternativeMode = AlternativeMode.DEFAULT;
60+
61+
/**
62+
* The format to return the data in.
63+
*/
64+
@Param(value = "format", expander = BRouterOutputFormatExpander.class)
65+
private OutputFormat outputFormat = OutputFormat.GEOJSON;
66+
67+
/**
68+
* A geo position.
69+
*
70+
* @param latitude the latitude of the position.
71+
* @param longitude the longitude of the position.
72+
*/
73+
public record GeoPosition(double latitude, double longitude) {
74+
75+
}
76+
77+
/**
78+
* The output format of the routing data.
79+
*/
80+
public enum OutputFormat {
81+
82+
/**
83+
* Output the data in Keyhole Markup Language.
84+
*/
85+
KML,
86+
/**
87+
* Output the data in GPS Exchange Format.
88+
*/
89+
GPX,
90+
/**
91+
* Output the data in GeoJson format.
92+
*/
93+
GEOJSON,
94+
}
95+
96+
/**
97+
* Which alternative routing mode to use.
98+
*/
99+
public enum AlternativeMode {
100+
101+
/**
102+
* Default routing engine.
103+
*/
104+
DEFAULT,
105+
/**
106+
* First alternative routing engine.
107+
*/
108+
FIRST_ALTERNATIVE,
109+
/**
110+
* Second alternative routing engine.
111+
*/
112+
SECOND_ALTERNATIVE,
113+
/**
114+
* Third alternative routing engine.
115+
*/
116+
THIRD_ALTERNATIVE,
117+
}
118+
}

0 commit comments

Comments
 (0)