001package com.box.sdkgen.managers.weblinks;
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.weblink.WebLink;
015import com.box.sdkgen.serialization.json.JsonManager;
016import java.util.Map;
017
018public class WebLinksManager {
019
020  public Authentication auth;
021
022  public NetworkSession networkSession;
023
024  public WebLinksManager() {
025    this.networkSession = new NetworkSession();
026  }
027
028  protected WebLinksManager(Builder builder) {
029    this.auth = builder.auth;
030    this.networkSession = builder.networkSession;
031  }
032
033  /**
034   * Creates a web link object within a folder.
035   *
036   * @param requestBody Request body of createWebLink method
037   */
038  public WebLink createWebLink(CreateWebLinkRequestBody requestBody) {
039    return createWebLink(requestBody, new CreateWebLinkHeaders());
040  }
041
042  /**
043   * Creates a web link object within a folder.
044   *
045   * @param requestBody Request body of createWebLink method
046   * @param headers Headers of createWebLink method
047   */
048  public WebLink createWebLink(CreateWebLinkRequestBody requestBody, CreateWebLinkHeaders headers) {
049    Map<String, String> headersMap = prepareParams(mergeMaps(mapOf(), headers.getExtraHeaders()));
050    FetchResponse response =
051        this.networkSession
052            .getNetworkClient()
053            .fetch(
054                new FetchOptions.Builder(
055                        String.join(
056                            "", this.networkSession.getBaseUrls().getBaseUrl(), "/2.0/web_links"),
057                        "POST")
058                    .headers(headersMap)
059                    .data(JsonManager.serialize(requestBody))
060                    .contentType("application/json")
061                    .responseFormat(ResponseFormat.JSON)
062                    .auth(this.auth)
063                    .networkSession(this.networkSession)
064                    .build());
065    return JsonManager.deserialize(response.getData(), WebLink.class);
066  }
067
068  /**
069   * Retrieve information about a web link.
070   *
071   * @param webLinkId The ID of the web link. Example: "12345"
072   */
073  public WebLink getWebLinkById(String webLinkId) {
074    return getWebLinkById(webLinkId, new GetWebLinkByIdHeaders());
075  }
076
077  /**
078   * Retrieve information about a web link.
079   *
080   * @param webLinkId The ID of the web link. Example: "12345"
081   * @param headers Headers of getWebLinkById method
082   */
083  public WebLink getWebLinkById(String webLinkId, GetWebLinkByIdHeaders headers) {
084    Map<String, String> headersMap =
085        prepareParams(
086            mergeMaps(
087                mapOf(entryOf("boxapi", convertToString(headers.getBoxapi()))),
088                headers.getExtraHeaders()));
089    FetchResponse response =
090        this.networkSession
091            .getNetworkClient()
092            .fetch(
093                new FetchOptions.Builder(
094                        String.join(
095                            "",
096                            this.networkSession.getBaseUrls().getBaseUrl(),
097                            "/2.0/web_links/",
098                            convertToString(webLinkId)),
099                        "GET")
100                    .headers(headersMap)
101                    .responseFormat(ResponseFormat.JSON)
102                    .auth(this.auth)
103                    .networkSession(this.networkSession)
104                    .build());
105    return JsonManager.deserialize(response.getData(), WebLink.class);
106  }
107
108  /**
109   * Updates a web link object.
110   *
111   * @param webLinkId The ID of the web link. Example: "12345"
112   */
113  public WebLink updateWebLinkById(String webLinkId) {
114    return updateWebLinkById(
115        webLinkId, new UpdateWebLinkByIdRequestBody(), new UpdateWebLinkByIdHeaders());
116  }
117
118  /**
119   * Updates a web link object.
120   *
121   * @param webLinkId The ID of the web link. Example: "12345"
122   * @param requestBody Request body of updateWebLinkById method
123   */
124  public WebLink updateWebLinkById(String webLinkId, UpdateWebLinkByIdRequestBody requestBody) {
125    return updateWebLinkById(webLinkId, requestBody, new UpdateWebLinkByIdHeaders());
126  }
127
128  /**
129   * Updates a web link object.
130   *
131   * @param webLinkId The ID of the web link. Example: "12345"
132   * @param headers Headers of updateWebLinkById method
133   */
134  public WebLink updateWebLinkById(String webLinkId, UpdateWebLinkByIdHeaders headers) {
135    return updateWebLinkById(webLinkId, new UpdateWebLinkByIdRequestBody(), headers);
136  }
137
138  /**
139   * Updates a web link object.
140   *
141   * @param webLinkId The ID of the web link. Example: "12345"
142   * @param requestBody Request body of updateWebLinkById method
143   * @param headers Headers of updateWebLinkById method
144   */
145  public WebLink updateWebLinkById(
146      String webLinkId,
147      UpdateWebLinkByIdRequestBody requestBody,
148      UpdateWebLinkByIdHeaders headers) {
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/web_links/",
159                            convertToString(webLinkId)),
160                        "PUT")
161                    .headers(headersMap)
162                    .data(JsonManager.serialize(requestBody))
163                    .contentType("application/json")
164                    .responseFormat(ResponseFormat.JSON)
165                    .auth(this.auth)
166                    .networkSession(this.networkSession)
167                    .build());
168    return JsonManager.deserialize(response.getData(), WebLink.class);
169  }
170
171  /**
172   * Deletes a web link.
173   *
174   * @param webLinkId The ID of the web link. Example: "12345"
175   */
176  public void deleteWebLinkById(String webLinkId) {
177    deleteWebLinkById(webLinkId, new DeleteWebLinkByIdHeaders());
178  }
179
180  /**
181   * Deletes a web link.
182   *
183   * @param webLinkId The ID of the web link. Example: "12345"
184   * @param headers Headers of deleteWebLinkById method
185   */
186  public void deleteWebLinkById(String webLinkId, DeleteWebLinkByIdHeaders 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/web_links/",
197                            convertToString(webLinkId)),
198                        "DELETE")
199                    .headers(headersMap)
200                    .responseFormat(ResponseFormat.NO_CONTENT)
201                    .auth(this.auth)
202                    .networkSession(this.networkSession)
203                    .build());
204  }
205
206  public Authentication getAuth() {
207    return auth;
208  }
209
210  public NetworkSession getNetworkSession() {
211    return networkSession;
212  }
213
214  public static class Builder {
215
216    protected Authentication auth;
217
218    protected NetworkSession networkSession;
219
220    public Builder() {}
221
222    public Builder auth(Authentication auth) {
223      this.auth = auth;
224      return this;
225    }
226
227    public Builder networkSession(NetworkSession networkSession) {
228      this.networkSession = networkSession;
229      return this;
230    }
231
232    public WebLinksManager build() {
233      if (this.networkSession == null) {
234        this.networkSession = new NetworkSession();
235      }
236      return new WebLinksManager(this);
237    }
238  }
239}