001package com.box.sdkgen.managers.recentitems;
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.recentitems.RecentItems;
015import com.box.sdkgen.serialization.json.JsonManager;
016import java.util.Map;
017
018public class RecentItemsManager {
019
020  public Authentication auth;
021
022  public NetworkSession networkSession;
023
024  public RecentItemsManager() {
025    this.networkSession = new NetworkSession();
026  }
027
028  protected RecentItemsManager(Builder builder) {
029    this.auth = builder.auth;
030    this.networkSession = builder.networkSession;
031  }
032
033  /**
034   * Returns information about the recent items accessed by a user, either in the last 90 days or up
035   * to the last 1000 items accessed.
036   */
037  public RecentItems getRecentItems() {
038    return getRecentItems(new GetRecentItemsQueryParams(), new GetRecentItemsHeaders());
039  }
040
041  /**
042   * Returns information about the recent items accessed by a user, either in the last 90 days or up
043   * to the last 1000 items accessed.
044   *
045   * @param queryParams Query parameters of getRecentItems method
046   */
047  public RecentItems getRecentItems(GetRecentItemsQueryParams queryParams) {
048    return getRecentItems(queryParams, new GetRecentItemsHeaders());
049  }
050
051  /**
052   * Returns information about the recent items accessed by a user, either in the last 90 days or up
053   * to the last 1000 items accessed.
054   *
055   * @param headers Headers of getRecentItems method
056   */
057  public RecentItems getRecentItems(GetRecentItemsHeaders headers) {
058    return getRecentItems(new GetRecentItemsQueryParams(), headers);
059  }
060
061  /**
062   * Returns information about the recent items accessed by a user, either in the last 90 days or up
063   * to the last 1000 items accessed.
064   *
065   * @param queryParams Query parameters of getRecentItems method
066   * @param headers Headers of getRecentItems method
067   */
068  public RecentItems getRecentItems(
069      GetRecentItemsQueryParams queryParams, GetRecentItemsHeaders headers) {
070    Map<String, String> queryParamsMap =
071        prepareParams(
072            mapOf(
073                entryOf("fields", convertToString(queryParams.getFields())),
074                entryOf("limit", convertToString(queryParams.getLimit())),
075                entryOf("marker", convertToString(queryParams.getMarker()))));
076    Map<String, String> headersMap = prepareParams(mergeMaps(mapOf(), headers.getExtraHeaders()));
077    FetchResponse response =
078        this.networkSession
079            .getNetworkClient()
080            .fetch(
081                new FetchOptions.Builder(
082                        String.join(
083                            "",
084                            this.networkSession.getBaseUrls().getBaseUrl(),
085                            "/2.0/recent_items"),
086                        "GET")
087                    .params(queryParamsMap)
088                    .headers(headersMap)
089                    .responseFormat(ResponseFormat.JSON)
090                    .auth(this.auth)
091                    .networkSession(this.networkSession)
092                    .build());
093    return JsonManager.deserialize(response.getData(), RecentItems.class);
094  }
095
096  public Authentication getAuth() {
097    return auth;
098  }
099
100  public NetworkSession getNetworkSession() {
101    return networkSession;
102  }
103
104  public static class Builder {
105
106    protected Authentication auth;
107
108    protected NetworkSession networkSession;
109
110    public Builder() {}
111
112    public Builder auth(Authentication auth) {
113      this.auth = auth;
114      return this;
115    }
116
117    public Builder networkSession(NetworkSession networkSession) {
118      this.networkSession = networkSession;
119      return this;
120    }
121
122    public RecentItemsManager build() {
123      if (this.networkSession == null) {
124        this.networkSession = new NetworkSession();
125      }
126      return new RecentItemsManager(this);
127    }
128  }
129}