23
23
import java .net .HttpURLConnection ;
24
24
import java .net .Proxy ;
25
25
import java .net .URI ;
26
+ import java .util .Collections ;
27
+ import java .util .List ;
26
28
import lombok .extern .slf4j .Slf4j ;
29
+ import org .javatuples .Pair ;
27
30
import software .amazon .cloudwatchlogs .emf .exception .EMFClientException ;
28
31
import software .amazon .cloudwatchlogs .emf .util .IOUtils ;
29
32
import software .amazon .cloudwatchlogs .emf .util .Jackson ;
@@ -33,24 +36,37 @@ public class ResourceFetcher {
33
36
34
37
/** Fetch a json object from a given uri and deserialize it to the specified class: clazz. */
35
38
<T > T fetch (URI endpoint , Class <T > clazz ) {
36
- String response = doReadResource (endpoint , "GET" );
39
+ String response = doReadResource (endpoint , "GET" , Collections . emptyList () );
37
40
return Jackson .fromJsonString (response , clazz );
38
41
}
39
42
43
+ /**
44
+ * Request a json object from a given uri with the provided headers and deserialize it to the
45
+ * specified class: clazz.
46
+ */
47
+ <T > T fetch (URI endpoint , String method , Class <T > clazz , List <Pair <String , String >> headers ) {
48
+ String response = doReadResource (endpoint , method , headers );
49
+ return Jackson .fromJsonString (response , clazz );
50
+ }
51
+
52
+ /** Request a string from a given uri with the provided headers */
53
+ String fetch (URI endpoint , String method , List <Pair <String , String >> headers ) {
54
+ return doReadResource (endpoint , method , headers );
55
+ }
56
+
40
57
/**
41
58
* Fetch a json object from a given uri and deserialize it to the specified class with a given
42
59
* Jackson ObjectMapper.
43
60
*/
44
61
<T > T fetch (URI endpoint , ObjectMapper objectMapper , Class <T > clazz ) {
45
- String response = doReadResource (endpoint , "GET" );
62
+ String response = doReadResource (endpoint , "GET" , Collections . emptyList () );
46
63
return Jackson .fromJsonString (response , objectMapper , clazz );
47
64
}
48
65
49
- private String doReadResource (URI endpoint , String method ) {
66
+ private String doReadResource (URI endpoint , String method , List < Pair < String , String >> headers ) {
50
67
InputStream inputStream = null ;
51
68
try {
52
-
53
- HttpURLConnection connection = connectToEndpoint (endpoint , method );
69
+ HttpURLConnection connection = connectToEndpoint (endpoint , method , headers );
54
70
55
71
int statusCode = connection .getResponseCode ();
56
72
@@ -105,13 +121,16 @@ private void handleErrorResponse(InputStream errorStream, String responseMessage
105
121
}
106
122
}
107
123
108
- private HttpURLConnection connectToEndpoint (URI endpoint , String method ) throws IOException {
124
+ private HttpURLConnection connectToEndpoint (
125
+ URI endpoint , String method , List <Pair <String , String >> headers ) throws IOException {
109
126
HttpURLConnection connection =
110
127
(HttpURLConnection ) endpoint .toURL ().openConnection (Proxy .NO_PROXY );
111
128
connection .setConnectTimeout (1000 );
112
129
connection .setReadTimeout (1000 );
113
130
connection .setRequestMethod (method );
114
131
connection .setDoOutput (true );
132
+ headers .forEach (
133
+ header -> connection .setRequestProperty (header .getValue0 (), header .getValue1 ()));
115
134
116
135
connection .connect ();
117
136
0 commit comments