001package com.box.sdkgen.managers.emailaliases;
002
003import static com.box.sdkgen.internal.utils.UtilsManager.convertToString;
004import static com.box.sdkgen.internal.utils.UtilsManager.mapOf;
005import static com.box.sdkgen.internal.utils.UtilsManager.mergeMaps;
006import static com.box.sdkgen.internal.utils.UtilsManager.prepareParams;
007
008import com.box.sdkgen.networking.auth.Authentication;
009import com.box.sdkgen.networking.fetchoptions.FetchOptions;
010import com.box.sdkgen.networking.fetchoptions.ResponseFormat;
011import com.box.sdkgen.networking.fetchresponse.FetchResponse;
012import com.box.sdkgen.networking.network.NetworkSession;
013import com.box.sdkgen.schemas.emailalias.EmailAlias;
014import com.box.sdkgen.schemas.emailaliases.EmailAliases;
015import com.box.sdkgen.serialization.json.JsonManager;
016import java.util.Map;
017
018public class EmailAliasesManager {
019
020  public Authentication auth;
021
022  public NetworkSession networkSession;
023
024  public EmailAliasesManager() {
025    this.networkSession = new NetworkSession();
026  }
027
028  protected EmailAliasesManager(Builder builder) {
029    this.auth = builder.auth;
030    this.networkSession = builder.networkSession;
031  }
032
033  /**
034   * Retrieves all email aliases for a user. The collection does not include the primary login for
035   * the user.
036   *
037   * @param userId The ID of the user. Example: "12345"
038   */
039  public EmailAliases getUserEmailAliases(String userId) {
040    return getUserEmailAliases(userId, new GetUserEmailAliasesHeaders());
041  }
042
043  /**
044   * Retrieves all email aliases for a user. The collection does not include the primary login for
045   * the user.
046   *
047   * @param userId The ID of the user. Example: "12345"
048   * @param headers Headers of getUserEmailAliases method
049   */
050  public EmailAliases getUserEmailAliases(String userId, GetUserEmailAliasesHeaders headers) {
051    Map<String, String> headersMap = prepareParams(mergeMaps(mapOf(), headers.getExtraHeaders()));
052    FetchResponse response =
053        this.networkSession
054            .getNetworkClient()
055            .fetch(
056                new FetchOptions.Builder(
057                        String.join(
058                            "",
059                            this.networkSession.getBaseUrls().getBaseUrl(),
060                            "/2.0/users/",
061                            convertToString(userId),
062                            "/email_aliases"),
063                        "GET")
064                    .headers(headersMap)
065                    .responseFormat(ResponseFormat.JSON)
066                    .auth(this.auth)
067                    .networkSession(this.networkSession)
068                    .build());
069    return JsonManager.deserialize(response.getData(), EmailAliases.class);
070  }
071
072  /**
073   * Adds a new email alias to a user account..
074   *
075   * @param userId The ID of the user. Example: "12345"
076   * @param requestBody Request body of createUserEmailAlias method
077   */
078  public EmailAlias createUserEmailAlias(
079      String userId, CreateUserEmailAliasRequestBody requestBody) {
080    return createUserEmailAlias(userId, requestBody, new CreateUserEmailAliasHeaders());
081  }
082
083  /**
084   * Adds a new email alias to a user account..
085   *
086   * @param userId The ID of the user. Example: "12345"
087   * @param requestBody Request body of createUserEmailAlias method
088   * @param headers Headers of createUserEmailAlias method
089   */
090  public EmailAlias createUserEmailAlias(
091      String userId,
092      CreateUserEmailAliasRequestBody requestBody,
093      CreateUserEmailAliasHeaders headers) {
094    Map<String, String> headersMap = prepareParams(mergeMaps(mapOf(), headers.getExtraHeaders()));
095    FetchResponse response =
096        this.networkSession
097            .getNetworkClient()
098            .fetch(
099                new FetchOptions.Builder(
100                        String.join(
101                            "",
102                            this.networkSession.getBaseUrls().getBaseUrl(),
103                            "/2.0/users/",
104                            convertToString(userId),
105                            "/email_aliases"),
106                        "POST")
107                    .headers(headersMap)
108                    .data(JsonManager.serialize(requestBody))
109                    .contentType("application/json")
110                    .responseFormat(ResponseFormat.JSON)
111                    .auth(this.auth)
112                    .networkSession(this.networkSession)
113                    .build());
114    return JsonManager.deserialize(response.getData(), EmailAlias.class);
115  }
116
117  /**
118   * Removes an email alias from a user.
119   *
120   * @param userId The ID of the user. Example: "12345"
121   * @param emailAliasId The ID of the email alias. Example: "23432"
122   */
123  public void deleteUserEmailAliasById(String userId, String emailAliasId) {
124    deleteUserEmailAliasById(userId, emailAliasId, new DeleteUserEmailAliasByIdHeaders());
125  }
126
127  /**
128   * Removes an email alias from a user.
129   *
130   * @param userId The ID of the user. Example: "12345"
131   * @param emailAliasId The ID of the email alias. Example: "23432"
132   * @param headers Headers of deleteUserEmailAliasById method
133   */
134  public void deleteUserEmailAliasById(
135      String userId, String emailAliasId, DeleteUserEmailAliasByIdHeaders headers) {
136    Map<String, String> headersMap = prepareParams(mergeMaps(mapOf(), headers.getExtraHeaders()));
137    FetchResponse response =
138        this.networkSession
139            .getNetworkClient()
140            .fetch(
141                new FetchOptions.Builder(
142                        String.join(
143                            "",
144                            this.networkSession.getBaseUrls().getBaseUrl(),
145                            "/2.0/users/",
146                            convertToString(userId),
147                            "/email_aliases/",
148                            convertToString(emailAliasId)),
149                        "DELETE")
150                    .headers(headersMap)
151                    .responseFormat(ResponseFormat.NO_CONTENT)
152                    .auth(this.auth)
153                    .networkSession(this.networkSession)
154                    .build());
155  }
156
157  public Authentication getAuth() {
158    return auth;
159  }
160
161  public NetworkSession getNetworkSession() {
162    return networkSession;
163  }
164
165  public static class Builder {
166
167    protected Authentication auth;
168
169    protected NetworkSession networkSession;
170
171    public Builder() {}
172
173    public Builder auth(Authentication auth) {
174      this.auth = auth;
175      return this;
176    }
177
178    public Builder networkSession(NetworkSession networkSession) {
179      this.networkSession = networkSession;
180      return this;
181    }
182
183    public EmailAliasesManager build() {
184      if (this.networkSession == null) {
185        this.networkSession = new NetworkSession();
186      }
187      return new EmailAliasesManager(this);
188    }
189  }
190}