001package com.box.sdkgen.managers.filemetadata; 002 003import static com.box.sdkgen.internal.utils.UtilsManager.convertToString; 004import static com.box.sdkgen.internal.utils.UtilsManager.entryOf; 005import static com.box.sdkgen.internal.utils.UtilsManager.mapOf; 006import static com.box.sdkgen.internal.utils.UtilsManager.mergeMaps; 007import static com.box.sdkgen.internal.utils.UtilsManager.prepareParams; 008 009import com.box.sdkgen.networking.auth.Authentication; 010import com.box.sdkgen.networking.fetchoptions.FetchOptions; 011import com.box.sdkgen.networking.fetchoptions.ResponseFormat; 012import com.box.sdkgen.networking.fetchresponse.FetchResponse; 013import com.box.sdkgen.networking.network.NetworkSession; 014import com.box.sdkgen.schemas.metadatafull.MetadataFull; 015import com.box.sdkgen.schemas.metadatas.Metadatas; 016import com.box.sdkgen.serialization.json.JsonManager; 017import java.util.List; 018import java.util.Map; 019 020public class FileMetadataManager { 021 022 public Authentication auth; 023 024 public NetworkSession networkSession; 025 026 public FileMetadataManager() { 027 this.networkSession = new NetworkSession(); 028 } 029 030 protected FileMetadataManager(Builder builder) { 031 this.auth = builder.auth; 032 this.networkSession = builder.networkSession; 033 } 034 035 /** 036 * Retrieves all metadata for a given file. 037 * 038 * @param fileId The unique identifier that represents a file. 039 * <p>The ID for any file can be determined by visiting a file in the web application and 040 * copying the ID from the URL. For example, for the URL `https://*.app.box.com/files/123` the 041 * `file_id` is `123`. Example: "12345" 042 */ 043 public Metadatas getFileMetadata(String fileId) { 044 return getFileMetadata(fileId, new GetFileMetadataQueryParams(), new GetFileMetadataHeaders()); 045 } 046 047 /** 048 * Retrieves all metadata for a given file. 049 * 050 * @param fileId The unique identifier that represents a file. 051 * <p>The ID for any file can be determined by visiting a file in the web application and 052 * copying the ID from the URL. For example, for the URL `https://*.app.box.com/files/123` the 053 * `file_id` is `123`. Example: "12345" 054 * @param queryParams Query parameters of getFileMetadata method 055 */ 056 public Metadatas getFileMetadata(String fileId, GetFileMetadataQueryParams queryParams) { 057 return getFileMetadata(fileId, queryParams, new GetFileMetadataHeaders()); 058 } 059 060 /** 061 * Retrieves all metadata for a given file. 062 * 063 * @param fileId The unique identifier that represents a file. 064 * <p>The ID for any file can be determined by visiting a file in the web application and 065 * copying the ID from the URL. For example, for the URL `https://*.app.box.com/files/123` the 066 * `file_id` is `123`. Example: "12345" 067 * @param headers Headers of getFileMetadata method 068 */ 069 public Metadatas getFileMetadata(String fileId, GetFileMetadataHeaders headers) { 070 return getFileMetadata(fileId, new GetFileMetadataQueryParams(), headers); 071 } 072 073 /** 074 * Retrieves all metadata for a given file. 075 * 076 * @param fileId The unique identifier that represents a file. 077 * <p>The ID for any file can be determined by visiting a file in the web application and 078 * copying the ID from the URL. For example, for the URL `https://*.app.box.com/files/123` the 079 * `file_id` is `123`. Example: "12345" 080 * @param queryParams Query parameters of getFileMetadata method 081 * @param headers Headers of getFileMetadata method 082 */ 083 public Metadatas getFileMetadata( 084 String fileId, GetFileMetadataQueryParams queryParams, GetFileMetadataHeaders headers) { 085 Map<String, String> queryParamsMap = 086 prepareParams(mapOf(entryOf("view", convertToString(queryParams.getView())))); 087 Map<String, String> headersMap = prepareParams(mergeMaps(mapOf(), headers.getExtraHeaders())); 088 FetchResponse response = 089 this.networkSession 090 .getNetworkClient() 091 .fetch( 092 new FetchOptions.Builder( 093 String.join( 094 "", 095 this.networkSession.getBaseUrls().getBaseUrl(), 096 "/2.0/files/", 097 convertToString(fileId), 098 "/metadata"), 099 "GET") 100 .params(queryParamsMap) 101 .headers(headersMap) 102 .responseFormat(ResponseFormat.JSON) 103 .auth(this.auth) 104 .networkSession(this.networkSession) 105 .build()); 106 return JsonManager.deserialize(response.getData(), Metadatas.class); 107 } 108 109 /** 110 * Retrieves the instance of a metadata template that has been applied to a file. 111 * 112 * @param fileId The unique identifier that represents a file. 113 * <p>The ID for any file can be determined by visiting a file in the web application and 114 * copying the ID from the URL. For example, for the URL `https://*.app.box.com/files/123` the 115 * `file_id` is `123`. Example: "12345" 116 * @param scope The scope of the metadata template. Example: "global" 117 * @param templateKey The name of the metadata template. Example: "properties" 118 */ 119 public MetadataFull getFileMetadataById( 120 String fileId, GetFileMetadataByIdScope scope, String templateKey) { 121 return getFileMetadataById( 122 fileId, 123 scope, 124 templateKey, 125 new GetFileMetadataByIdQueryParams(), 126 new GetFileMetadataByIdHeaders()); 127 } 128 129 /** 130 * Retrieves the instance of a metadata template that has been applied to a file. 131 * 132 * @param fileId The unique identifier that represents a file. 133 * <p>The ID for any file can be determined by visiting a file in the web application and 134 * copying the ID from the URL. For example, for the URL `https://*.app.box.com/files/123` the 135 * `file_id` is `123`. Example: "12345" 136 * @param scope The scope of the metadata template. Example: "global" 137 * @param templateKey The name of the metadata template. Example: "properties" 138 * @param queryParams Query parameters of getFileMetadataById method 139 */ 140 public MetadataFull getFileMetadataById( 141 String fileId, 142 GetFileMetadataByIdScope scope, 143 String templateKey, 144 GetFileMetadataByIdQueryParams queryParams) { 145 return getFileMetadataById( 146 fileId, scope, templateKey, queryParams, new GetFileMetadataByIdHeaders()); 147 } 148 149 /** 150 * Retrieves the instance of a metadata template that has been applied to a file. 151 * 152 * @param fileId The unique identifier that represents a file. 153 * <p>The ID for any file can be determined by visiting a file in the web application and 154 * copying the ID from the URL. For example, for the URL `https://*.app.box.com/files/123` the 155 * `file_id` is `123`. Example: "12345" 156 * @param scope The scope of the metadata template. Example: "global" 157 * @param templateKey The name of the metadata template. Example: "properties" 158 * @param headers Headers of getFileMetadataById method 159 */ 160 public MetadataFull getFileMetadataById( 161 String fileId, 162 GetFileMetadataByIdScope scope, 163 String templateKey, 164 GetFileMetadataByIdHeaders headers) { 165 return getFileMetadataById( 166 fileId, scope, templateKey, new GetFileMetadataByIdQueryParams(), headers); 167 } 168 169 /** 170 * Retrieves the instance of a metadata template that has been applied to a file. 171 * 172 * @param fileId The unique identifier that represents a file. 173 * <p>The ID for any file can be determined by visiting a file in the web application and 174 * copying the ID from the URL. For example, for the URL `https://*.app.box.com/files/123` the 175 * `file_id` is `123`. Example: "12345" 176 * @param scope The scope of the metadata template. Example: "global" 177 * @param templateKey The name of the metadata template. Example: "properties" 178 * @param queryParams Query parameters of getFileMetadataById method 179 * @param headers Headers of getFileMetadataById method 180 */ 181 public MetadataFull getFileMetadataById( 182 String fileId, 183 GetFileMetadataByIdScope scope, 184 String templateKey, 185 GetFileMetadataByIdQueryParams queryParams, 186 GetFileMetadataByIdHeaders headers) { 187 Map<String, String> queryParamsMap = 188 prepareParams(mapOf(entryOf("view", convertToString(queryParams.getView())))); 189 Map<String, String> headersMap = prepareParams(mergeMaps(mapOf(), headers.getExtraHeaders())); 190 FetchResponse response = 191 this.networkSession 192 .getNetworkClient() 193 .fetch( 194 new FetchOptions.Builder( 195 String.join( 196 "", 197 this.networkSession.getBaseUrls().getBaseUrl(), 198 "/2.0/files/", 199 convertToString(fileId), 200 "/metadata/", 201 convertToString(scope), 202 "/", 203 convertToString(templateKey)), 204 "GET") 205 .params(queryParamsMap) 206 .headers(headersMap) 207 .responseFormat(ResponseFormat.JSON) 208 .auth(this.auth) 209 .networkSession(this.networkSession) 210 .build()); 211 return JsonManager.deserialize(response.getData(), MetadataFull.class); 212 } 213 214 /** 215 * Applies an instance of a metadata template to a file. 216 * 217 * <p>In most cases only values that are present in the metadata template will be accepted, except 218 * for the `global.properties` template which accepts any key-value pair. 219 * 220 * @param fileId The unique identifier that represents a file. 221 * <p>The ID for any file can be determined by visiting a file in the web application and 222 * copying the ID from the URL. For example, for the URL `https://*.app.box.com/files/123` the 223 * `file_id` is `123`. Example: "12345" 224 * @param scope The scope of the metadata template. Example: "global" 225 * @param templateKey The name of the metadata template. Example: "properties" 226 * @param requestBody Request body of createFileMetadataById method 227 */ 228 public MetadataFull createFileMetadataById( 229 String fileId, 230 CreateFileMetadataByIdScope scope, 231 String templateKey, 232 Map<String, Object> requestBody) { 233 return createFileMetadataById( 234 fileId, scope, templateKey, requestBody, new CreateFileMetadataByIdHeaders()); 235 } 236 237 /** 238 * Applies an instance of a metadata template to a file. 239 * 240 * <p>In most cases only values that are present in the metadata template will be accepted, except 241 * for the `global.properties` template which accepts any key-value pair. 242 * 243 * @param fileId The unique identifier that represents a file. 244 * <p>The ID for any file can be determined by visiting a file in the web application and 245 * copying the ID from the URL. For example, for the URL `https://*.app.box.com/files/123` the 246 * `file_id` is `123`. Example: "12345" 247 * @param scope The scope of the metadata template. Example: "global" 248 * @param templateKey The name of the metadata template. Example: "properties" 249 * @param requestBody Request body of createFileMetadataById method 250 * @param headers Headers of createFileMetadataById method 251 */ 252 public MetadataFull createFileMetadataById( 253 String fileId, 254 CreateFileMetadataByIdScope scope, 255 String templateKey, 256 Map<String, Object> requestBody, 257 CreateFileMetadataByIdHeaders headers) { 258 Map<String, String> headersMap = prepareParams(mergeMaps(mapOf(), headers.getExtraHeaders())); 259 FetchResponse response = 260 this.networkSession 261 .getNetworkClient() 262 .fetch( 263 new FetchOptions.Builder( 264 String.join( 265 "", 266 this.networkSession.getBaseUrls().getBaseUrl(), 267 "/2.0/files/", 268 convertToString(fileId), 269 "/metadata/", 270 convertToString(scope), 271 "/", 272 convertToString(templateKey)), 273 "POST") 274 .headers(headersMap) 275 .data(JsonManager.serialize(requestBody)) 276 .contentType("application/json") 277 .responseFormat(ResponseFormat.JSON) 278 .auth(this.auth) 279 .networkSession(this.networkSession) 280 .build()); 281 return JsonManager.deserialize(response.getData(), MetadataFull.class); 282 } 283 284 /** 285 * Updates a piece of metadata on a file. 286 * 287 * <p>The metadata instance can only be updated if the template has already been applied to the 288 * file before. When editing metadata, only values that match the metadata template schema will be 289 * accepted. 290 * 291 * <p>The update is applied atomically. If any errors occur during the application of the 292 * operations, the metadata instance will not be changed. 293 * 294 * @param fileId The unique identifier that represents a file. 295 * <p>The ID for any file can be determined by visiting a file in the web application and 296 * copying the ID from the URL. For example, for the URL `https://*.app.box.com/files/123` the 297 * `file_id` is `123`. Example: "12345" 298 * @param scope The scope of the metadata template. Example: "global" 299 * @param templateKey The name of the metadata template. Example: "properties" 300 * @param requestBody Request body of updateFileMetadataById method 301 */ 302 public MetadataFull updateFileMetadataById( 303 String fileId, 304 UpdateFileMetadataByIdScope scope, 305 String templateKey, 306 List<UpdateFileMetadataByIdRequestBody> requestBody) { 307 return updateFileMetadataById( 308 fileId, scope, templateKey, requestBody, new UpdateFileMetadataByIdHeaders()); 309 } 310 311 /** 312 * Updates a piece of metadata on a file. 313 * 314 * <p>The metadata instance can only be updated if the template has already been applied to the 315 * file before. When editing metadata, only values that match the metadata template schema will be 316 * accepted. 317 * 318 * <p>The update is applied atomically. If any errors occur during the application of the 319 * operations, the metadata instance will not be changed. 320 * 321 * @param fileId The unique identifier that represents a file. 322 * <p>The ID for any file can be determined by visiting a file in the web application and 323 * copying the ID from the URL. For example, for the URL `https://*.app.box.com/files/123` the 324 * `file_id` is `123`. Example: "12345" 325 * @param scope The scope of the metadata template. Example: "global" 326 * @param templateKey The name of the metadata template. Example: "properties" 327 * @param requestBody Request body of updateFileMetadataById method 328 * @param headers Headers of updateFileMetadataById method 329 */ 330 public MetadataFull updateFileMetadataById( 331 String fileId, 332 UpdateFileMetadataByIdScope scope, 333 String templateKey, 334 List<UpdateFileMetadataByIdRequestBody> requestBody, 335 UpdateFileMetadataByIdHeaders headers) { 336 Map<String, String> headersMap = prepareParams(mergeMaps(mapOf(), headers.getExtraHeaders())); 337 FetchResponse response = 338 this.networkSession 339 .getNetworkClient() 340 .fetch( 341 new FetchOptions.Builder( 342 String.join( 343 "", 344 this.networkSession.getBaseUrls().getBaseUrl(), 345 "/2.0/files/", 346 convertToString(fileId), 347 "/metadata/", 348 convertToString(scope), 349 "/", 350 convertToString(templateKey)), 351 "PUT") 352 .headers(headersMap) 353 .data(JsonManager.serialize(requestBody)) 354 .contentType("application/json-patch+json") 355 .responseFormat(ResponseFormat.JSON) 356 .auth(this.auth) 357 .networkSession(this.networkSession) 358 .build()); 359 return JsonManager.deserialize(response.getData(), MetadataFull.class); 360 } 361 362 /** 363 * Deletes a piece of file metadata. 364 * 365 * @param fileId The unique identifier that represents a file. 366 * <p>The ID for any file can be determined by visiting a file in the web application and 367 * copying the ID from the URL. For example, for the URL `https://*.app.box.com/files/123` the 368 * `file_id` is `123`. Example: "12345" 369 * @param scope The scope of the metadata template. Example: "global" 370 * @param templateKey The name of the metadata template. Example: "properties" 371 */ 372 public void deleteFileMetadataById( 373 String fileId, DeleteFileMetadataByIdScope scope, String templateKey) { 374 deleteFileMetadataById(fileId, scope, templateKey, new DeleteFileMetadataByIdHeaders()); 375 } 376 377 /** 378 * Deletes a piece of file metadata. 379 * 380 * @param fileId The unique identifier that represents a file. 381 * <p>The ID for any file can be determined by visiting a file in the web application and 382 * copying the ID from the URL. For example, for the URL `https://*.app.box.com/files/123` the 383 * `file_id` is `123`. Example: "12345" 384 * @param scope The scope of the metadata template. Example: "global" 385 * @param templateKey The name of the metadata template. Example: "properties" 386 * @param headers Headers of deleteFileMetadataById method 387 */ 388 public void deleteFileMetadataById( 389 String fileId, 390 DeleteFileMetadataByIdScope scope, 391 String templateKey, 392 DeleteFileMetadataByIdHeaders headers) { 393 Map<String, String> headersMap = prepareParams(mergeMaps(mapOf(), headers.getExtraHeaders())); 394 FetchResponse response = 395 this.networkSession 396 .getNetworkClient() 397 .fetch( 398 new FetchOptions.Builder( 399 String.join( 400 "", 401 this.networkSession.getBaseUrls().getBaseUrl(), 402 "/2.0/files/", 403 convertToString(fileId), 404 "/metadata/", 405 convertToString(scope), 406 "/", 407 convertToString(templateKey)), 408 "DELETE") 409 .headers(headersMap) 410 .responseFormat(ResponseFormat.NO_CONTENT) 411 .auth(this.auth) 412 .networkSession(this.networkSession) 413 .build()); 414 } 415 416 public Authentication getAuth() { 417 return auth; 418 } 419 420 public NetworkSession getNetworkSession() { 421 return networkSession; 422 } 423 424 public static class Builder { 425 426 protected Authentication auth; 427 428 protected NetworkSession networkSession; 429 430 public Builder() {} 431 432 public Builder auth(Authentication auth) { 433 this.auth = auth; 434 return this; 435 } 436 437 public Builder networkSession(NetworkSession networkSession) { 438 this.networkSession = networkSession; 439 return this; 440 } 441 442 public FileMetadataManager build() { 443 if (this.networkSession == null) { 444 this.networkSession = new NetworkSession(); 445 } 446 return new FileMetadataManager(this); 447 } 448 } 449}