001package com.box.sdkgen.managers.usercollaborations; 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.collaboration.Collaboration; 015import com.box.sdkgen.serialization.json.JsonManager; 016import java.util.Map; 017 018public class UserCollaborationsManager { 019 020 public Authentication auth; 021 022 public NetworkSession networkSession; 023 024 public UserCollaborationsManager() { 025 this.networkSession = new NetworkSession(); 026 } 027 028 protected UserCollaborationsManager(Builder builder) { 029 this.auth = builder.auth; 030 this.networkSession = builder.networkSession; 031 } 032 033 /** 034 * Retrieves a single collaboration. 035 * 036 * @param collaborationId The ID of the collaboration. Example: "1234" 037 */ 038 public Collaboration getCollaborationById(String collaborationId) { 039 return getCollaborationById( 040 collaborationId, new GetCollaborationByIdQueryParams(), new GetCollaborationByIdHeaders()); 041 } 042 043 /** 044 * Retrieves a single collaboration. 045 * 046 * @param collaborationId The ID of the collaboration. Example: "1234" 047 * @param queryParams Query parameters of getCollaborationById method 048 */ 049 public Collaboration getCollaborationById( 050 String collaborationId, GetCollaborationByIdQueryParams queryParams) { 051 return getCollaborationById(collaborationId, queryParams, new GetCollaborationByIdHeaders()); 052 } 053 054 /** 055 * Retrieves a single collaboration. 056 * 057 * @param collaborationId The ID of the collaboration. Example: "1234" 058 * @param headers Headers of getCollaborationById method 059 */ 060 public Collaboration getCollaborationById( 061 String collaborationId, GetCollaborationByIdHeaders headers) { 062 return getCollaborationById(collaborationId, new GetCollaborationByIdQueryParams(), headers); 063 } 064 065 /** 066 * Retrieves a single collaboration. 067 * 068 * @param collaborationId The ID of the collaboration. Example: "1234" 069 * @param queryParams Query parameters of getCollaborationById method 070 * @param headers Headers of getCollaborationById method 071 */ 072 public Collaboration getCollaborationById( 073 String collaborationId, 074 GetCollaborationByIdQueryParams queryParams, 075 GetCollaborationByIdHeaders headers) { 076 Map<String, String> queryParamsMap = 077 prepareParams(mapOf(entryOf("fields", convertToString(queryParams.getFields())))); 078 Map<String, String> headersMap = prepareParams(mergeMaps(mapOf(), headers.getExtraHeaders())); 079 FetchResponse response = 080 this.networkSession 081 .getNetworkClient() 082 .fetch( 083 new FetchOptions.Builder( 084 String.join( 085 "", 086 this.networkSession.getBaseUrls().getBaseUrl(), 087 "/2.0/collaborations/", 088 convertToString(collaborationId)), 089 "GET") 090 .params(queryParamsMap) 091 .headers(headersMap) 092 .responseFormat(ResponseFormat.JSON) 093 .auth(this.auth) 094 .networkSession(this.networkSession) 095 .build()); 096 return JsonManager.deserialize(response.getData(), Collaboration.class); 097 } 098 099 /** 100 * Updates a collaboration. Can be used to change the owner of an item, or to accept collaboration 101 * invites. In case of accepting collaboration invite, role is not required. 102 * 103 * @param collaborationId The ID of the collaboration. Example: "1234" 104 */ 105 public Collaboration updateCollaborationById(String collaborationId) { 106 return updateCollaborationById( 107 collaborationId, 108 new UpdateCollaborationByIdRequestBody(), 109 new UpdateCollaborationByIdHeaders()); 110 } 111 112 /** 113 * Updates a collaboration. Can be used to change the owner of an item, or to accept collaboration 114 * invites. In case of accepting collaboration invite, role is not required. 115 * 116 * @param collaborationId The ID of the collaboration. Example: "1234" 117 * @param requestBody Request body of updateCollaborationById method 118 */ 119 public Collaboration updateCollaborationById( 120 String collaborationId, UpdateCollaborationByIdRequestBody requestBody) { 121 return updateCollaborationById( 122 collaborationId, requestBody, new UpdateCollaborationByIdHeaders()); 123 } 124 125 /** 126 * Updates a collaboration. Can be used to change the owner of an item, or to accept collaboration 127 * invites. In case of accepting collaboration invite, role is not required. 128 * 129 * @param collaborationId The ID of the collaboration. Example: "1234" 130 * @param headers Headers of updateCollaborationById method 131 */ 132 public Collaboration updateCollaborationById( 133 String collaborationId, UpdateCollaborationByIdHeaders headers) { 134 return updateCollaborationById( 135 collaborationId, new UpdateCollaborationByIdRequestBody(), headers); 136 } 137 138 /** 139 * Updates a collaboration. Can be used to change the owner of an item, or to accept collaboration 140 * invites. In case of accepting collaboration invite, role is not required. 141 * 142 * @param collaborationId The ID of the collaboration. Example: "1234" 143 * @param requestBody Request body of updateCollaborationById method 144 * @param headers Headers of updateCollaborationById method 145 */ 146 public Collaboration updateCollaborationById( 147 String collaborationId, 148 UpdateCollaborationByIdRequestBody requestBody, 149 UpdateCollaborationByIdHeaders headers) { 150 Map<String, String> headersMap = prepareParams(mergeMaps(mapOf(), headers.getExtraHeaders())); 151 FetchResponse response = 152 this.networkSession 153 .getNetworkClient() 154 .fetch( 155 new FetchOptions.Builder( 156 String.join( 157 "", 158 this.networkSession.getBaseUrls().getBaseUrl(), 159 "/2.0/collaborations/", 160 convertToString(collaborationId)), 161 "PUT") 162 .headers(headersMap) 163 .data(JsonManager.serialize(requestBody)) 164 .contentType("application/json") 165 .responseFormat(ResponseFormat.JSON) 166 .auth(this.auth) 167 .networkSession(this.networkSession) 168 .build()); 169 if (convertToString(response.getStatus()).equals("204")) { 170 return null; 171 } 172 return JsonManager.deserialize(response.getData(), Collaboration.class); 173 } 174 175 /** 176 * Deletes a single collaboration. 177 * 178 * @param collaborationId The ID of the collaboration. Example: "1234" 179 */ 180 public void deleteCollaborationById(String collaborationId) { 181 deleteCollaborationById(collaborationId, new DeleteCollaborationByIdHeaders()); 182 } 183 184 /** 185 * Deletes a single collaboration. 186 * 187 * @param collaborationId The ID of the collaboration. Example: "1234" 188 * @param headers Headers of deleteCollaborationById method 189 */ 190 public void deleteCollaborationById( 191 String collaborationId, DeleteCollaborationByIdHeaders headers) { 192 Map<String, String> headersMap = prepareParams(mergeMaps(mapOf(), headers.getExtraHeaders())); 193 FetchResponse response = 194 this.networkSession 195 .getNetworkClient() 196 .fetch( 197 new FetchOptions.Builder( 198 String.join( 199 "", 200 this.networkSession.getBaseUrls().getBaseUrl(), 201 "/2.0/collaborations/", 202 convertToString(collaborationId)), 203 "DELETE") 204 .headers(headersMap) 205 .responseFormat(ResponseFormat.NO_CONTENT) 206 .auth(this.auth) 207 .networkSession(this.networkSession) 208 .build()); 209 } 210 211 /** 212 * Adds a collaboration for a single user or a single group to a file or folder. 213 * 214 * <p>Collaborations can be created using email address, user IDs, or a group IDs. 215 * 216 * <p>If a collaboration is being created with a group, access to this endpoint is dependent on 217 * the group's ability to be invited. 218 * 219 * <p>If collaboration is in `pending` status, the following fields are redacted: - `login` and 220 * `name` are hidden if a collaboration was created using `user_id`, - `name` is hidden if a 221 * collaboration was created using `login`. 222 * 223 * @param requestBody Request body of createCollaboration method 224 */ 225 public Collaboration createCollaboration(CreateCollaborationRequestBody requestBody) { 226 return createCollaboration( 227 requestBody, new CreateCollaborationQueryParams(), new CreateCollaborationHeaders()); 228 } 229 230 /** 231 * Adds a collaboration for a single user or a single group to a file or folder. 232 * 233 * <p>Collaborations can be created using email address, user IDs, or a group IDs. 234 * 235 * <p>If a collaboration is being created with a group, access to this endpoint is dependent on 236 * the group's ability to be invited. 237 * 238 * <p>If collaboration is in `pending` status, the following fields are redacted: - `login` and 239 * `name` are hidden if a collaboration was created using `user_id`, - `name` is hidden if a 240 * collaboration was created using `login`. 241 * 242 * @param requestBody Request body of createCollaboration method 243 * @param queryParams Query parameters of createCollaboration method 244 */ 245 public Collaboration createCollaboration( 246 CreateCollaborationRequestBody requestBody, CreateCollaborationQueryParams queryParams) { 247 return createCollaboration(requestBody, queryParams, new CreateCollaborationHeaders()); 248 } 249 250 /** 251 * Adds a collaboration for a single user or a single group to a file or folder. 252 * 253 * <p>Collaborations can be created using email address, user IDs, or a group IDs. 254 * 255 * <p>If a collaboration is being created with a group, access to this endpoint is dependent on 256 * the group's ability to be invited. 257 * 258 * <p>If collaboration is in `pending` status, the following fields are redacted: - `login` and 259 * `name` are hidden if a collaboration was created using `user_id`, - `name` is hidden if a 260 * collaboration was created using `login`. 261 * 262 * @param requestBody Request body of createCollaboration method 263 * @param headers Headers of createCollaboration method 264 */ 265 public Collaboration createCollaboration( 266 CreateCollaborationRequestBody requestBody, CreateCollaborationHeaders headers) { 267 return createCollaboration(requestBody, new CreateCollaborationQueryParams(), headers); 268 } 269 270 /** 271 * Adds a collaboration for a single user or a single group to a file or folder. 272 * 273 * <p>Collaborations can be created using email address, user IDs, or a group IDs. 274 * 275 * <p>If a collaboration is being created with a group, access to this endpoint is dependent on 276 * the group's ability to be invited. 277 * 278 * <p>If collaboration is in `pending` status, the following fields are redacted: - `login` and 279 * `name` are hidden if a collaboration was created using `user_id`, - `name` is hidden if a 280 * collaboration was created using `login`. 281 * 282 * @param requestBody Request body of createCollaboration method 283 * @param queryParams Query parameters of createCollaboration method 284 * @param headers Headers of createCollaboration method 285 */ 286 public Collaboration createCollaboration( 287 CreateCollaborationRequestBody requestBody, 288 CreateCollaborationQueryParams queryParams, 289 CreateCollaborationHeaders headers) { 290 Map<String, String> queryParamsMap = 291 prepareParams( 292 mapOf( 293 entryOf("fields", convertToString(queryParams.getFields())), 294 entryOf("notify", convertToString(queryParams.getNotify())))); 295 Map<String, String> headersMap = prepareParams(mergeMaps(mapOf(), headers.getExtraHeaders())); 296 FetchResponse response = 297 this.networkSession 298 .getNetworkClient() 299 .fetch( 300 new FetchOptions.Builder( 301 String.join( 302 "", 303 this.networkSession.getBaseUrls().getBaseUrl(), 304 "/2.0/collaborations"), 305 "POST") 306 .params(queryParamsMap) 307 .headers(headersMap) 308 .data(JsonManager.serialize(requestBody)) 309 .contentType("application/json") 310 .responseFormat(ResponseFormat.JSON) 311 .auth(this.auth) 312 .networkSession(this.networkSession) 313 .build()); 314 return JsonManager.deserialize(response.getData(), Collaboration.class); 315 } 316 317 public Authentication getAuth() { 318 return auth; 319 } 320 321 public NetworkSession getNetworkSession() { 322 return networkSession; 323 } 324 325 public static class Builder { 326 327 protected Authentication auth; 328 329 protected NetworkSession networkSession; 330 331 public Builder() {} 332 333 public Builder auth(Authentication auth) { 334 this.auth = auth; 335 return this; 336 } 337 338 public Builder networkSession(NetworkSession networkSession) { 339 this.networkSession = networkSession; 340 return this; 341 } 342 343 public UserCollaborationsManager build() { 344 if (this.networkSession == null) { 345 this.networkSession = new NetworkSession(); 346 } 347 return new UserCollaborationsManager(this); 348 } 349 } 350}