|
18 | 18 |
|
19 | 19 | import java.io.IOException; |
20 | 20 | import java.lang.reflect.Type; |
| 21 | +import java.util.Map; |
21 | 22 |
|
22 | | -/** |
23 | | - * Created by jkodumal on 11/2/15. |
24 | | - */ |
25 | 23 | class FeatureRequestor { |
26 | 24 |
|
27 | 25 | private final String apiKey; |
@@ -60,50 +58,90 @@ protected CloseableHttpClient createClient() { |
60 | 58 | return client; |
61 | 59 | } |
62 | 60 |
|
63 | | - <T> FeatureRep<T> makeRequest(String featureKey, boolean latest) throws IOException { |
| 61 | + Map<String, FeatureRep<?>> makeAllRequest(boolean latest) throws IOException { |
64 | 62 | Gson gson = new Gson(); |
65 | 63 | HttpCacheContext context = HttpCacheContext.create(); |
66 | 64 |
|
67 | | - String resource = latest ? "/api/eval/latest-features/" : "/api/eval/features/"; |
| 65 | + String resource = latest ? "/api/eval/latest-features" : "/api/eval/features"; |
68 | 66 |
|
69 | | - HttpGet request = config.getRequest(apiKey,resource + featureKey); |
| 67 | + HttpGet request = config.getRequest(apiKey, resource); |
70 | 68 |
|
71 | 69 | CloseableHttpResponse response = null; |
72 | 70 | try { |
73 | 71 | response = client.execute(request, context); |
74 | 72 |
|
75 | | - CacheResponseStatus responseStatus = context.getCacheResponseStatus(); |
76 | | - |
77 | | - switch (responseStatus) { |
78 | | - case CACHE_HIT: |
79 | | - logger.debug("A response was generated from the cache with " + |
80 | | - "no requests sent upstream"); |
81 | | - break; |
82 | | - case CACHE_MODULE_RESPONSE: |
83 | | - logger.debug("The response was generated directly by the " + |
84 | | - "caching module"); |
85 | | - break; |
86 | | - case CACHE_MISS: |
87 | | - logger.debug("The response came from an upstream server"); |
88 | | - break; |
89 | | - case VALIDATED: |
90 | | - logger.debug("The response was generated from the cache " + |
91 | | - "after validating the entry with the origin server"); |
92 | | - break; |
| 73 | + logCacheResponse(context.getCacheResponseStatus()); |
| 74 | + |
| 75 | + handleResponseStatus(response.getStatusLine().getStatusCode(), null); |
| 76 | + |
| 77 | + Type type = new TypeToken<Map<String, FeatureRep<?>>>() {}.getType(); |
| 78 | + |
| 79 | + Map<String, FeatureRep<?>> result = gson.fromJson(EntityUtils.toString(response.getEntity()), type); |
| 80 | + return result; |
| 81 | + } |
| 82 | + finally { |
| 83 | + try { |
| 84 | + if (response != null) response.close(); |
| 85 | + } catch (IOException e) { |
93 | 86 | } |
| 87 | + } |
| 88 | + } |
94 | 89 |
|
95 | | - int status = response.getStatusLine().getStatusCode(); |
| 90 | + void logCacheResponse(CacheResponseStatus status) { |
| 91 | + switch (status) { |
| 92 | + case CACHE_HIT: |
| 93 | + logger.debug("A response was generated from the cache with " + |
| 94 | + "no requests sent upstream"); |
| 95 | + break; |
| 96 | + case CACHE_MODULE_RESPONSE: |
| 97 | + logger.debug("The response was generated directly by the " + |
| 98 | + "caching module"); |
| 99 | + break; |
| 100 | + case CACHE_MISS: |
| 101 | + logger.debug("The response came from an upstream server"); |
| 102 | + break; |
| 103 | + case VALIDATED: |
| 104 | + logger.debug("The response was generated from the cache " + |
| 105 | + "after validating the entry with the origin server"); |
| 106 | + break; |
| 107 | + } |
| 108 | + } |
96 | 109 |
|
97 | | - if (status != HttpStatus.SC_OK) { |
98 | | - if (status == HttpStatus.SC_UNAUTHORIZED) { |
99 | | - logger.error("Invalid API key"); |
100 | | - } else if (status == HttpStatus.SC_NOT_FOUND) { |
| 110 | + void handleResponseStatus(int status, String featureKey) throws IOException { |
| 111 | + |
| 112 | + if (status != HttpStatus.SC_OK) { |
| 113 | + if (status == HttpStatus.SC_UNAUTHORIZED) { |
| 114 | + logger.error("Invalid API key"); |
| 115 | + } else if (status == HttpStatus.SC_NOT_FOUND) { |
| 116 | + if (featureKey != null) { |
101 | 117 | logger.error("Unknown feature key: " + featureKey); |
102 | | - } else { |
103 | | - logger.error("Unexpected status code: " + status); |
104 | 118 | } |
105 | | - throw new IOException("Failed to fetch flag"); |
| 119 | + else { |
| 120 | + logger.error("Resource not found"); |
| 121 | + } |
| 122 | + } else { |
| 123 | + logger.error("Unexpected status code: " + status); |
106 | 124 | } |
| 125 | + throw new IOException("Failed to fetch flag"); |
| 126 | + } |
| 127 | + |
| 128 | + } |
| 129 | + |
| 130 | + <T> FeatureRep<T> makeRequest(String featureKey, boolean latest) throws IOException { |
| 131 | + Gson gson = new Gson(); |
| 132 | + HttpCacheContext context = HttpCacheContext.create(); |
| 133 | + |
| 134 | + String resource = latest ? "/api/eval/latest-features/" : "/api/eval/features/"; |
| 135 | + |
| 136 | + HttpGet request = config.getRequest(apiKey,resource + featureKey); |
| 137 | + |
| 138 | + CloseableHttpResponse response = null; |
| 139 | + try { |
| 140 | + response = client.execute(request, context); |
| 141 | + |
| 142 | + logCacheResponse(context.getCacheResponseStatus()); |
| 143 | + |
| 144 | + handleResponseStatus(response.getStatusLine().getStatusCode(), featureKey); |
107 | 145 |
|
108 | 146 | Type type = new TypeToken<FeatureRep<T>>() {}.getType(); |
109 | 147 |
|
|
0 commit comments