|
| 1 | +package com.sap.csc.client; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider; |
| 4 | + |
| 5 | +import javax.ws.rs.client.Client; |
| 6 | +import javax.ws.rs.client.ClientBuilder; |
| 7 | +import javax.ws.rs.core.Response; |
| 8 | +import javax.ws.rs.core.UriBuilder; |
| 9 | +import java.net.URI; |
| 10 | +import java.util.HashMap; |
| 11 | +import java.util.Map; |
| 12 | + |
| 13 | +public class FlexHelper { |
| 14 | + public static final String FLEX_BASE = "https://api.flightstats.com/flex"; |
| 15 | + public static final String APP_ID = "appId"; |
| 16 | + public static final String APP_KEY = "appKey"; |
| 17 | + public static final String EXTENDED_OPTIONS = "extendedOptions"; |
| 18 | + public static final String INCLUDE_NEW_FIELDS = "includeNewFields"; |
| 19 | + public static final String INCLUDE_DELTAS = "includeDeltas"; |
| 20 | + public static final String INCLUDE_DIRECTS = "includeDirects"; |
| 21 | + public static final String INCLUDE_CARGO = "includeCargo"; |
| 22 | + public static final String INCLUDE_SURFACE = "includeSurface"; |
| 23 | + public static final String EXCLUDE_APPENDIX = "excludeAppendix"; |
| 24 | + public static final String LANGUAGE_CODE = "languageCode:%s"; |
| 25 | + private static final Client httpClient = ClientBuilder.newClient().register(JacksonJsonProvider.class); |
| 26 | + |
| 27 | + private FlexHelper() { |
| 28 | + super(); |
| 29 | + } |
| 30 | + |
| 31 | + public static <X> X executeHttpGet(URI uri, Class<X> clazz) { |
| 32 | + Response response = httpClient.target(uri).request("application/json;charset=UTF-8").get(Response.class); |
| 33 | + checkForError(uri, response); |
| 34 | + return response.readEntity(clazz); |
| 35 | + } |
| 36 | + |
| 37 | + private static void checkForError(URI source, Response response) { |
| 38 | + if (response.getStatus() >= 300) { |
| 39 | + throw new RuntimeException("http status=" + response.getStatus() + ", source: " + source + ": " |
| 40 | + + response.readEntity(String.class)); |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + public static UriBuilder createRequestUri(String path, Map<String, String> options, String appId, String appKey) { |
| 45 | + UriBuilder builder = UriBuilder.fromPath(FLEX_BASE + path).queryParam(APP_ID, appId).queryParam(APP_KEY, |
| 46 | + appKey); |
| 47 | + appendQueryParams(options, builder); |
| 48 | + return builder; |
| 49 | + } |
| 50 | + |
| 51 | + private static void appendQueryParams(Map<String, String> options, UriBuilder builder) { |
| 52 | + options = options == null ? new HashMap<>() : options; |
| 53 | + if (options.containsKey(EXTENDED_OPTIONS)) { |
| 54 | + options.put(EXTENDED_OPTIONS, options.get(EXTENDED_OPTIONS) + ",useHttpErrors"); |
| 55 | + } else { |
| 56 | + options.put(EXTENDED_OPTIONS, "useHttpErrors"); |
| 57 | + } |
| 58 | + for (Map.Entry<String, String> option : options.entrySet()) { |
| 59 | + builder.queryParam(option.getKey(), option.getValue()); |
| 60 | + } |
| 61 | + } |
| 62 | +} |
0 commit comments