|
11 | 11 | import com.cloudpdf.api.core.RequestOptions; |
12 | 12 | import com.cloudpdf.api.core.RetryInterceptor; |
13 | 13 | import com.cloudpdf.api.types.DeploymentLicenseStatusResponse; |
| 14 | +import com.cloudpdf.api.types.DeploymentModulesResponse; |
14 | 15 | import com.fasterxml.jackson.core.JsonProcessingException; |
15 | 16 | import java.io.IOException; |
16 | 17 | import java.util.concurrent.CompletableFuture; |
@@ -97,4 +98,76 @@ public void onFailure(@NotNull Call call, @NotNull IOException e) { |
97 | 98 | }); |
98 | 99 | return future; |
99 | 100 | } |
| 101 | + |
| 102 | + /** |
| 103 | + * What each module declares, for a dashboard or a script that lists modules, offers their permissions in its dialogs and shows their meters. Which tenants may use one is in their <code>features</code> (<code>tenants.get</code>); what it costs, if anything, is the operator's. |
| 104 | + */ |
| 105 | + public CompletableFuture<CloudPDFClientHttpResponse<DeploymentModulesResponse>> modules() { |
| 106 | + return modules(null); |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * What each module declares, for a dashboard or a script that lists modules, offers their permissions in its dialogs and shows their meters. Which tenants may use one is in their <code>features</code> (<code>tenants.get</code>); what it costs, if anything, is the operator's. |
| 111 | + */ |
| 112 | + public CompletableFuture<CloudPDFClientHttpResponse<DeploymentModulesResponse>> modules( |
| 113 | + RequestOptions requestOptions) { |
| 114 | + HttpUrl.Builder httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl()) |
| 115 | + .newBuilder() |
| 116 | + .addPathSegments("v1/deployment/modules"); |
| 117 | + if (requestOptions != null) { |
| 118 | + requestOptions.getQueryParameters().forEach((_key, _value) -> { |
| 119 | + httpUrl.addQueryParameter(_key, _value); |
| 120 | + }); |
| 121 | + } |
| 122 | + Request okhttpRequest = new Request.Builder() |
| 123 | + .url(httpUrl.build()) |
| 124 | + .method("GET", null) |
| 125 | + .headers(Headers.of(clientOptions.headers(requestOptions))) |
| 126 | + .addHeader("Accept", "application/json") |
| 127 | + .build(); |
| 128 | + OkHttpClient client = clientOptions.httpClient(); |
| 129 | + if (requestOptions != null && requestOptions.getTimeout().isPresent()) { |
| 130 | + client = clientOptions.httpClientWithTimeout(requestOptions); |
| 131 | + } |
| 132 | + if (requestOptions != null && requestOptions.getMaxRetries().isPresent()) { |
| 133 | + okhttpRequest = okhttpRequest |
| 134 | + .newBuilder() |
| 135 | + .tag( |
| 136 | + RetryInterceptor.MaxRetriesOverride.class, |
| 137 | + new RetryInterceptor.MaxRetriesOverride( |
| 138 | + requestOptions.getMaxRetries().get())) |
| 139 | + .build(); |
| 140 | + } |
| 141 | + CompletableFuture<CloudPDFClientHttpResponse<DeploymentModulesResponse>> future = new CompletableFuture<>(); |
| 142 | + client.newCall(okhttpRequest).enqueue(new Callback() { |
| 143 | + @Override |
| 144 | + public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { |
| 145 | + try (ResponseBody responseBody = response.body()) { |
| 146 | + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; |
| 147 | + if (response.isSuccessful()) { |
| 148 | + future.complete(new CloudPDFClientHttpResponse<>( |
| 149 | + ObjectMappers.JSON_MAPPER.readValue( |
| 150 | + responseBodyString, DeploymentModulesResponse.class), |
| 151 | + response)); |
| 152 | + return; |
| 153 | + } |
| 154 | + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); |
| 155 | + future.completeExceptionally(new CloudPDFApiException( |
| 156 | + "Error with status code " + response.code(), response.code(), errorBody, response)); |
| 157 | + return; |
| 158 | + } catch (JsonProcessingException e) { |
| 159 | + future.completeExceptionally( |
| 160 | + new CloudPDFException("Failed to deserialize response: " + e.getMessage(), e)); |
| 161 | + } catch (IOException e) { |
| 162 | + future.completeExceptionally(new CloudPDFException("Network error executing HTTP request", e)); |
| 163 | + } |
| 164 | + } |
| 165 | + |
| 166 | + @Override |
| 167 | + public void onFailure(@NotNull Call call, @NotNull IOException e) { |
| 168 | + future.completeExceptionally(new CloudPDFException("Network error executing HTTP request", e)); |
| 169 | + } |
| 170 | + }); |
| 171 | + return future; |
| 172 | + } |
100 | 173 | } |
0 commit comments