001package com.box.sdkgen.managers.avatars;
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.MultipartItem;
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.useravatar.UserAvatar;
015import com.box.sdkgen.serialization.json.JsonManager;
016import java.io.InputStream;
017import java.util.Arrays;
018import java.util.Map;
019
020public class AvatarsManager {
021
022  public Authentication auth;
023
024  public NetworkSession networkSession;
025
026  public AvatarsManager() {
027    this.networkSession = new NetworkSession();
028  }
029
030  protected AvatarsManager(Builder builder) {
031    this.auth = builder.auth;
032    this.networkSession = builder.networkSession;
033  }
034
035  /**
036   * Retrieves an image of a the user's avatar.
037   *
038   * @param userId The ID of the user. Example: "12345"
039   */
040  public InputStream getUserAvatar(String userId) {
041    return getUserAvatar(userId, new GetUserAvatarHeaders());
042  }
043
044  /**
045   * Retrieves an image of a the user's avatar.
046   *
047   * @param userId The ID of the user. Example: "12345"
048   * @param headers Headers of getUserAvatar method
049   */
050  public InputStream getUserAvatar(String userId, GetUserAvatarHeaders 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                            "/avatar"),
063                        "GET")
064                    .headers(headersMap)
065                    .responseFormat(ResponseFormat.BINARY)
066                    .auth(this.auth)
067                    .networkSession(this.networkSession)
068                    .build());
069    return response.getContent();
070  }
071
072  /**
073   * Adds or updates a user avatar.
074   *
075   * @param userId The ID of the user. Example: "12345"
076   * @param requestBody Request body of createUserAvatar method
077   */
078  public UserAvatar createUserAvatar(String userId, CreateUserAvatarRequestBody requestBody) {
079    return createUserAvatar(userId, requestBody, new CreateUserAvatarHeaders());
080  }
081
082  /**
083   * Adds or updates a user avatar.
084   *
085   * @param userId The ID of the user. Example: "12345"
086   * @param requestBody Request body of createUserAvatar method
087   * @param headers Headers of createUserAvatar method
088   */
089  public UserAvatar createUserAvatar(
090      String userId, CreateUserAvatarRequestBody requestBody, CreateUserAvatarHeaders headers) {
091    Map<String, String> headersMap = prepareParams(mergeMaps(mapOf(), headers.getExtraHeaders()));
092    FetchResponse response =
093        this.networkSession
094            .getNetworkClient()
095            .fetch(
096                new FetchOptions.Builder(
097                        String.join(
098                            "",
099                            this.networkSession.getBaseUrls().getBaseUrl(),
100                            "/2.0/users/",
101                            convertToString(userId),
102                            "/avatar"),
103                        "POST")
104                    .headers(headersMap)
105                    .multipartData(
106                        Arrays.asList(
107                            new MultipartItem.Builder("pic")
108                                .fileStream(requestBody.getPic())
109                                .fileName(requestBody.getPicFileName())
110                                .contentType(requestBody.getPicContentType())
111                                .build()))
112                    .contentType("multipart/form-data")
113                    .responseFormat(ResponseFormat.JSON)
114                    .auth(this.auth)
115                    .networkSession(this.networkSession)
116                    .build());
117    return JsonManager.deserialize(response.getData(), UserAvatar.class);
118  }
119
120  /**
121   * Removes an existing user avatar. You cannot reverse this operation.
122   *
123   * @param userId The ID of the user. Example: "12345"
124   */
125  public void deleteUserAvatar(String userId) {
126    deleteUserAvatar(userId, new DeleteUserAvatarHeaders());
127  }
128
129  /**
130   * Removes an existing user avatar. You cannot reverse this operation.
131   *
132   * @param userId The ID of the user. Example: "12345"
133   * @param headers Headers of deleteUserAvatar method
134   */
135  public void deleteUserAvatar(String userId, DeleteUserAvatarHeaders 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                            "/avatar"),
148                        "DELETE")
149                    .headers(headersMap)
150                    .responseFormat(ResponseFormat.NO_CONTENT)
151                    .auth(this.auth)
152                    .networkSession(this.networkSession)
153                    .build());
154  }
155
156  public Authentication getAuth() {
157    return auth;
158  }
159
160  public NetworkSession getNetworkSession() {
161    return networkSession;
162  }
163
164  public static class Builder {
165
166    protected Authentication auth;
167
168    protected NetworkSession networkSession;
169
170    public Builder() {}
171
172    public Builder auth(Authentication auth) {
173      this.auth = auth;
174      return this;
175    }
176
177    public Builder networkSession(NetworkSession networkSession) {
178      this.networkSession = networkSession;
179      return this;
180    }
181
182    public AvatarsManager build() {
183      if (this.networkSession == null) {
184        this.networkSession = new NetworkSession();
185      }
186      return new AvatarsManager(this);
187    }
188  }
189}