|
1
|
|
|
package com.hltech.pact.gen.domain.client.feign; |
|
2
|
|
|
|
|
3
|
|
|
import com.google.common.collect.Lists; |
|
4
|
|
|
import com.hltech.pact.gen.domain.client.ClientMethodRepresentationExtractor; |
|
5
|
|
|
import com.hltech.pact.gen.domain.client.annotation.handlers.AnnotatedMethodHandler; |
|
6
|
|
|
import com.hltech.pact.gen.domain.client.model.Body; |
|
7
|
|
|
import com.hltech.pact.gen.domain.client.model.ClientMethodRepresentation; |
|
8
|
|
|
import com.hltech.pact.gen.domain.client.model.Param; |
|
9
|
|
|
import com.hltech.pact.gen.domain.client.model.RequestRepresentation; |
|
10
|
|
|
import com.hltech.pact.gen.domain.client.model.ResponseRepresentation; |
|
11
|
|
|
import com.hltech.pact.gen.domain.client.util.RawHeadersParser; |
|
12
|
|
|
import com.hltech.pact.gen.domain.client.util.TypeExtractor; |
|
13
|
|
|
import org.springframework.http.HttpStatus; |
|
14
|
|
|
|
|
15
|
|
|
import java.lang.reflect.Method; |
|
16
|
|
|
import java.util.Arrays; |
|
17
|
|
|
import java.util.Collection; |
|
18
|
|
|
import java.util.List; |
|
19
|
|
|
import java.util.stream.Collectors; |
|
20
|
|
|
|
|
21
|
|
|
public class FeignMethodRepresentationExtractor implements ClientMethodRepresentationExtractor { |
|
22
|
|
|
|
|
23
|
|
|
private final Collection<AnnotatedMethodHandler> annotatedMethodHandlers; |
|
24
|
|
|
|
|
25
|
|
|
public FeignMethodRepresentationExtractor(Collection<AnnotatedMethodHandler> annotatedMethodHandlers) { |
|
26
|
|
|
this.annotatedMethodHandlers = annotatedMethodHandlers; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
@Override |
|
30
|
|
|
public ClientMethodRepresentation extractClientMethodRepresentation(Method clientMethod) { |
|
31
|
|
|
return ClientMethodRepresentation.builder() |
|
32
|
|
|
.requestRepresentation(extractRequestProperties(clientMethod)) |
|
33
|
|
|
.responseRepresentationList(extractResponseProperties(clientMethod)) |
|
34
|
|
|
.build(); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
private RequestRepresentation extractRequestProperties(Method feignClientMethod) { |
|
38
|
|
|
return this.annotatedMethodHandlers.stream() |
|
39
|
|
|
.filter(annotationHandler -> annotationHandler.isSupported(feignClientMethod)) |
|
40
|
|
|
.findFirst().orElseThrow(() -> new IllegalArgumentException("Unknown HTTP method")) |
|
41
|
|
|
.handle(feignClientMethod); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
private static List<ResponseRepresentation> extractResponseProperties(Method feignClientMethod) { |
|
45
|
|
|
feignClientMethod.getGenericReturnType(); |
|
46
|
|
|
|
|
47
|
|
|
List<ResponseRepresentation> results = Arrays |
|
48
|
|
|
.stream(feignClientMethod.getDeclaredAnnotationsByType(InteractionInfo.class)) |
|
49
|
|
|
.map(annotation -> populateResponse( |
|
50
|
|
|
annotation.responseStatus(), |
|
51
|
|
|
RawHeadersParser.parseAll(annotation.responseHeaders()), |
|
52
|
|
|
feignClientMethod, |
|
53
|
|
|
annotation.description(), |
|
54
|
|
|
annotation.emptyBodyExpected())) |
|
55
|
|
|
.collect(Collectors.toList()); |
|
56
|
|
|
|
|
57
|
|
|
return !results.isEmpty() ? results : |
|
58
|
|
|
Lists.newArrayList(populateResponse(HttpStatus.OK, Lists.newArrayList(), feignClientMethod, "", false)); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
private static ResponseRepresentation populateResponse(HttpStatus status, |
|
62
|
|
|
List<Param> headers, |
|
63
|
|
|
Method feignClientMethod, |
|
64
|
|
|
String description, |
|
65
|
|
|
boolean isEmptyBodyExpected) { |
|
66
|
|
|
|
|
67
|
|
|
return ResponseRepresentation.builder() |
|
68
|
|
|
.status(status) |
|
69
|
|
|
.headers(headers) |
|
70
|
|
|
.body(Body.builder() |
|
71
|
|
|
.type(feignClientMethod.getReturnType()) |
|
72
|
|
|
.genericArgumentTypes( |
|
73
|
|
|
TypeExtractor.extractParameterTypesFromType(feignClientMethod.getGenericReturnType())) |
|
74
|
|
|
.build()) |
|
75
|
|
|
.description(description) |
|
76
|
|
|
.emptyBodyExpected(isEmptyBodyExpected) |
|
77
|
|
|
.build(); |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|