001package com.box.sdkgen.managers.comments; 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.commentfull.CommentFull; 015import com.box.sdkgen.schemas.comments.Comments; 016import com.box.sdkgen.serialization.json.JsonManager; 017import java.util.Map; 018 019public class CommentsManager { 020 021 public Authentication auth; 022 023 public NetworkSession networkSession; 024 025 public CommentsManager() { 026 this.networkSession = new NetworkSession(); 027 } 028 029 protected CommentsManager(Builder builder) { 030 this.auth = builder.auth; 031 this.networkSession = builder.networkSession; 032 } 033 034 /** 035 * Retrieves a list of comments for a file. 036 * 037 * @param fileId The unique identifier that represents a file. 038 * <p>The ID for any file can be determined by visiting a file in the web application and 039 * copying the ID from the URL. For example, for the URL `https://*.app.box.com/files/123` the 040 * `file_id` is `123`. Example: "12345" 041 */ 042 public Comments getFileComments(String fileId) { 043 return getFileComments(fileId, new GetFileCommentsQueryParams(), new GetFileCommentsHeaders()); 044 } 045 046 /** 047 * Retrieves a list of comments for a file. 048 * 049 * @param fileId The unique identifier that represents a file. 050 * <p>The ID for any file can be determined by visiting a file in the web application and 051 * copying the ID from the URL. For example, for the URL `https://*.app.box.com/files/123` the 052 * `file_id` is `123`. Example: "12345" 053 * @param queryParams Query parameters of getFileComments method 054 */ 055 public Comments getFileComments(String fileId, GetFileCommentsQueryParams queryParams) { 056 return getFileComments(fileId, queryParams, new GetFileCommentsHeaders()); 057 } 058 059 /** 060 * Retrieves a list of comments for a file. 061 * 062 * @param fileId The unique identifier that represents a file. 063 * <p>The ID for any file can be determined by visiting a file in the web application and 064 * copying the ID from the URL. For example, for the URL `https://*.app.box.com/files/123` the 065 * `file_id` is `123`. Example: "12345" 066 * @param headers Headers of getFileComments method 067 */ 068 public Comments getFileComments(String fileId, GetFileCommentsHeaders headers) { 069 return getFileComments(fileId, new GetFileCommentsQueryParams(), headers); 070 } 071 072 /** 073 * Retrieves a list of comments for a file. 074 * 075 * @param fileId The unique identifier that represents a file. 076 * <p>The ID for any file can be determined by visiting a file in the web application and 077 * copying the ID from the URL. For example, for the URL `https://*.app.box.com/files/123` the 078 * `file_id` is `123`. Example: "12345" 079 * @param queryParams Query parameters of getFileComments method 080 * @param headers Headers of getFileComments method 081 */ 082 public Comments getFileComments( 083 String fileId, GetFileCommentsQueryParams queryParams, GetFileCommentsHeaders headers) { 084 Map<String, String> queryParamsMap = 085 prepareParams( 086 mapOf( 087 entryOf("fields", convertToString(queryParams.getFields())), 088 entryOf("limit", convertToString(queryParams.getLimit())), 089 entryOf("offset", convertToString(queryParams.getOffset())))); 090 Map<String, String> headersMap = prepareParams(mergeMaps(mapOf(), headers.getExtraHeaders())); 091 FetchResponse response = 092 this.networkSession 093 .getNetworkClient() 094 .fetch( 095 new FetchOptions.Builder( 096 String.join( 097 "", 098 this.networkSession.getBaseUrls().getBaseUrl(), 099 "/2.0/files/", 100 convertToString(fileId), 101 "/comments"), 102 "GET") 103 .params(queryParamsMap) 104 .headers(headersMap) 105 .responseFormat(ResponseFormat.JSON) 106 .auth(this.auth) 107 .networkSession(this.networkSession) 108 .build()); 109 return JsonManager.deserialize(response.getData(), Comments.class); 110 } 111 112 /** 113 * Retrieves the message and metadata for a specific comment, as well as information on the user 114 * who created the comment. 115 * 116 * @param commentId The ID of the comment. Example: "12345" 117 */ 118 public CommentFull getCommentById(String commentId) { 119 return getCommentById(commentId, new GetCommentByIdQueryParams(), new GetCommentByIdHeaders()); 120 } 121 122 /** 123 * Retrieves the message and metadata for a specific comment, as well as information on the user 124 * who created the comment. 125 * 126 * @param commentId The ID of the comment. Example: "12345" 127 * @param queryParams Query parameters of getCommentById method 128 */ 129 public CommentFull getCommentById(String commentId, GetCommentByIdQueryParams queryParams) { 130 return getCommentById(commentId, queryParams, new GetCommentByIdHeaders()); 131 } 132 133 /** 134 * Retrieves the message and metadata for a specific comment, as well as information on the user 135 * who created the comment. 136 * 137 * @param commentId The ID of the comment. Example: "12345" 138 * @param headers Headers of getCommentById method 139 */ 140 public CommentFull getCommentById(String commentId, GetCommentByIdHeaders headers) { 141 return getCommentById(commentId, new GetCommentByIdQueryParams(), headers); 142 } 143 144 /** 145 * Retrieves the message and metadata for a specific comment, as well as information on the user 146 * who created the comment. 147 * 148 * @param commentId The ID of the comment. Example: "12345" 149 * @param queryParams Query parameters of getCommentById method 150 * @param headers Headers of getCommentById method 151 */ 152 public CommentFull getCommentById( 153 String commentId, GetCommentByIdQueryParams queryParams, GetCommentByIdHeaders headers) { 154 Map<String, String> queryParamsMap = 155 prepareParams(mapOf(entryOf("fields", convertToString(queryParams.getFields())))); 156 Map<String, String> headersMap = prepareParams(mergeMaps(mapOf(), headers.getExtraHeaders())); 157 FetchResponse response = 158 this.networkSession 159 .getNetworkClient() 160 .fetch( 161 new FetchOptions.Builder( 162 String.join( 163 "", 164 this.networkSession.getBaseUrls().getBaseUrl(), 165 "/2.0/comments/", 166 convertToString(commentId)), 167 "GET") 168 .params(queryParamsMap) 169 .headers(headersMap) 170 .responseFormat(ResponseFormat.JSON) 171 .auth(this.auth) 172 .networkSession(this.networkSession) 173 .build()); 174 return JsonManager.deserialize(response.getData(), CommentFull.class); 175 } 176 177 /** 178 * Update the message of a comment. 179 * 180 * @param commentId The ID of the comment. Example: "12345" 181 */ 182 public CommentFull updateCommentById(String commentId) { 183 return updateCommentById( 184 commentId, 185 new UpdateCommentByIdRequestBody(), 186 new UpdateCommentByIdQueryParams(), 187 new UpdateCommentByIdHeaders()); 188 } 189 190 /** 191 * Update the message of a comment. 192 * 193 * @param commentId The ID of the comment. Example: "12345" 194 * @param requestBody Request body of updateCommentById method 195 */ 196 public CommentFull updateCommentById(String commentId, UpdateCommentByIdRequestBody requestBody) { 197 return updateCommentById( 198 commentId, requestBody, new UpdateCommentByIdQueryParams(), new UpdateCommentByIdHeaders()); 199 } 200 201 /** 202 * Update the message of a comment. 203 * 204 * @param commentId The ID of the comment. Example: "12345" 205 * @param queryParams Query parameters of updateCommentById method 206 */ 207 public CommentFull updateCommentById(String commentId, UpdateCommentByIdQueryParams queryParams) { 208 return updateCommentById( 209 commentId, new UpdateCommentByIdRequestBody(), queryParams, new UpdateCommentByIdHeaders()); 210 } 211 212 /** 213 * Update the message of a comment. 214 * 215 * @param commentId The ID of the comment. Example: "12345" 216 * @param requestBody Request body of updateCommentById method 217 * @param queryParams Query parameters of updateCommentById method 218 */ 219 public CommentFull updateCommentById( 220 String commentId, 221 UpdateCommentByIdRequestBody requestBody, 222 UpdateCommentByIdQueryParams queryParams) { 223 return updateCommentById(commentId, requestBody, queryParams, new UpdateCommentByIdHeaders()); 224 } 225 226 /** 227 * Update the message of a comment. 228 * 229 * @param commentId The ID of the comment. Example: "12345" 230 * @param headers Headers of updateCommentById method 231 */ 232 public CommentFull updateCommentById(String commentId, UpdateCommentByIdHeaders headers) { 233 return updateCommentById( 234 commentId, new UpdateCommentByIdRequestBody(), new UpdateCommentByIdQueryParams(), headers); 235 } 236 237 /** 238 * Update the message of a comment. 239 * 240 * @param commentId The ID of the comment. Example: "12345" 241 * @param requestBody Request body of updateCommentById method 242 * @param headers Headers of updateCommentById method 243 */ 244 public CommentFull updateCommentById( 245 String commentId, 246 UpdateCommentByIdRequestBody requestBody, 247 UpdateCommentByIdHeaders headers) { 248 return updateCommentById(commentId, requestBody, new UpdateCommentByIdQueryParams(), headers); 249 } 250 251 /** 252 * Update the message of a comment. 253 * 254 * @param commentId The ID of the comment. Example: "12345" 255 * @param queryParams Query parameters of updateCommentById method 256 * @param headers Headers of updateCommentById method 257 */ 258 public CommentFull updateCommentById( 259 String commentId, 260 UpdateCommentByIdQueryParams queryParams, 261 UpdateCommentByIdHeaders headers) { 262 return updateCommentById(commentId, new UpdateCommentByIdRequestBody(), queryParams, headers); 263 } 264 265 /** 266 * Update the message of a comment. 267 * 268 * @param commentId The ID of the comment. Example: "12345" 269 * @param requestBody Request body of updateCommentById method 270 * @param queryParams Query parameters of updateCommentById method 271 * @param headers Headers of updateCommentById method 272 */ 273 public CommentFull updateCommentById( 274 String commentId, 275 UpdateCommentByIdRequestBody requestBody, 276 UpdateCommentByIdQueryParams queryParams, 277 UpdateCommentByIdHeaders headers) { 278 Map<String, String> queryParamsMap = 279 prepareParams(mapOf(entryOf("fields", convertToString(queryParams.getFields())))); 280 Map<String, String> headersMap = prepareParams(mergeMaps(mapOf(), headers.getExtraHeaders())); 281 FetchResponse response = 282 this.networkSession 283 .getNetworkClient() 284 .fetch( 285 new FetchOptions.Builder( 286 String.join( 287 "", 288 this.networkSession.getBaseUrls().getBaseUrl(), 289 "/2.0/comments/", 290 convertToString(commentId)), 291 "PUT") 292 .params(queryParamsMap) 293 .headers(headersMap) 294 .data(JsonManager.serialize(requestBody)) 295 .contentType("application/json") 296 .responseFormat(ResponseFormat.JSON) 297 .auth(this.auth) 298 .networkSession(this.networkSession) 299 .build()); 300 return JsonManager.deserialize(response.getData(), CommentFull.class); 301 } 302 303 /** 304 * Permanently deletes a comment. 305 * 306 * @param commentId The ID of the comment. Example: "12345" 307 */ 308 public void deleteCommentById(String commentId) { 309 deleteCommentById(commentId, new DeleteCommentByIdHeaders()); 310 } 311 312 /** 313 * Permanently deletes a comment. 314 * 315 * @param commentId The ID of the comment. Example: "12345" 316 * @param headers Headers of deleteCommentById method 317 */ 318 public void deleteCommentById(String commentId, DeleteCommentByIdHeaders headers) { 319 Map<String, String> headersMap = prepareParams(mergeMaps(mapOf(), headers.getExtraHeaders())); 320 FetchResponse response = 321 this.networkSession 322 .getNetworkClient() 323 .fetch( 324 new FetchOptions.Builder( 325 String.join( 326 "", 327 this.networkSession.getBaseUrls().getBaseUrl(), 328 "/2.0/comments/", 329 convertToString(commentId)), 330 "DELETE") 331 .headers(headersMap) 332 .responseFormat(ResponseFormat.NO_CONTENT) 333 .auth(this.auth) 334 .networkSession(this.networkSession) 335 .build()); 336 } 337 338 /** 339 * Adds a comment by the user to a specific file, or as a reply to an other comment. 340 * 341 * @param requestBody Request body of createComment method 342 */ 343 public CommentFull createComment(CreateCommentRequestBody requestBody) { 344 return createComment(requestBody, new CreateCommentQueryParams(), new CreateCommentHeaders()); 345 } 346 347 /** 348 * Adds a comment by the user to a specific file, or as a reply to an other comment. 349 * 350 * @param requestBody Request body of createComment method 351 * @param queryParams Query parameters of createComment method 352 */ 353 public CommentFull createComment( 354 CreateCommentRequestBody requestBody, CreateCommentQueryParams queryParams) { 355 return createComment(requestBody, queryParams, new CreateCommentHeaders()); 356 } 357 358 /** 359 * Adds a comment by the user to a specific file, or as a reply to an other comment. 360 * 361 * @param requestBody Request body of createComment method 362 * @param headers Headers of createComment method 363 */ 364 public CommentFull createComment( 365 CreateCommentRequestBody requestBody, CreateCommentHeaders headers) { 366 return createComment(requestBody, new CreateCommentQueryParams(), headers); 367 } 368 369 /** 370 * Adds a comment by the user to a specific file, or as a reply to an other comment. 371 * 372 * @param requestBody Request body of createComment method 373 * @param queryParams Query parameters of createComment method 374 * @param headers Headers of createComment method 375 */ 376 public CommentFull createComment( 377 CreateCommentRequestBody requestBody, 378 CreateCommentQueryParams queryParams, 379 CreateCommentHeaders headers) { 380 Map<String, String> queryParamsMap = 381 prepareParams(mapOf(entryOf("fields", convertToString(queryParams.getFields())))); 382 Map<String, String> headersMap = prepareParams(mergeMaps(mapOf(), headers.getExtraHeaders())); 383 FetchResponse response = 384 this.networkSession 385 .getNetworkClient() 386 .fetch( 387 new FetchOptions.Builder( 388 String.join( 389 "", this.networkSession.getBaseUrls().getBaseUrl(), "/2.0/comments"), 390 "POST") 391 .params(queryParamsMap) 392 .headers(headersMap) 393 .data(JsonManager.serialize(requestBody)) 394 .contentType("application/json") 395 .responseFormat(ResponseFormat.JSON) 396 .auth(this.auth) 397 .networkSession(this.networkSession) 398 .build()); 399 return JsonManager.deserialize(response.getData(), CommentFull.class); 400 } 401 402 public Authentication getAuth() { 403 return auth; 404 } 405 406 public NetworkSession getNetworkSession() { 407 return networkSession; 408 } 409 410 public static class Builder { 411 412 protected Authentication auth; 413 414 protected NetworkSession networkSession; 415 416 public Builder() {} 417 418 public Builder auth(Authentication auth) { 419 this.auth = auth; 420 return this; 421 } 422 423 public Builder networkSession(NetworkSession networkSession) { 424 this.networkSession = networkSession; 425 return this; 426 } 427 428 public CommentsManager build() { 429 if (this.networkSession == null) { 430 this.networkSession = new NetworkSession(); 431 } 432 return new CommentsManager(this); 433 } 434 } 435}