001package com.box.sdkgen.managers.invites;
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.invite.Invite;
015import com.box.sdkgen.serialization.json.JsonManager;
016import java.util.Map;
017
018public class InvitesManager {
019
020  public Authentication auth;
021
022  public NetworkSession networkSession;
023
024  public InvitesManager() {
025    this.networkSession = new NetworkSession();
026  }
027
028  protected InvitesManager(Builder builder) {
029    this.auth = builder.auth;
030    this.networkSession = builder.networkSession;
031  }
032
033  /**
034   * Invites an existing external user to join an enterprise.
035   *
036   * <p>The existing user can not be part of another enterprise and must already have a Box account.
037   * Once invited, the user will receive an email and are prompted to accept the invitation within
038   * the Box web application.
039   *
040   * <p>This method requires the "Manage An Enterprise" scope enabled for the application, which can
041   * be enabled within the developer console.
042   *
043   * @param requestBody Request body of createInvite method
044   */
045  public Invite createInvite(CreateInviteRequestBody requestBody) {
046    return createInvite(requestBody, new CreateInviteQueryParams(), new CreateInviteHeaders());
047  }
048
049  /**
050   * Invites an existing external user to join an enterprise.
051   *
052   * <p>The existing user can not be part of another enterprise and must already have a Box account.
053   * Once invited, the user will receive an email and are prompted to accept the invitation within
054   * the Box web application.
055   *
056   * <p>This method requires the "Manage An Enterprise" scope enabled for the application, which can
057   * be enabled within the developer console.
058   *
059   * @param requestBody Request body of createInvite method
060   * @param queryParams Query parameters of createInvite method
061   */
062  public Invite createInvite(
063      CreateInviteRequestBody requestBody, CreateInviteQueryParams queryParams) {
064    return createInvite(requestBody, queryParams, new CreateInviteHeaders());
065  }
066
067  /**
068   * Invites an existing external user to join an enterprise.
069   *
070   * <p>The existing user can not be part of another enterprise and must already have a Box account.
071   * Once invited, the user will receive an email and are prompted to accept the invitation within
072   * the Box web application.
073   *
074   * <p>This method requires the "Manage An Enterprise" scope enabled for the application, which can
075   * be enabled within the developer console.
076   *
077   * @param requestBody Request body of createInvite method
078   * @param headers Headers of createInvite method
079   */
080  public Invite createInvite(CreateInviteRequestBody requestBody, CreateInviteHeaders headers) {
081    return createInvite(requestBody, new CreateInviteQueryParams(), headers);
082  }
083
084  /**
085   * Invites an existing external user to join an enterprise.
086   *
087   * <p>The existing user can not be part of another enterprise and must already have a Box account.
088   * Once invited, the user will receive an email and are prompted to accept the invitation within
089   * the Box web application.
090   *
091   * <p>This method requires the "Manage An Enterprise" scope enabled for the application, which can
092   * be enabled within the developer console.
093   *
094   * @param requestBody Request body of createInvite method
095   * @param queryParams Query parameters of createInvite method
096   * @param headers Headers of createInvite method
097   */
098  public Invite createInvite(
099      CreateInviteRequestBody requestBody,
100      CreateInviteQueryParams queryParams,
101      CreateInviteHeaders headers) {
102    Map<String, String> queryParamsMap =
103        prepareParams(mapOf(entryOf("fields", convertToString(queryParams.getFields()))));
104    Map<String, String> headersMap = prepareParams(mergeMaps(mapOf(), headers.getExtraHeaders()));
105    FetchResponse response =
106        this.networkSession
107            .getNetworkClient()
108            .fetch(
109                new FetchOptions.Builder(
110                        String.join(
111                            "", this.networkSession.getBaseUrls().getBaseUrl(), "/2.0/invites"),
112                        "POST")
113                    .params(queryParamsMap)
114                    .headers(headersMap)
115                    .data(JsonManager.serialize(requestBody))
116                    .contentType("application/json")
117                    .responseFormat(ResponseFormat.JSON)
118                    .auth(this.auth)
119                    .networkSession(this.networkSession)
120                    .build());
121    return JsonManager.deserialize(response.getData(), Invite.class);
122  }
123
124  /**
125   * Returns the status of a user invite.
126   *
127   * @param inviteId The ID of an invite. Example: "213723"
128   */
129  public Invite getInviteById(String inviteId) {
130    return getInviteById(inviteId, new GetInviteByIdQueryParams(), new GetInviteByIdHeaders());
131  }
132
133  /**
134   * Returns the status of a user invite.
135   *
136   * @param inviteId The ID of an invite. Example: "213723"
137   * @param queryParams Query parameters of getInviteById method
138   */
139  public Invite getInviteById(String inviteId, GetInviteByIdQueryParams queryParams) {
140    return getInviteById(inviteId, queryParams, new GetInviteByIdHeaders());
141  }
142
143  /**
144   * Returns the status of a user invite.
145   *
146   * @param inviteId The ID of an invite. Example: "213723"
147   * @param headers Headers of getInviteById method
148   */
149  public Invite getInviteById(String inviteId, GetInviteByIdHeaders headers) {
150    return getInviteById(inviteId, new GetInviteByIdQueryParams(), headers);
151  }
152
153  /**
154   * Returns the status of a user invite.
155   *
156   * @param inviteId The ID of an invite. Example: "213723"
157   * @param queryParams Query parameters of getInviteById method
158   * @param headers Headers of getInviteById method
159   */
160  public Invite getInviteById(
161      String inviteId, GetInviteByIdQueryParams queryParams, GetInviteByIdHeaders headers) {
162    Map<String, String> queryParamsMap =
163        prepareParams(mapOf(entryOf("fields", convertToString(queryParams.getFields()))));
164    Map<String, String> headersMap = prepareParams(mergeMaps(mapOf(), headers.getExtraHeaders()));
165    FetchResponse response =
166        this.networkSession
167            .getNetworkClient()
168            .fetch(
169                new FetchOptions.Builder(
170                        String.join(
171                            "",
172                            this.networkSession.getBaseUrls().getBaseUrl(),
173                            "/2.0/invites/",
174                            convertToString(inviteId)),
175                        "GET")
176                    .params(queryParamsMap)
177                    .headers(headersMap)
178                    .responseFormat(ResponseFormat.JSON)
179                    .auth(this.auth)
180                    .networkSession(this.networkSession)
181                    .build());
182    return JsonManager.deserialize(response.getData(), Invite.class);
183  }
184
185  public Authentication getAuth() {
186    return auth;
187  }
188
189  public NetworkSession getNetworkSession() {
190    return networkSession;
191  }
192
193  public static class Builder {
194
195    protected Authentication auth;
196
197    protected NetworkSession networkSession;
198
199    public Builder() {}
200
201    public Builder auth(Authentication auth) {
202      this.auth = auth;
203      return this;
204    }
205
206    public Builder networkSession(NetworkSession networkSession) {
207      this.networkSession = networkSession;
208      return this;
209    }
210
211    public InvitesManager build() {
212      if (this.networkSession == null) {
213        this.networkSession = new NetworkSession();
214      }
215      return new InvitesManager(this);
216    }
217  }
218}