|
7 | 7 | import com.microsoft.playwright.options.FilePayload; |
8 | 8 | import com.microsoft.playwright.options.FormData; |
9 | 9 | import com.microsoft.playwright.options.RequestOptions; |
| 10 | +import gov.nasa.jpl.aerie.e2e.types.workspaces.BulkPutItem; |
10 | 11 |
|
11 | 12 | import javax.json.Json; |
12 | 13 | import java.io.IOException; |
13 | 14 | import java.nio.charset.StandardCharsets; |
14 | 15 | import java.nio.file.Path; |
| 16 | +import java.util.List; |
15 | 17 | import java.util.Map; |
16 | 18 | import java.util.Optional; |
17 | 19 |
|
@@ -185,12 +187,154 @@ public void deleteWorkspace(int workspaceId) throws IOException { |
185 | 187 | * @return the APIResponse from the Workspace Server |
186 | 188 | */ |
187 | 189 | public APIResponse deleteWorkspace(String authToken, int workspaceId) { |
188 | | - final var options = RequestOptions.create() |
189 | | - .setHeader("Authorization", "Bearer "+authToken); |
| 190 | + final var options = RequestOptions.create().setHeader("Authorization", "Bearer "+authToken); |
190 | 191 | return request.delete("/ws/%d".formatted(workspaceId), options); |
191 | 192 | } |
192 | 193 |
|
193 | 194 |
|
| 195 | + /** |
| 196 | + * Call the GET endpoint in the Workspace Server |
| 197 | + * @param token The JWT token for the user making the request |
| 198 | + * @param workspaceId The workspace the item is in |
| 199 | + * @param itemPath The Path within the workspace where the item is |
| 200 | + * @return The APIResponse from the server |
| 201 | + */ |
| 202 | + public APIResponse get(String token, int workspaceId, Path itemPath) { |
| 203 | + final var options = RequestOptions.create().setHeader("Authorization", "Bearer " + token); |
| 204 | + return request.get("/ws/%d/%s".formatted(workspaceId, itemPath.toString()), options); |
| 205 | + } |
| 206 | + |
| 207 | + /** |
| 208 | + * Call the 'Bulk PUT' endpoint in the Workspace server. |
| 209 | + * @param token The JWT token for the user making the request |
| 210 | + * @param workspaceId The workspace to insert the file into |
| 211 | + * @param toPut List of things to be placed on the server. If there are file contents, it will be uploaded as file. |
| 212 | + * If the Optional is empty, it will be uploaded as a directory. |
| 213 | + * @return The APIResponse from the server |
| 214 | + */ |
| 215 | + public APIResponse bulkPut(String token, int workspaceId, List<BulkPutItem> toPut) { |
| 216 | + final var formData = FormData.create(); |
| 217 | + final var bodyArray = Json.createArrayBuilder(); |
| 218 | + |
| 219 | + // Generate the request body |
| 220 | + for(final var putItem : toPut) { |
| 221 | + bodyArray.add(putItem.toJson()); |
| 222 | + if(putItem instanceof BulkPutItem.FileBulkPutItem fileInput) { |
| 223 | + formData.append("files", fileInput.generateFilePayload()); |
| 224 | + } |
| 225 | + } |
| 226 | + |
| 227 | + // Generate the request |
| 228 | + final var options = RequestOptions |
| 229 | + .create() |
| 230 | + .setHeader("Authorization", "Bearer "+token) |
| 231 | + .setMultipart(formData.set("body", bodyArray.build().toString())); |
| 232 | + |
| 233 | + return request.put("/ws/bulk/%d".formatted(workspaceId), options); |
| 234 | + } |
| 235 | + |
| 236 | + /** |
| 237 | + * Call the 'Bulk POST' endpoint in the Workspace server to move items. |
| 238 | + * |
| 239 | + * @param token The JWT token for the user making the request |
| 240 | + * @param workspaceId The source workspace |
| 241 | + * @param paths The list of items to be affected by the request |
| 242 | + * @param destination The destination folder to place the items in |
| 243 | + * @param destinationWorkspaceId If present, the destination workspace. |
| 244 | + * @param overwrite If present, the value of the 'overwrite' flag |
| 245 | + * @return The APIResponse from the server |
| 246 | + */ |
| 247 | + public APIResponse bulkMove( |
| 248 | + String token, |
| 249 | + int workspaceId, |
| 250 | + List<Path> paths, |
| 251 | + Path destination, |
| 252 | + Optional<Integer> destinationWorkspaceId, |
| 253 | + Optional<Boolean> overwrite |
| 254 | + ) { |
| 255 | + // Generate the request body |
| 256 | + final var body = Json.createObjectBuilder().add("moveTo", destination.toString()); |
| 257 | + |
| 258 | + final var itemsArray = Json.createArrayBuilder(); |
| 259 | + paths.forEach(p -> itemsArray.add(Json.createObjectBuilder().add("path", p.toString()))); |
| 260 | + body.add("items", itemsArray); |
| 261 | + |
| 262 | + destinationWorkspaceId.ifPresent(wid -> body.add("toWorkspace", wid)); |
| 263 | + |
| 264 | + overwrite.ifPresent(o -> body.add("overwrite", o)); |
| 265 | + |
| 266 | + // Generate request |
| 267 | + final var options = RequestOptions |
| 268 | + .create() |
| 269 | + .setHeader("Authorization", "Bearer "+token) |
| 270 | + .setHeader("Content-type", "application/json") |
| 271 | + .setData(body.build().toString()); |
| 272 | + |
| 273 | + return request.post("/ws/bulk/%d".formatted(workspaceId), options); |
| 274 | + } |
| 275 | + |
| 276 | + /** |
| 277 | + * Call the 'Bulk POST' endpoint in the Workspace server to copy items. |
| 278 | + * @param token The JWT token for the user making the request |
| 279 | + * @param workspaceId The source workspace |
| 280 | + * @param paths The list of items to be affected by the request |
| 281 | + * @param destination The destination folder to place the items in |
| 282 | + * @param destinationWorkspaceId If present, the destination workspace. |
| 283 | + * @param overwrite If present, the value of the 'overwrite' flag |
| 284 | + * @return The APIResponse from the server |
| 285 | + */ |
| 286 | + public APIResponse bulkCopy( |
| 287 | + String token, |
| 288 | + int workspaceId, |
| 289 | + List<Path> paths, |
| 290 | + Path destination, |
| 291 | + Optional<Integer> destinationWorkspaceId, |
| 292 | + Optional<Boolean> overwrite |
| 293 | + ) { |
| 294 | + // Generate the request body |
| 295 | + final var body = Json.createObjectBuilder().add("copyTo", destination.toString()); |
| 296 | + |
| 297 | + final var itemsArray = Json.createArrayBuilder(); |
| 298 | + paths.forEach(p -> itemsArray.add(Json.createObjectBuilder().add("path", p.toString()))); |
| 299 | + body.add("items", itemsArray); |
| 300 | + |
| 301 | + destinationWorkspaceId.ifPresent(wid -> body.add("toWorkspace", wid)); |
| 302 | + |
| 303 | + overwrite.ifPresent(o -> body.add("overwrite", o)); |
| 304 | + |
| 305 | + // Generate request |
| 306 | + final var options = RequestOptions |
| 307 | + .create() |
| 308 | + .setHeader("Authorization", "Bearer "+token) |
| 309 | + .setHeader("Content-type", "application/json") |
| 310 | + .setData(body.build().toString()); |
| 311 | + |
| 312 | + return request.post("/ws/bulk/%d".formatted(workspaceId), options); |
| 313 | + } |
| 314 | + |
| 315 | + |
| 316 | + /** |
| 317 | + * Call the 'Bulk DELETE' endpoint in the Workspace server. |
| 318 | + * @param token The JWT token for the user making the request |
| 319 | + * @param workspaceId The source workspace |
| 320 | + * @param paths The list of items to be deleted |
| 321 | + * @return The APIResponse from the server |
| 322 | + */ |
| 323 | + public APIResponse bulkDelete(String token, int workspaceId, List<Path> paths) { |
| 324 | + // Generate the request body |
| 325 | + final var body = Json.createArrayBuilder(); |
| 326 | + paths.forEach(p -> body.add(p.toString())); |
| 327 | + |
| 328 | + // Generate request |
| 329 | + final var options = RequestOptions |
| 330 | + .create() |
| 331 | + .setHeader("Authorization", "Bearer "+token) |
| 332 | + .setHeader("Content-type", "application/json") |
| 333 | + .setData(body.build().toString()); |
| 334 | + |
| 335 | + return request.delete("/ws/bulk/%d".formatted(workspaceId), options); |
| 336 | + } |
| 337 | + |
194 | 338 | @Override |
195 | 339 | public void close() { |
196 | 340 | request.dispose(); |
|
0 commit comments