001package com.box.sdkgen.managers.collections;
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.collection.Collection;
015import com.box.sdkgen.schemas.collections.Collections;
016import com.box.sdkgen.schemas.itemsoffsetpaginated.ItemsOffsetPaginated;
017import com.box.sdkgen.serialization.json.JsonManager;
018import java.util.Map;
019
020public class CollectionsManager {
021
022  public Authentication auth;
023
024  public NetworkSession networkSession;
025
026  public CollectionsManager() {
027    this.networkSession = new NetworkSession();
028  }
029
030  protected CollectionsManager(Builder builder) {
031    this.auth = builder.auth;
032    this.networkSession = builder.networkSession;
033  }
034
035  /**
036   * Retrieves all collections for a given user.
037   *
038   * <p>Currently, only the `favorites` collection is supported.
039   */
040  public Collections getCollections() {
041    return getCollections(new GetCollectionsQueryParams(), new GetCollectionsHeaders());
042  }
043
044  /**
045   * Retrieves all collections for a given user.
046   *
047   * <p>Currently, only the `favorites` collection is supported.
048   *
049   * @param queryParams Query parameters of getCollections method
050   */
051  public Collections getCollections(GetCollectionsQueryParams queryParams) {
052    return getCollections(queryParams, new GetCollectionsHeaders());
053  }
054
055  /**
056   * Retrieves all collections for a given user.
057   *
058   * <p>Currently, only the `favorites` collection is supported.
059   *
060   * @param headers Headers of getCollections method
061   */
062  public Collections getCollections(GetCollectionsHeaders headers) {
063    return getCollections(new GetCollectionsQueryParams(), headers);
064  }
065
066  /**
067   * Retrieves all collections for a given user.
068   *
069   * <p>Currently, only the `favorites` collection is supported.
070   *
071   * @param queryParams Query parameters of getCollections method
072   * @param headers Headers of getCollections method
073   */
074  public Collections getCollections(
075      GetCollectionsQueryParams queryParams, GetCollectionsHeaders headers) {
076    Map<String, String> queryParamsMap =
077        prepareParams(
078            mapOf(
079                entryOf("fields", convertToString(queryParams.getFields())),
080                entryOf("offset", convertToString(queryParams.getOffset())),
081                entryOf("limit", convertToString(queryParams.getLimit()))));
082    Map<String, String> headersMap = prepareParams(mergeMaps(mapOf(), headers.getExtraHeaders()));
083    FetchResponse response =
084        this.networkSession
085            .getNetworkClient()
086            .fetch(
087                new FetchOptions.Builder(
088                        String.join(
089                            "", this.networkSession.getBaseUrls().getBaseUrl(), "/2.0/collections"),
090                        "GET")
091                    .params(queryParamsMap)
092                    .headers(headersMap)
093                    .responseFormat(ResponseFormat.JSON)
094                    .auth(this.auth)
095                    .networkSession(this.networkSession)
096                    .build());
097    return JsonManager.deserialize(response.getData(), Collections.class);
098  }
099
100  /**
101   * Retrieves the files and/or folders contained within this collection.
102   *
103   * @param collectionId The ID of the collection. Example: "926489"
104   */
105  public ItemsOffsetPaginated getCollectionItems(String collectionId) {
106    return getCollectionItems(
107        collectionId, new GetCollectionItemsQueryParams(), new GetCollectionItemsHeaders());
108  }
109
110  /**
111   * Retrieves the files and/or folders contained within this collection.
112   *
113   * @param collectionId The ID of the collection. Example: "926489"
114   * @param queryParams Query parameters of getCollectionItems method
115   */
116  public ItemsOffsetPaginated getCollectionItems(
117      String collectionId, GetCollectionItemsQueryParams queryParams) {
118    return getCollectionItems(collectionId, queryParams, new GetCollectionItemsHeaders());
119  }
120
121  /**
122   * Retrieves the files and/or folders contained within this collection.
123   *
124   * @param collectionId The ID of the collection. Example: "926489"
125   * @param headers Headers of getCollectionItems method
126   */
127  public ItemsOffsetPaginated getCollectionItems(
128      String collectionId, GetCollectionItemsHeaders headers) {
129    return getCollectionItems(collectionId, new GetCollectionItemsQueryParams(), headers);
130  }
131
132  /**
133   * Retrieves the files and/or folders contained within this collection.
134   *
135   * @param collectionId The ID of the collection. Example: "926489"
136   * @param queryParams Query parameters of getCollectionItems method
137   * @param headers Headers of getCollectionItems method
138   */
139  public ItemsOffsetPaginated getCollectionItems(
140      String collectionId,
141      GetCollectionItemsQueryParams queryParams,
142      GetCollectionItemsHeaders headers) {
143    Map<String, String> queryParamsMap =
144        prepareParams(
145            mapOf(
146                entryOf("fields", convertToString(queryParams.getFields())),
147                entryOf("offset", convertToString(queryParams.getOffset())),
148                entryOf("limit", convertToString(queryParams.getLimit()))));
149    Map<String, String> headersMap = prepareParams(mergeMaps(mapOf(), headers.getExtraHeaders()));
150    FetchResponse response =
151        this.networkSession
152            .getNetworkClient()
153            .fetch(
154                new FetchOptions.Builder(
155                        String.join(
156                            "",
157                            this.networkSession.getBaseUrls().getBaseUrl(),
158                            "/2.0/collections/",
159                            convertToString(collectionId),
160                            "/items"),
161                        "GET")
162                    .params(queryParamsMap)
163                    .headers(headersMap)
164                    .responseFormat(ResponseFormat.JSON)
165                    .auth(this.auth)
166                    .networkSession(this.networkSession)
167                    .build());
168    return JsonManager.deserialize(response.getData(), ItemsOffsetPaginated.class);
169  }
170
171  /**
172   * Retrieves a collection by its ID.
173   *
174   * @param collectionId The ID of the collection. Example: "926489"
175   */
176  public Collection getCollectionById(String collectionId) {
177    return getCollectionById(collectionId, new GetCollectionByIdHeaders());
178  }
179
180  /**
181   * Retrieves a collection by its ID.
182   *
183   * @param collectionId The ID of the collection. Example: "926489"
184   * @param headers Headers of getCollectionById method
185   */
186  public Collection getCollectionById(String collectionId, GetCollectionByIdHeaders headers) {
187    Map<String, String> headersMap = prepareParams(mergeMaps(mapOf(), headers.getExtraHeaders()));
188    FetchResponse response =
189        this.networkSession
190            .getNetworkClient()
191            .fetch(
192                new FetchOptions.Builder(
193                        String.join(
194                            "",
195                            this.networkSession.getBaseUrls().getBaseUrl(),
196                            "/2.0/collections/",
197                            convertToString(collectionId)),
198                        "GET")
199                    .headers(headersMap)
200                    .responseFormat(ResponseFormat.JSON)
201                    .auth(this.auth)
202                    .networkSession(this.networkSession)
203                    .build());
204    return JsonManager.deserialize(response.getData(), Collection.class);
205  }
206
207  public Authentication getAuth() {
208    return auth;
209  }
210
211  public NetworkSession getNetworkSession() {
212    return networkSession;
213  }
214
215  public static class Builder {
216
217    protected Authentication auth;
218
219    protected NetworkSession networkSession;
220
221    public Builder() {}
222
223    public Builder auth(Authentication auth) {
224      this.auth = auth;
225      return this;
226    }
227
228    public Builder networkSession(NetworkSession networkSession) {
229      this.networkSession = networkSession;
230      return this;
231    }
232
233    public CollectionsManager build() {
234      if (this.networkSession == null) {
235        this.networkSession = new NetworkSession();
236      }
237      return new CollectionsManager(this);
238    }
239  }
240}