Skip to content

Commit 947d665

Browse files
committed
#9 - ENH: Add PathConversion with static helper methods to convert common types to String for path building
1 parent 49985b2 commit 947d665

File tree

6 files changed

+295
-5
lines changed

6 files changed

+295
-5
lines changed

client/src/main/java/io/avaje/http/client/DHttpClientRequest.java

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,8 @@
1212
import java.nio.charset.StandardCharsets;
1313
import java.nio.file.Path;
1414
import java.time.Duration;
15-
import java.util.ArrayList;
16-
import java.util.LinkedHashMap;
17-
import java.util.List;
18-
import java.util.Map;
19-
import java.util.Optional;
15+
import java.time.LocalDate;
16+
import java.util.*;
2017
import java.util.function.Supplier;
2118
import java.util.stream.Stream;
2219

@@ -94,6 +91,30 @@ public HttpClientRequest path(String path) {
9491
return this;
9592
}
9693

94+
@Override
95+
public HttpClientRequest path(int val) {
96+
url.path(val);
97+
return this;
98+
}
99+
100+
@Override
101+
public HttpClientRequest path(long val) {
102+
url.path(val);
103+
return this;
104+
}
105+
106+
@Override
107+
public HttpClientRequest path(UUID val) {
108+
url.path(val);
109+
return this;
110+
}
111+
112+
@Override
113+
public HttpClientRequest path(LocalDate val) {
114+
url.path(val);
115+
return this;
116+
}
117+
97118
@Override
98119
public HttpClientRequest matrixParam(String name, String value) {
99120
url.matrixParam(name, value);

client/src/main/java/io/avaje/http/client/HttpClientRequest.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import java.net.http.HttpRequest;
66
import java.nio.file.Path;
77
import java.time.Duration;
8+
import java.time.LocalDate;
9+
import java.util.UUID;
810
import java.util.function.Supplier;
911

1012
/**
@@ -85,6 +87,38 @@ public interface HttpClientRequest {
8587
*/
8688
HttpClientRequest path(String path);
8789

90+
/**
91+
* Add a path segment to the URL.
92+
*
93+
* @param val The value to add to the URL path.
94+
* @return The request being built
95+
*/
96+
HttpClientRequest path(int val);
97+
98+
/**
99+
* Add a path segment to the URL.
100+
*
101+
* @param val The value to add to the URL path.
102+
* @return The request being built
103+
*/
104+
HttpClientRequest path(long val);
105+
106+
/**
107+
* Add a path segment to the URL.
108+
*
109+
* @param val The value to add to the URL path.
110+
* @return The request being built
111+
*/
112+
HttpClientRequest path(UUID val);
113+
114+
/**
115+
* Add a path segment to the URL.
116+
*
117+
* @param val The value to add to the URL path.
118+
* @return The request being built
119+
*/
120+
HttpClientRequest path(LocalDate val);
121+
88122
/**
89123
* Add a matrix parameter to the current path segment.
90124
*
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
package io.avaje.http.client;
2+
3+
import java.math.BigDecimal;
4+
import java.time.*;
5+
import java.util.UUID;
6+
7+
/**
8+
* Helper methods to convert common types to String path values.
9+
*/
10+
public class PathConversion {
11+
12+
/**
13+
* Convert to path.
14+
*/
15+
public static String toPath(int val) {
16+
return Integer.toString(val);
17+
}
18+
19+
/**
20+
* Convert to path.
21+
*/
22+
public static String toPath(long val) {
23+
return Long.toString(val);
24+
}
25+
26+
/**
27+
* Convert to path.
28+
*/
29+
public static String toPath(boolean val) {
30+
return Boolean.toString(val);
31+
}
32+
33+
/**
34+
* Convert to path.
35+
*/
36+
public static String toPath(UUID val) {
37+
return val.toString();
38+
}
39+
40+
/**
41+
* Convert to path.
42+
*/
43+
public static String toPath(LocalDate val) {
44+
return val.toString();
45+
}
46+
47+
/**
48+
* Convert to path.
49+
*/
50+
public static String toPath(LocalTime val) {
51+
return val.toString();
52+
}
53+
54+
/**
55+
* Convert to path.
56+
*/
57+
public static String toPath(LocalDateTime val) {
58+
return val.toString();
59+
}
60+
61+
/**
62+
* Convert to path.
63+
*/
64+
public static String toPath(Instant val) {
65+
return val.toString();
66+
}
67+
68+
/**
69+
* Convert to path.
70+
*/
71+
public static String toPath(OffsetDateTime val) {
72+
return val.toString();
73+
}
74+
75+
/**
76+
* Convert to path.
77+
*/
78+
public static String toPath(ZonedDateTime val) {
79+
return val.toString();
80+
}
81+
82+
/**
83+
* Convert to path.
84+
*/
85+
public static String toPath(ZoneId val) {
86+
return val.toString();
87+
}
88+
89+
/**
90+
* Convert to path.
91+
*/
92+
public static String toPath(BigDecimal val) {
93+
return val.toString();
94+
}
95+
96+
/**
97+
* Convert to path.
98+
*/
99+
public static String toPath(double val) {
100+
return Double.toString(val);
101+
}
102+
103+
/**
104+
* Convert to path.
105+
*/
106+
public static String toPath(float val) {
107+
return Float.toString(val);
108+
}
109+
110+
}

client/src/main/java/io/avaje/http/client/UrlBuilder.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import java.net.URLEncoder;
44
import java.nio.charset.StandardCharsets;
5+
import java.time.LocalDate;
6+
import java.util.UUID;
57

68
/**
79
* Build a URL typically using a base url and adding path and query parameters.
@@ -38,6 +40,34 @@ public UrlBuilder path(String path) {
3840
return this;
3941
}
4042

43+
/**
44+
* Add a path segment to the url.
45+
*/
46+
public UrlBuilder path(int val) {
47+
return path(Integer.toString(val));
48+
}
49+
50+
/**
51+
* Add a path segment to the url.
52+
*/
53+
public UrlBuilder path(long val) {
54+
return path(Long.toString(val));
55+
}
56+
57+
/**
58+
* Add a path segment to the url.
59+
*/
60+
public UrlBuilder path(LocalDate val) {
61+
return path(val.toString());
62+
}
63+
64+
/**
65+
* Add a path segment to the url.
66+
*/
67+
public UrlBuilder path(UUID val) {
68+
return path(val.toString());
69+
}
70+
4171
/**
4272
* Append a query parameter.
4373
* <p>
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package io.avaje.http.client;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import java.math.BigDecimal;
6+
import java.time.*;
7+
import java.util.UUID;
8+
9+
import static org.junit.jupiter.api.Assertions.*;
10+
11+
class PathConversionTest {
12+
13+
@Test
14+
void toPath() {
15+
assertEquals("1.3", PathConversion.toPath(1.3d));
16+
assertEquals("1.3", PathConversion.toPath(1.3f));
17+
assertEquals("42", PathConversion.toPath(42));
18+
assertEquals("87", PathConversion.toPath(87L));
19+
assertEquals("true", PathConversion.toPath(true));
20+
assertEquals("false", PathConversion.toPath(false));
21+
}
22+
23+
@Test
24+
void toPath_BigDecimal() {
25+
BigDecimal val = new BigDecimal("42.567");
26+
assertEquals("42.567", PathConversion.toPath(val));
27+
}
28+
29+
@Test
30+
void toPath_date() {
31+
LocalDate date = LocalDate.of(2020, 2, 5);
32+
assertEquals("2020-02-05", PathConversion.toPath(date));
33+
}
34+
35+
@Test
36+
void toPath_uuid() {
37+
UUID uuid = UUID.randomUUID();
38+
assertEquals(uuid.toString(), PathConversion.toPath(uuid));
39+
}
40+
41+
@Test
42+
void toPath_zoneId() {
43+
ZoneId zoneId = ZoneId.systemDefault();
44+
assertEquals(zoneId.toString(), PathConversion.toPath(zoneId));
45+
}
46+
47+
@Test
48+
void toPath_OffsetDateTime() {
49+
OffsetDateTime now = OffsetDateTime.now();
50+
assertEquals(now.toString(), PathConversion.toPath(now));
51+
}
52+
53+
@Test
54+
void toPath_ZonedDateTime() {
55+
ZonedDateTime now = ZonedDateTime.now();
56+
assertEquals(now.toString(), PathConversion.toPath(now));
57+
}
58+
59+
@Test
60+
void toPath_Instant() {
61+
Instant now = Instant.now();
62+
assertEquals(now.toString(), PathConversion.toPath(now));
63+
}
64+
65+
@Test
66+
void toPath_LocalTime() {
67+
LocalTime val = LocalTime.of(13, 43);
68+
assertEquals("13:43", PathConversion.toPath(val));
69+
}
70+
71+
@Test
72+
void toPath_LocalDateTime() {
73+
LocalTime time = LocalTime.of(13, 43);
74+
LocalDate day = LocalDate.of(2020, 1, 4);
75+
LocalDateTime dayTime = LocalDateTime.of(day, time);
76+
77+
assertEquals("2020-01-04T13:43", PathConversion.toPath(dayTime));
78+
assertEquals("2020-01-04", PathConversion.toPath(day));
79+
}
80+
81+
}

client/src/test/java/io/avaje/http/client/UrlBuilderTest.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
import org.junit.jupiter.api.Test;
44

5+
import java.time.LocalDate;
6+
import java.util.UUID;
7+
58
import static org.assertj.core.api.Assertions.assertThat;
69

710
class UrlBuilderTest {
@@ -17,6 +20,17 @@ void path() {
1720
assertThat(new UrlBuilder("https://foo").path("bar").build()).isEqualTo("https://foo/bar");
1821
}
1922

23+
` @Test
24+
void path_otherTypes() {
25+
LocalDate date = LocalDate.of(2020, 5, 12);
26+
UUID uuid = UUID.randomUUID();
27+
assertThat(new UrlBuilder("https://foo")
28+
.path(uuid)
29+
.path(42)
30+
.path(date)
31+
.path(98L).build()).isEqualTo("https://foo/" + uuid + "/42/2020-05-12/98");
32+
}
33+
2034
@Test
2135
void matrixParam() {
2236
final String url = new UrlBuilder("https://foo").path("bar").matrixParam("a", "one").matrixParam("b", "two")

0 commit comments

Comments
 (0)