|
4 | 4 | import com.fasterxml.jackson.databind.ObjectMapper; |
5 | 5 | import io.whitefox.api.utils.ApiClient; |
6 | 6 | import io.whitefox.api.utils.ApiException; |
7 | | - |
8 | 7 | import java.io.File; |
9 | 8 | import java.io.FileInputStream; |
10 | 9 | import java.io.IOException; |
|
14 | 13 | import java.util.function.Supplier; |
15 | 14 |
|
16 | 15 | public class ApiUtils { |
17 | | - /** |
18 | | - * Returns the result of the call of the first argument (f) unless it throws an ApiException with HTTP status code 409, |
19 | | - * in that case, returns the result of the call of the second argument (defaultValue). |
20 | | - * If defaultValue is not dynamic, you can use also {@link ApiUtils#recoverConflict recoverConflinct}. |
21 | | - */ |
22 | | - public static <T> T recoverConflictLazy(Supplier<T> f, Supplier<T> defaultValue) { |
23 | | - try { |
24 | | - return f.get(); |
25 | | - } catch (ApiException e) { |
26 | | - if (e.getCode() == 409) { |
27 | | - return defaultValue.get(); |
28 | | - } else { |
29 | | - throw e; |
30 | | - } |
31 | | - } |
| 16 | + /** |
| 17 | + * Returns the result of the call of the first argument (f) unless it throws an ApiException with HTTP status code 409, |
| 18 | + * in that case, returns the result of the call of the second argument (defaultValue). |
| 19 | + * If defaultValue is not dynamic, you can use also {@link ApiUtils#recoverConflict recoverConflinct}. |
| 20 | + */ |
| 21 | + public static <T> T recoverConflictLazy(Supplier<T> f, Supplier<T> defaultValue) { |
| 22 | + try { |
| 23 | + return f.get(); |
| 24 | + } catch (ApiException e) { |
| 25 | + if (e.getCode() == 409) { |
| 26 | + return defaultValue.get(); |
| 27 | + } else { |
| 28 | + throw e; |
| 29 | + } |
32 | 30 | } |
| 31 | + } |
33 | 32 |
|
34 | | - /** |
35 | | - * Returns the result of the call of the first argument (f) unless it throws an ApiException with HTTP status code 409, |
36 | | - * in that case, returns the second argument (defaultValue). |
37 | | - * If defaultValue is dynamic, you can use also {@link ApiUtils#recoverConflictLazy recoverConflictLazy}. |
38 | | - */ |
39 | | - public static <T> T recoverConflict(Supplier<T> f, T defaultValue) { |
40 | | - return recoverConflictLazy(f, new Supplier<T>() { |
41 | | - @Override |
42 | | - public T get() { |
43 | | - return defaultValue; |
44 | | - } |
45 | | - }); |
46 | | - } |
| 33 | + /** |
| 34 | + * Returns the result of the call of the first argument (f) unless it throws an ApiException with HTTP status code 409, |
| 35 | + * in that case, returns the second argument (defaultValue). |
| 36 | + * If defaultValue is dynamic, you can use also {@link ApiUtils#recoverConflictLazy recoverConflictLazy}. |
| 37 | + */ |
| 38 | + public static <T> T recoverConflict(Supplier<T> f, T defaultValue) { |
| 39 | + return recoverConflictLazy(f, new Supplier<T>() { |
| 40 | + @Override |
| 41 | + public T get() { |
| 42 | + return defaultValue; |
| 43 | + } |
| 44 | + }); |
| 45 | + } |
47 | 46 |
|
48 | | - /** |
49 | | - * Calls the first argument (f), if the call throws an ApiException with HTTP status code 409 will swallow the exception. |
50 | | - */ |
51 | | - public static <T> void ignoreConflict(Supplier<T> f) { |
52 | | - recoverConflict(f, null); |
53 | | - } |
| 47 | + /** |
| 48 | + * Calls the first argument (f), if the call throws an ApiException with HTTP status code 409 will swallow the exception. |
| 49 | + */ |
| 50 | + public static <T> void ignoreConflict(Supplier<T> f) { |
| 51 | + recoverConflict(f, null); |
| 52 | + } |
54 | 53 |
|
55 | | - private static final ObjectMapper objectMapper = new ObjectMapper(); |
56 | | - public static final String ENDPOINT_FIELD_NAME = "endpoint"; |
57 | | - public static final String BEARER_TOKEN_FIELD_NAME = "bearerToken"; |
| 54 | + private static final ObjectMapper objectMapper = new ObjectMapper(); |
| 55 | + public static final String ENDPOINT_FIELD_NAME = "endpoint"; |
| 56 | + public static final String BEARER_TOKEN_FIELD_NAME = "bearerToken"; |
58 | 57 |
|
59 | | - /** |
60 | | - * Reads a resource named as the parameter, parses it following |
61 | | - * <a href="https://github.com/delta-io/delta-sharing/blob/main/PROTOCOL.md#profile-file-format">delta sharing specification</a> |
62 | | - * and configures an {@link ApiClient ApiClient} accordingly. |
63 | | - */ |
64 | | - public static ApiClient configureApiClientFromResource(String resourceName) { |
65 | | - try (InputStream is = ApiUtils.class.getClassLoader().getResourceAsStream(resourceName)) { |
66 | | - return configureClientInternal(objectMapper.reader().readTree(is)); |
67 | | - } catch (IOException e) { |
68 | | - throw new RuntimeException(String.format("Cannot read %s", resourceName), e); |
69 | | - } catch (NullPointerException e) { |
70 | | - throw new RuntimeException(String.format("Cannot find resource %s", resourceName), e); |
71 | | - } |
| 58 | + /** |
| 59 | + * Reads a resource named as the parameter, parses it following |
| 60 | + * <a href="https://github.com/delta-io/delta-sharing/blob/main/PROTOCOL.md#profile-file-format">delta sharing specification</a> |
| 61 | + * and configures an {@link ApiClient ApiClient} accordingly. |
| 62 | + */ |
| 63 | + public static ApiClient configureApiClientFromResource(String resourceName) { |
| 64 | + try (InputStream is = ApiUtils.class.getClassLoader().getResourceAsStream(resourceName)) { |
| 65 | + return configureClientInternal(objectMapper.reader().readTree(is)); |
| 66 | + } catch (IOException e) { |
| 67 | + throw new RuntimeException(String.format("Cannot read %s", resourceName), e); |
| 68 | + } catch (NullPointerException e) { |
| 69 | + throw new RuntimeException(String.format("Cannot find resource %s", resourceName), e); |
72 | 70 | } |
| 71 | + } |
73 | 72 |
|
74 | | - /** |
75 | | - * Reads a local file named as the parameter, parses it following |
76 | | - * <a href="https://github.com/delta-io/delta-sharing/blob/main/PROTOCOL.md#profile-file-format">delta sharing specification</a> |
77 | | - * and configures an {@link ApiClient ApiClient} accordingly. |
78 | | - */ |
79 | | - public static ApiClient configureClientFromFile(File file) { |
80 | | - try (InputStream is = new FileInputStream(file)) { |
81 | | - return configureClientInternal(objectMapper.reader().readTree(is)); |
82 | | - } catch (IOException e) { |
83 | | - throw new RuntimeException(String.format("Cannot read %s", file), e); |
84 | | - } |
| 73 | + /** |
| 74 | + * Reads a local file named as the parameter, parses it following |
| 75 | + * <a href="https://github.com/delta-io/delta-sharing/blob/main/PROTOCOL.md#profile-file-format">delta sharing specification</a> |
| 76 | + * and configures an {@link ApiClient ApiClient} accordingly. |
| 77 | + */ |
| 78 | + public static ApiClient configureClientFromFile(File file) { |
| 79 | + try (InputStream is = new FileInputStream(file)) { |
| 80 | + return configureClientInternal(objectMapper.reader().readTree(is)); |
| 81 | + } catch (IOException e) { |
| 82 | + throw new RuntimeException(String.format("Cannot read %s", file), e); |
85 | 83 | } |
| 84 | + } |
86 | 85 |
|
87 | | - private static ApiClient configureClientInternal(JsonNode conf) { |
88 | | - var endpointText = getRequiredField(conf, ENDPOINT_FIELD_NAME).asText(); |
89 | | - var token = getRequiredField(conf, BEARER_TOKEN_FIELD_NAME).asText(); |
90 | | - try { |
91 | | - var endpoint = new URI(endpointText); |
92 | | - var apiClient = new ApiClient(); |
93 | | - apiClient.setHost(endpoint.getHost()); |
94 | | - apiClient.setPort(endpoint.getPort()); |
95 | | - apiClient.setScheme(endpoint.getScheme()); |
96 | | - apiClient.setRequestInterceptor( |
97 | | - builder -> builder.header("Authorization", String.format("Bearer %s", token))); |
98 | | - return apiClient; |
99 | | - } catch (URISyntaxException u) { |
100 | | - throw new RuntimeException(String.format("Invalid endpoint syntax %s", endpointText), u); |
101 | | - } |
| 86 | + private static ApiClient configureClientInternal(JsonNode conf) { |
| 87 | + var endpointText = getRequiredField(conf, ENDPOINT_FIELD_NAME).asText(); |
| 88 | + var token = getRequiredField(conf, BEARER_TOKEN_FIELD_NAME).asText(); |
| 89 | + try { |
| 90 | + var endpoint = new URI(endpointText); |
| 91 | + var apiClient = new ApiClient(); |
| 92 | + apiClient.setHost(endpoint.getHost()); |
| 93 | + apiClient.setPort(endpoint.getPort()); |
| 94 | + apiClient.setScheme(endpoint.getScheme()); |
| 95 | + apiClient.setRequestInterceptor( |
| 96 | + builder -> builder.header("Authorization", String.format("Bearer %s", token))); |
| 97 | + return apiClient; |
| 98 | + } catch (URISyntaxException u) { |
| 99 | + throw new RuntimeException(String.format("Invalid endpoint syntax %s", endpointText), u); |
102 | 100 | } |
| 101 | + } |
103 | 102 |
|
104 | | - private static JsonNode getRequiredField(JsonNode node, String fieldName) { |
105 | | - if (node.has(fieldName)) { |
106 | | - return node.get(fieldName); |
107 | | - } else { |
108 | | - throw new RuntimeException( |
109 | | - String.format("Cannot find required field %s in %s", fieldName, node)); |
110 | | - } |
| 103 | + private static JsonNode getRequiredField(JsonNode node, String fieldName) { |
| 104 | + if (node.has(fieldName)) { |
| 105 | + return node.get(fieldName); |
| 106 | + } else { |
| 107 | + throw new RuntimeException( |
| 108 | + String.format("Cannot find required field %s in %s", fieldName, node)); |
111 | 109 | } |
| 110 | + } |
112 | 111 | } |
0 commit comments