Skip to content

Commit 2bd6caa

Browse files
authored
Merge pull request #5 from richard483/dev-march
Dev march
2 parents c236251 + 7adc460 commit 2bd6caa

File tree

12 files changed

+371
-41
lines changed

12 files changed

+371
-41
lines changed

pom.xml

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,27 @@
1414
<version>0.0.1-SNAPSHOT</version>
1515
<name>raven-apiclient</name>
1616
<description>raven-apiclient</description>
17-
<url/>
17+
<url>https://github.com/richard483/raven-api-client</url>
1818
<licenses>
19-
<license/>
19+
<license>
20+
<name>Apache License, Version 2.0</name>
21+
<url>http://www.apache.org/licenses/LICENSE-2.0</url>
22+
<distribution>repo</distribution>
23+
</license>
2024
</licenses>
2125
<developers>
22-
<developer/>
26+
<developer>
27+
<id>richard483</id>
28+
<name>Richard William</name>
29+
<email>richard.william483@gmail.com</email>
30+
<url>https://github.com/richard483</url>
31+
</developer>
2332
</developers>
2433
<scm>
25-
<connection/>
26-
<developerConnection/>
27-
<tag/>
28-
<url/>
34+
<connection>scm:git:git://github.com/richard483/raven-api-client.git</connection>
35+
<developerConnection>scm:git:ssh://github.com/richard483/raven-api-client.git</developerConnection>
36+
<tag>HEAD</tag>
37+
<url>https://github.com/richard483/raven-api-client</url>
2938
</scm>
3039
<properties>
3140
<java.version>21</java.version>
@@ -57,6 +66,12 @@
5766
<plugin>
5867
<groupId>org.springframework.boot</groupId>
5968
<artifactId>spring-boot-maven-plugin</artifactId>
69+
<executions>
70+
<execution>
71+
<id>repackage</id>
72+
<phase>none</phase>
73+
</execution>
74+
</executions>
6075
<configuration>
6176
<excludes>
6277
<exclude>
@@ -68,5 +83,11 @@
6883
</plugin>
6984
</plugins>
7085
</build>
71-
86+
<distributionManagement>
87+
<repository>
88+
<id>github</id>
89+
<name>richard483</name>
90+
<url>https://maven.pkg.github.com/richard483/raven-api-client</url>
91+
</repository>
92+
</distributionManagement>
7293
</project>

src/main/java/com/nephren/raven/apiclient/aop/RequestMappingMetadataBuilder.java

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import java.lang.reflect.Type;
1111
import java.util.Arrays;
1212
import java.util.HashMap;
13+
import java.util.List;
1314
import java.util.Map;
1415
import java.util.stream.Collectors;
1516
import lombok.extern.slf4j.Slf4j;
@@ -310,7 +311,7 @@ private RavenRequestMapping getRequestMappingAnnotation(Method method) {
310311
return null;
311312
}
312313

313-
private <T extends java.lang.annotation.Annotation> RavenRequestMapping getAnnotation(
314+
private <T extends Annotation> RavenRequestMapping getAnnotation(
314315
Method method, Class<T> annotationType) {
315316
try {
316317
T annotation = method.getAnnotation(annotationType);
@@ -321,14 +322,14 @@ private <T extends java.lang.annotation.Annotation> RavenRequestMapping getAnnot
321322
String[] headers = (String[]) annotation.getClass().getMethod("headers").invoke(annotation);
322323
String[] path = (String[]) annotation.getClass().getMethod("path").invoke(annotation);
323324
String[] value = (String[]) annotation.getClass().getMethod("value").invoke(annotation);
324-
RequestMethod[] methodValue = new RequestMethod[] {getRequestMethod(annotationType)};
325+
RequestMethod[] requestMethod = getRequestMethod(annotation, annotationType);
325326
return RavenRequestMapping.builder()
326327
.consumes(consumes)
327328
.produces(produces)
328329
.headers(headers)
329330
.path(path)
330331
.value(value)
331-
.method(methodValue)
332+
.method(requestMethod)
332333
.build();
333334
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
334335
log.error("#RavenApiClient getAnnotation got error trace: ");
@@ -337,13 +338,16 @@ private <T extends java.lang.annotation.Annotation> RavenRequestMapping getAnnot
337338
return null;
338339
}
339340

340-
private RequestMethod getRequestMethod(Class clazz) {
341-
return switch (clazz.getSimpleName()) {
342-
case "PutMapping" -> RequestMethod.PUT;
343-
case "PostMapping" -> RequestMethod.POST;
344-
case "PatchMapping" -> RequestMethod.PATCH;
345-
case "DeleteMapping" -> RequestMethod.DELETE;
346-
default -> RequestMethod.GET;
341+
private <T extends Annotation> RequestMethod[] getRequestMethod(T annotation,
342+
Class<T> annotationType)
343+
throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
344+
return switch (annotationType.getSimpleName()) {
345+
case "PutMapping" -> List.of(RequestMethod.PUT).toArray(new RequestMethod[0]);
346+
case "PostMapping" -> List.of(RequestMethod.POST).toArray(new RequestMethod[0]);
347+
case "PatchMapping" -> List.of(RequestMethod.PATCH).toArray(new RequestMethod[0]);
348+
case "DeleteMapping" -> List.of(RequestMethod.DELETE).toArray(new RequestMethod[0]);
349+
case "RequestMapping" -> (RequestMethod[]) annotation.getClass().getMethod("method").invoke(annotation);
350+
default -> List.of(RequestMethod.GET).toArray(new RequestMethod[0]);
347351
};
348352
}
349353

src/main/java/com/nephren/raven/apiclient/interceptor/RavenApiClientMethodInterceptor.java

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -177,8 +177,6 @@ private WebClient.RequestHeadersUriSpec<?> doMethod(String methodName) {
177177
case RequestMethod.POST -> webClient.post();
178178
case RequestMethod.PUT -> webClient.put();
179179
case RequestMethod.DELETE -> webClient.delete();
180-
case RequestMethod.HEAD -> webClient.head();
181-
case RequestMethod.OPTIONS -> webClient.options();
182180
case RequestMethod.PATCH -> webClient.patch();
183181
default -> throw new UnsupportedOperationException(
184182
"#RavenAPIClientMethodInterceptor Unsupported method: " + methodName);
@@ -187,12 +185,14 @@ private WebClient.RequestHeadersUriSpec<?> doMethod(String methodName) {
187185

188186
private WebClient.RequestHeadersSpec<?> getUriBuilder(
189187
String methodName, Object[] arguments, WebClient.RequestHeadersUriSpec<?> client) {
190-
if (metadata.getApiUrlPositions().containsKey(methodName)) {
191-
String baseUrl = (String) arguments[metadata.getApiUrlPositions().get(methodName)];
192-
return client.uri(baseUrl, uriBuilder -> getUri(uriBuilder, methodName, arguments));
193-
} else {
194-
return client.uri(uriBuilder -> getUri(uriBuilder, methodName, arguments));
195-
}
188+
// TODO: maybe we could develop the dynamic URL feature later, but for now, we will just use the static URL
189+
// if (metadata.getApiUrlPositions().containsKey(methodName)) {
190+
// String baseUrl = (String) arguments[metadata.getApiUrlPositions().get(methodName)];
191+
// return client.uri(baseUrl, uriBuilder -> getUri(uriBuilder, methodName, arguments));
192+
// } else {
193+
// return client.uri(uriBuilder -> getUri(uriBuilder, methodName, arguments));
194+
// }
195+
return client.uri(uriBuilder -> getUri(uriBuilder, methodName, arguments));
196196
}
197197

198198
private WebClient.RequestHeadersSpec<?> doHeader(
@@ -212,8 +212,8 @@ private WebClient.RequestHeadersSpec<?> doHeader(
212212
return spec;
213213
}
214214

215-
private Mono doBody(
216-
WebClient.RequestHeadersSpec<?> client, Method method, String methodName,
215+
private <T extends WebClient.RequestHeadersSpec<?>> Mono<? extends WebClient.RequestHeadersSpec<?>> doBody(
216+
T client, Method method, String methodName,
217217
Object[] arguments) {
218218
if (client instanceof WebClient.RequestBodySpec bodySpec) {
219219
String contentType = metadata.getContentTypes().get(methodName);
@@ -236,34 +236,36 @@ private Mono doBody(
236236
return Mono.just(client);
237237
}
238238

239-
private Mono doResponse(Mono<WebClient.RequestHeadersSpec<?>> client, String methodName) {
239+
private Mono doResponse(Mono<? extends WebClient.RequestHeadersSpec<?>> client, String methodName) {
240240
Type type = metadata.getResponseBodyClasses().get(methodName);
241241
return handleResponseType(client, type);
242242
}
243243

244-
private Mono handleResponseType(Mono<WebClient.RequestHeadersSpec<?>> client, Type type) {
244+
private Mono handleResponseType(Mono<? extends WebClient.RequestHeadersSpec<?>> client, Type type) {
245245
if (type instanceof ParameterizedType parameterizedType) {
246246
return handleParameterizedType(client, parameterizedType);
247247
} else {
248248
return client.flatMap(c -> c.retrieve().bodyToMono((Class) type));
249249
}
250250
}
251251

252-
private Mono handleParameterizedType(Mono<WebClient.RequestHeadersSpec<?>> client,
252+
private Mono handleParameterizedType(Mono<? extends WebClient.RequestHeadersSpec<?>> client,
253253
ParameterizedType parameterizedType) {
254254
if (ResponseEntity.class.equals(parameterizedType.getRawType())) {
255255
return handleResponseEntity(client, parameterizedType);
256256
} else {
257257
return client.flatMap(
258-
c -> c.retrieve().bodyToMono(ParameterizedTypeReference.forType(parameterizedType)));
258+
c -> c.retrieve().bodyToMono(ParameterizedTypeReference.forType(parameterizedType.getRawType())));
259259
}
260260
}
261261

262-
private Mono handleResponseEntity(Mono<WebClient.RequestHeadersSpec<?>> client, ParameterizedType parameterizedType) {
262+
private Mono handleResponseEntity(Mono<? extends WebClient.RequestHeadersSpec<?>> client,
263+
ParameterizedType parameterizedType) {
263264
return handleResponseSpec(getResponseEntitySpec(client), parameterizedType);
264265
}
265266

266-
private Mono<WebClient.ResponseSpec> getResponseEntitySpec(Mono<WebClient.RequestHeadersSpec<?>> client) {
267+
private Mono<WebClient.ResponseSpec> getResponseEntitySpec(Mono<? extends WebClient.RequestHeadersSpec<
268+
?>> client) {
267269
// TODO: need to update error handling
268270
return client.map(spec -> spec.retrieve().onStatus(HttpStatusCode::isError,
269271
clientResponse -> Mono.empty()));
@@ -280,7 +282,7 @@ private Mono handleResponseSpec(Mono<WebClient.ResponseSpec> responseSpec, Param
280282
}
281283

282284
private Mono handleListResponseSpec(Mono<WebClient.ResponseSpec> responseSpec, ParameterizedType parameterizedType) {
283-
return responseSpec.flatMap(respEntity -> respEntity.toEntityList(
285+
return responseSpec.flatMap(respEntity -> respEntity.toEntity(
284286
ParameterizedTypeReference.forType(
285287
parameterizedType.getActualTypeArguments()[0])));
286288
}

src/test/java/com/nephren/raven/apiclient/RavenApiClientGetTests.java

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
import org.springframework.boot.test.context.SpringBootTest;
88
import org.springframework.test.web.reactive.server.WebTestClient;
99

10+
import java.util.List;
11+
1012
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
1113
@AutoConfigureWebTestClient
1214
class RavenApiClientGetTests {
@@ -26,6 +28,23 @@ void getRequest() {
2628
.expectBody(String.class).isEqualTo("Hello, World!");
2729
}
2830

31+
@Test
32+
void getRequestWithRequestMapping() {
33+
webTestClient.get().uri("http://localhost:8080/get/request-mapping")
34+
.exchange()
35+
.expectStatus()
36+
.isOk()
37+
.expectBody(String.class).isEqualTo("Hello, World!");
38+
}
39+
40+
@Test
41+
void getRequestWithRequestMappingUnsupportedMethod() {
42+
webTestClient.get().uri("http://localhost:8080/get/request-mapping-unsupported")
43+
.exchange()
44+
.expectStatus()
45+
.is5xxServerError();
46+
}
47+
2948
@Test
3049
void getRequestNoPath() {
3150
webTestClient.get().uri("http://localhost:8080/get/no-path")
@@ -128,6 +147,15 @@ void getRequestWithQueryParam() {
128147
.expectBody(String.class).isEqualTo("Message received with name: Richard and age: 22");
129148
}
130149

150+
@Test
151+
void getRequestWithQueryParamCollection() {
152+
webTestClient.get().uri("http://localhost:8080/get/withQueryParamAndCollection?names=Richard,Nephren,William")
153+
.exchange()
154+
.expectStatus()
155+
.isOk()
156+
.expectBody(String.class).isEqualTo("Message received with names: [Richard, Nephren, William] with the length of: 3");
157+
}
158+
131159
@Test
132160
void getRequestWithPathVariable() {
133161
webTestClient.get().uri("http://localhost:8080/get/withPathVariable/TowaSama")
@@ -148,4 +176,34 @@ void getRequestWithCookieParam() {
148176
.isEqualTo("Message received and contain username cookie of TowaSama");
149177
}
150178

179+
@Test
180+
void getRequestList() {
181+
webTestClient.get().uri("http://localhost:8080/get/list")
182+
.exchange()
183+
.expectStatus()
184+
.isOk()
185+
.expectBody(List.class)
186+
.isEqualTo(List.of("Hello", "こんいちわ", "Hola", "Bonjour", "Hallo"));
187+
}
188+
189+
@Test
190+
void getRequestWithoutResponseEntity() {
191+
webTestClient.get().uri("http://localhost:8080/get/withoutResponseEntity")
192+
.exchange()
193+
.expectStatus()
194+
.isOk()
195+
.expectBody(String.class)
196+
.isEqualTo("Hello, World! From string without ResponseEntity");
197+
}
198+
199+
@Test
200+
void getRequestListWithoutResponseEntity() {
201+
webTestClient.get().uri("http://localhost:8080/get/listWithoutResponseEntity")
202+
.exchange()
203+
.expectStatus()
204+
.isOk()
205+
.expectBody(List.class)
206+
.isEqualTo(List.of("Hello", "こんいちわ", "Hola", "Bonjour", "Hallo"));
207+
}
208+
151209
}

0 commit comments

Comments
 (0)