001package com.box.sdkgen.managers.zipdownloads; 002 003import static com.box.sdkgen.internal.utils.UtilsManager.mapOf; 004import static com.box.sdkgen.internal.utils.UtilsManager.mergeMaps; 005import static com.box.sdkgen.internal.utils.UtilsManager.prepareParams; 006 007import com.box.sdkgen.networking.auth.Authentication; 008import com.box.sdkgen.networking.fetchoptions.FetchOptions; 009import com.box.sdkgen.networking.fetchoptions.ResponseFormat; 010import com.box.sdkgen.networking.fetchresponse.FetchResponse; 011import com.box.sdkgen.networking.network.NetworkSession; 012import com.box.sdkgen.schemas.zipdownload.ZipDownload; 013import com.box.sdkgen.schemas.zipdownloadrequest.ZipDownloadRequest; 014import com.box.sdkgen.schemas.zipdownloadstatus.ZipDownloadStatus; 015import com.box.sdkgen.serialization.json.JsonManager; 016import java.io.InputStream; 017import java.util.Map; 018 019public class ZipDownloadsManager { 020 021 public Authentication auth; 022 023 public NetworkSession networkSession; 024 025 public ZipDownloadsManager() { 026 this.networkSession = new NetworkSession(); 027 } 028 029 protected ZipDownloadsManager(Builder builder) { 030 this.auth = builder.auth; 031 this.networkSession = builder.networkSession; 032 } 033 034 /** 035 * Creates a request to download multiple files and folders as a single `zip` archive file. This 036 * API does not return the archive but instead performs all the checks to ensure that the user has 037 * access to all the items, and then returns a `download_url` and a `status_url` that can be used 038 * to download the archive. 039 * 040 * <p>The limit for an archive is either the Account's upload limit or 10,000 files, whichever is 041 * met first. 042 * 043 * <p>**Note**: Downloading a large file can be affected by various factors such as distance, 044 * network latency, bandwidth, and congestion, as well as packet loss ratio and current server 045 * load. For these reasons we recommend that a maximum ZIP archive total size does not exceed 046 * 25GB. 047 * 048 * @param requestBody Request body of createZipDownload method 049 */ 050 public ZipDownload createZipDownload(ZipDownloadRequest requestBody) { 051 return createZipDownload(requestBody, new CreateZipDownloadHeaders()); 052 } 053 054 /** 055 * Creates a request to download multiple files and folders as a single `zip` archive file. This 056 * API does not return the archive but instead performs all the checks to ensure that the user has 057 * access to all the items, and then returns a `download_url` and a `status_url` that can be used 058 * to download the archive. 059 * 060 * <p>The limit for an archive is either the Account's upload limit or 10,000 files, whichever is 061 * met first. 062 * 063 * <p>**Note**: Downloading a large file can be affected by various factors such as distance, 064 * network latency, bandwidth, and congestion, as well as packet loss ratio and current server 065 * load. For these reasons we recommend that a maximum ZIP archive total size does not exceed 066 * 25GB. 067 * 068 * @param requestBody Request body of createZipDownload method 069 * @param headers Headers of createZipDownload method 070 */ 071 public ZipDownload createZipDownload( 072 ZipDownloadRequest requestBody, CreateZipDownloadHeaders headers) { 073 Map<String, String> headersMap = prepareParams(mergeMaps(mapOf(), headers.getExtraHeaders())); 074 FetchResponse response = 075 this.networkSession 076 .getNetworkClient() 077 .fetch( 078 new FetchOptions.Builder( 079 String.join( 080 "", 081 this.networkSession.getBaseUrls().getBaseUrl(), 082 "/2.0/zip_downloads"), 083 "POST") 084 .headers(headersMap) 085 .data(JsonManager.serialize(requestBody)) 086 .contentType("application/json") 087 .responseFormat(ResponseFormat.JSON) 088 .auth(this.auth) 089 .networkSession(this.networkSession) 090 .build()); 091 return JsonManager.deserialize(response.getData(), ZipDownload.class); 092 } 093 094 /** 095 * Returns the contents of a `zip` archive in binary format. This URL does not require any form of 096 * authentication and could be used in a user's browser to download the archive to a user's 097 * device. 098 * 099 * <p>By default, this URL is only valid for a few seconds from the creation of the request for 100 * this archive. Once a download has started it can not be stopped and resumed, instead a new 101 * request for a zip archive would need to be created. 102 * 103 * <p>The URL of this endpoint should not be considered as fixed. Instead, use the [Create zip 104 * download](https://developer.box.com/reference/post-zip-downloads) API to request to create a 105 * `zip` archive, and then follow the `download_url` field in the response to this endpoint. 106 * 107 * @param downloadUrl The URL that can be used to download created `zip` archive. Example: 108 * `https://dl.boxcloud.com/2.0/zip_downloads/29l00nfxDyHOt7RphI9zT_w==nDnZEDjY2S8iEWWCHEEiptFxwoWojjlibZjJ6geuE5xnXENDTPxzgbks_yY=/content` 109 */ 110 public InputStream getZipDownloadContent(String downloadUrl) { 111 return getZipDownloadContent(downloadUrl, new GetZipDownloadContentHeaders()); 112 } 113 114 /** 115 * Returns the contents of a `zip` archive in binary format. This URL does not require any form of 116 * authentication and could be used in a user's browser to download the archive to a user's 117 * device. 118 * 119 * <p>By default, this URL is only valid for a few seconds from the creation of the request for 120 * this archive. Once a download has started it can not be stopped and resumed, instead a new 121 * request for a zip archive would need to be created. 122 * 123 * <p>The URL of this endpoint should not be considered as fixed. Instead, use the [Create zip 124 * download](https://developer.box.com/reference/post-zip-downloads) API to request to create a 125 * `zip` archive, and then follow the `download_url` field in the response to this endpoint. 126 * 127 * @param downloadUrl The URL that can be used to download created `zip` archive. Example: 128 * `https://dl.boxcloud.com/2.0/zip_downloads/29l00nfxDyHOt7RphI9zT_w==nDnZEDjY2S8iEWWCHEEiptFxwoWojjlibZjJ6geuE5xnXENDTPxzgbks_yY=/content` 129 * @param headers Headers of getZipDownloadContent method 130 */ 131 public InputStream getZipDownloadContent( 132 String downloadUrl, GetZipDownloadContentHeaders headers) { 133 Map<String, String> headersMap = prepareParams(mergeMaps(mapOf(), headers.getExtraHeaders())); 134 FetchResponse response = 135 this.networkSession 136 .getNetworkClient() 137 .fetch( 138 new FetchOptions.Builder(downloadUrl, "GET") 139 .headers(headersMap) 140 .responseFormat(ResponseFormat.BINARY) 141 .auth(this.auth) 142 .networkSession(this.networkSession) 143 .build()); 144 return response.getContent(); 145 } 146 147 /** 148 * Returns the download status of a `zip` archive, allowing an application to inspect the progress 149 * of the download as well as the number of items that might have been skipped. 150 * 151 * <p>This endpoint can only be accessed once the download has started. Subsequently this endpoint 152 * is valid for 12 hours from the start of the download. 153 * 154 * <p>The URL of this endpoint should not be considered as fixed. Instead, use the [Create zip 155 * download](https://developer.box.com/reference/post-zip-downloads) API to request to create a 156 * `zip` archive, and then follow the `status_url` field in the response to this endpoint. 157 * 158 * @param statusUrl The URL that can be used to get the status of the `zip` archive being 159 * downloaded. Example: 160 * `https://dl.boxcloud.com/2.0/zip_downloads/29l00nfxDyHOt7RphI9zT_w==nDnZEDjY2S8iEWWCHEEiptFxwoWojjlibZjJ6geuE5xnXENDTPxzgbks_yY=/status` 161 */ 162 public ZipDownloadStatus getZipDownloadStatus(String statusUrl) { 163 return getZipDownloadStatus(statusUrl, new GetZipDownloadStatusHeaders()); 164 } 165 166 /** 167 * Returns the download status of a `zip` archive, allowing an application to inspect the progress 168 * of the download as well as the number of items that might have been skipped. 169 * 170 * <p>This endpoint can only be accessed once the download has started. Subsequently this endpoint 171 * is valid for 12 hours from the start of the download. 172 * 173 * <p>The URL of this endpoint should not be considered as fixed. Instead, use the [Create zip 174 * download](https://developer.box.com/reference/post-zip-downloads) API to request to create a 175 * `zip` archive, and then follow the `status_url` field in the response to this endpoint. 176 * 177 * @param statusUrl The URL that can be used to get the status of the `zip` archive being 178 * downloaded. Example: 179 * `https://dl.boxcloud.com/2.0/zip_downloads/29l00nfxDyHOt7RphI9zT_w==nDnZEDjY2S8iEWWCHEEiptFxwoWojjlibZjJ6geuE5xnXENDTPxzgbks_yY=/status` 180 * @param headers Headers of getZipDownloadStatus method 181 */ 182 public ZipDownloadStatus getZipDownloadStatus( 183 String statusUrl, GetZipDownloadStatusHeaders headers) { 184 Map<String, String> headersMap = prepareParams(mergeMaps(mapOf(), headers.getExtraHeaders())); 185 FetchResponse response = 186 this.networkSession 187 .getNetworkClient() 188 .fetch( 189 new FetchOptions.Builder(statusUrl, "GET") 190 .headers(headersMap) 191 .responseFormat(ResponseFormat.JSON) 192 .auth(this.auth) 193 .networkSession(this.networkSession) 194 .build()); 195 return JsonManager.deserialize(response.getData(), ZipDownloadStatus.class); 196 } 197 198 /** 199 * Creates a zip and downloads its content 200 * 201 * @param requestBody Zip download request body 202 */ 203 public InputStream downloadZip(ZipDownloadRequest requestBody) { 204 return downloadZip(requestBody, new DownloadZipHeaders()); 205 } 206 207 /** 208 * Creates a zip and downloads its content 209 * 210 * @param requestBody Zip download request body 211 * @param headers Headers of zip download method 212 */ 213 public InputStream downloadZip(ZipDownloadRequest requestBody, DownloadZipHeaders headers) { 214 ZipDownload zipDownloadSession = 215 this.createZipDownload( 216 new ZipDownloadRequest.Builder(requestBody.getItems()) 217 .downloadFileName(requestBody.getDownloadFileName()) 218 .build(), 219 new CreateZipDownloadHeaders.Builder().extraHeaders(headers.getExtraHeaders()).build()); 220 return this.getZipDownloadContent( 221 zipDownloadSession.getDownloadUrl(), 222 new GetZipDownloadContentHeaders.Builder().extraHeaders(headers.getExtraHeaders()).build()); 223 } 224 225 public Authentication getAuth() { 226 return auth; 227 } 228 229 public NetworkSession getNetworkSession() { 230 return networkSession; 231 } 232 233 public static class Builder { 234 235 protected Authentication auth; 236 237 protected NetworkSession networkSession; 238 239 public Builder() {} 240 241 public Builder auth(Authentication auth) { 242 this.auth = auth; 243 return this; 244 } 245 246 public Builder networkSession(NetworkSession networkSession) { 247 this.networkSession = networkSession; 248 return this; 249 } 250 251 public ZipDownloadsManager build() { 252 if (this.networkSession == null) { 253 this.networkSession = new NetworkSession(); 254 } 255 return new ZipDownloadsManager(this); 256 } 257 } 258}