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, field `name` is redacted when: - a collaboration 220 * was created using `user_id`, - a collaboration was created using `login`. 221 * 222 * @param requestBody Request body of createCollaboration method 223 */ 224 public Collaboration createCollaboration(CreateCollaborationRequestBody requestBody) { 225 return createCollaboration( 226 requestBody, new CreateCollaborationQueryParams(), new CreateCollaborationHeaders()); 227 } 228 229 /** 230 * Adds a collaboration for a single user or a single group to a file or folder. 231 * 232 * <p>Collaborations can be created using email address, user IDs, or a group IDs. 233 * 234 * <p>If a collaboration is being created with a group, access to this endpoint is dependent on 235 * the group's ability to be invited. 236 * 237 * <p>If collaboration is in `pending` status, field `name` is redacted when: - a collaboration 238 * was created using `user_id`, - a collaboration was created using `login`. 239 * 240 * @param requestBody Request body of createCollaboration method 241 * @param queryParams Query parameters of createCollaboration method 242 */ 243 public Collaboration createCollaboration( 244 CreateCollaborationRequestBody requestBody, CreateCollaborationQueryParams queryParams) { 245 return createCollaboration(requestBody, queryParams, new CreateCollaborationHeaders()); 246 } 247 248 /** 249 * Adds a collaboration for a single user or a single group to a file or folder. 250 * 251 * <p>Collaborations can be created using email address, user IDs, or a group IDs. 252 * 253 * <p>If a collaboration is being created with a group, access to this endpoint is dependent on 254 * the group's ability to be invited. 255 * 256 * <p>If collaboration is in `pending` status, field `name` is redacted when: - a collaboration 257 * was created using `user_id`, - a collaboration was created using `login`. 258 * 259 * @param requestBody Request body of createCollaboration method 260 * @param headers Headers of createCollaboration method 261 */ 262 public Collaboration createCollaboration( 263 CreateCollaborationRequestBody requestBody, CreateCollaborationHeaders headers) { 264 return createCollaboration(requestBody, new CreateCollaborationQueryParams(), headers); 265 } 266 267 /** 268 * Adds a collaboration for a single user or a single group to a file or folder. 269 * 270 * <p>Collaborations can be created using email address, user IDs, or a group IDs. 271 * 272 * <p>If a collaboration is being created with a group, access to this endpoint is dependent on 273 * the group's ability to be invited. 274 * 275 * <p>If collaboration is in `pending` status, field `name` is redacted when: - a collaboration 276 * was created using `user_id`, - a collaboration was created using `login`. 277 * 278 * @param requestBody Request body of createCollaboration method 279 * @param queryParams Query parameters of createCollaboration method 280 * @param headers Headers of createCollaboration method 281 */ 282 public Collaboration createCollaboration( 283 CreateCollaborationRequestBody requestBody, 284 CreateCollaborationQueryParams queryParams, 285 CreateCollaborationHeaders headers) { 286 Map<String, String> queryParamsMap = 287 prepareParams( 288 mapOf( 289 entryOf("fields", convertToString(queryParams.getFields())), 290 entryOf("notify", convertToString(queryParams.getNotify())))); 291 Map<String, String> headersMap = prepareParams(mergeMaps(mapOf(), headers.getExtraHeaders())); 292 FetchResponse response = 293 this.networkSession 294 .getNetworkClient() 295 .fetch( 296 new FetchOptions.Builder( 297 String.join( 298 "", 299 this.networkSession.getBaseUrls().getBaseUrl(), 300 "/2.0/collaborations"), 301 "POST") 302 .params(queryParamsMap) 303 .headers(headersMap) 304 .data(JsonManager.serialize(requestBody)) 305 .contentType("application/json") 306 .responseFormat(ResponseFormat.JSON) 307 .auth(this.auth) 308 .networkSession(this.networkSession) 309 .build()); 310 return JsonManager.deserialize(response.getData(), Collaboration.class); 311 } 312 313 public Authentication getAuth() { 314 return auth; 315 } 316 317 public NetworkSession getNetworkSession() { 318 return networkSession; 319 } 320 321 public static class Builder { 322 323 protected Authentication auth; 324 325 protected NetworkSession networkSession; 326 327 public Builder() {} 328 329 public Builder auth(Authentication auth) { 330 this.auth = auth; 331 return this; 332 } 333 334 public Builder networkSession(NetworkSession networkSession) { 335 this.networkSession = networkSession; 336 return this; 337 } 338 339 public UserCollaborationsManager build() { 340 if (this.networkSession == null) { 341 this.networkSession = new NetworkSession(); 342 } 343 return new UserCollaborationsManager(this); 344 } 345 } 346}