com.base.Base   A
last analyzed

Complexity

Total Complexity 25

Size/Duplication

Total Lines 344
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 25
c 3
b 0
f 0
dl 0
loc 344
rs 10

36 Methods

Rating   Name   Duplication   Size   Complexity  
getUserAccessToken 0 10 ?
A channelService() 0 2 1
setClient 0 3 ?
A sendRequest(String,String,Map,Map,Map,AccessToken,Response) 0 10 1
A Base(String,String) 0 5 1
A userService() 0 2 1
makeModel 0 7 ?
getClient 0 2 ?
A getUserAccessToken(String,String) 0 10 2
A invitationService() 0 2 1
bootstrapServices 0 10 ?
channelService 0 2 ?
A teamService() 0 2 1
preferenceService 0 2 ?
A sendRequest(String,String,Map,Map) 0 4 1
A preferenceService() 0 2 1
invitationService 0 2 ?
A sendRequest(String,String) 0 3 1
channelMemberService 0 2 ?
threadService 0 2 ?
A sendRequest(String,String,Map,Map,Map,AccessToken) 0 4 1
teamService 0 2 ?
A sendRequest(String,String,Map) 0 3 1
A threadService() 0 2 1
A getClient() 0 2 1
A channelMemberService() 0 2 1
teamMemberService 0 2 ?
A sendRequest(String,String,Map,Map,Map) 0 4 1
A Base(String) 0 4 1
A Base() 0 3 1
A teamMemberService() 0 2 1
A Base(BaseClient) 0 3 1
A setClient(BaseClient) 0 3 1
A makeModel(Type,String) 0 7 1
A bootstrapServices() 0 10 1
A messageService() 0 2 1
1
package com.base;
2
3
import com.base.Auth.AccessToken;
4
import com.base.Exceptions.BaseException;
5
import com.base.Exceptions.BaseHttpException;
6
import com.base.Http.Request.Request;
7
import com.base.Http.Response.Response;
8
import com.base.Models.ResponseModel;
9
import com.base.Services.*;
10
import com.google.gson.Gson;
11
import com.google.gson.GsonBuilder;
12
13
import java.io.File;
14
import java.lang.reflect.Type;
15
import java.util.HashMap;
16
import java.util.Map;
17
18
public final class Base {
19
20
    /**
21
     * {@link BaseClient} client
22
     */
23
    private BaseClient client;
24
25
    /**
26
     * Create UserService Reference
27
     */
28
    private UserService userService;
29
30
    /**
31
     * Create TeamService Reference
32
     */
33
    private TeamService teamService;
34
35
    /**
36
     * Create TeamMemberService Reference
37
     */
38
    private TeamMemberService teamMemberService;
39
40
    /**
41
     * Create ChannelService Reference
42
     */
43
    private ChannelService channelService;
44
45
    /**
46
     * Create ChannelMemberService Reference
47
     */
48
    private ChannelMemberService channelMemberService;
49
50
    /**
51
     * Create ThreadService Reference
52
     */
53
    private ThreadService threadService;
54
55
    /**
56
     * Create MessageService Reference
57
     */
58
    private MessageService messageService;
59
60
    /**
61
     * Create PreferencesService Reference
62
     */
63
    private PreferenceService preferenceService;
64
65
    /**
66
     * Create InvitationService  Reference
67
     */
68
    private InvitationService invitationService;
69
70
    /**
71
     * Construct {@link Base} with all bootstrapServices
72
     */
73
    public Base() {
74
        this.client = new BaseClient();
75
        this.bootstrapServices();
76
    }
77
78
    /**
79
     * Construct {@link Base} and set accessToken.
80
     *
81
     * @param accessToken Client access Token
82
     */
83
    public Base(String accessToken) {
84
        this();
85
        this.getClient()
86
                .setAccessToken(new AccessToken(accessToken));
87
    }
88
89
    /**
90
     * Construct {@link Base} wtih specified clientId and clientSecret
91
     *
92
     * @param clientId     Client ID
93
     * @param clientSecret Client Secret
94
     */
95
    public Base(String clientId, String clientSecret) {
96
        this();
97
        this.getClient()
98
                .setClientId(clientId)
99
                .setClientSecret(clientSecret);
100
    }
101
102
    /**
103
     * Construct {@link BaseClient}
104
     *
105
     * @param client baseClient
106
     */
107
    public Base(BaseClient client) {
108
        this();
109
        this.client = client;
110
    }
111
112
    /**
113
     * Make Model.
114
     *
115
     * @param model    Model Type.
116
     * @param jsonData JSON Data.
117
     * @return Model Object.
118
     */
119
    public static Object makeModel(Type model, String jsonData) {
120
        GsonBuilder builder = new GsonBuilder();
121
        Gson gson = builder.create();
122
        ResponseModel response = gson.fromJson(jsonData, ResponseModel.class);
123
        Object data = response.getData();
124
        String jData = gson.toJson(data);
125
        return gson.fromJson(jData, model);
126
    }
127
128
    /**
129
     * @return Base Client
130
     */
131
    public BaseClient getClient() {
132
        return client;
133
    }
134
135
    /**
136
     * Set Base Client
137
     *
138
     * @param client
139
     * @return
140
     */
141
    public Base setClient(BaseClient client) {
142
        this.client = client;
143
        return this;
144
    }
145
146
    /**
147
     * Send {@link Request} with endpoint and method
148
     *
149
     * @param endpoint API EndPoint
150
     * @param method   Method Type
151
     * @return Response
152
     * @throws BaseHttpException Exception
153
     */
154
    public Response sendRequest(String endpoint, String method)
155
            throws BaseHttpException {
156
        return this.sendRequest(endpoint, method, new HashMap<>());
157
    }
158
159
    /**
160
     * Send {@link Request} with endpoint, method and Parameters
161
     *
162
     * @param endpoint   API EndPoint
163
     * @param method     Method Type
164
     * @param parameters Parameters
165
     * @return Response
166
     * @throws BaseHttpException Exception
167
     */
168
    public Response sendRequest(String endpoint, String method, Map<String, String> parameters)
169
            throws BaseHttpException {
170
        return this.sendRequest(endpoint, method, parameters, new HashMap<>());
171
    }
172
173
    /**
174
     * Send {@link Request} with endpoint, method, Parameters and Headers
175
     *
176
     * @param endpoint   API EndPoint
177
     * @param method     Method Type
178
     * @param parameters Parameters
179
     * @param headers    Additional Headers
180
     * @return Response
181
     * @throws BaseHttpException Exception
182
     */
183
    public Response sendRequest(String endpoint, String method, Map<String, String> parameters, Map<String, String>
184
            headers)
185
            throws BaseHttpException {
186
        return this.sendRequest(endpoint, method, parameters, headers, new HashMap<>());
187
    }
188
189
    /**
190
     * Send {@link Request} with endpoint, method, Parameters, Headers and Files
191
     *
192
     * @param endpoint   API EndPoint
193
     * @param method     Method Type
194
     * @param parameters Parameters
195
     * @param files      Files that you want to upload
196
     * @return Response
197
     * @throws BaseHttpException Exception
198
     */
199
    public Response sendRequest(String endpoint, String method, Map<String, String> parameters, Map<String, String>
200
            headers, Map<String, File> files)
201
            throws BaseHttpException {
202
        return this.sendRequest(endpoint, method, parameters, headers, files, null, null);
203
    }
204
205
    /**
206
     * Send {@link Request} with endpoint, method, Parameters, Headers, Files and AccessToken
207
     *
208
     * @param endpoint    API EndPoint
209
     * @param method      Method Type
210
     * @param parameters  Parameters
211
     * @param files       Files that you want to upload
212
     * @param accessToken AccessToken of User
213
     * @return Response
214
     * @throws BaseHttpException Exception
215
     */
216
    public Response sendRequest(String endpoint, String method, Map<String, String> parameters, Map<String, String>
217
            headers, Map<String, File> files, AccessToken accessToken)
218
            throws BaseHttpException {
219
        return this.sendRequest(endpoint, method, parameters, headers, files, accessToken, null);
220
    }
221
222
    /**
223
     * Send {@link Request} with endpoint, method, Parameters, Headers, Files, AccessToken and Response
224
     *
225
     * @param endpoint    API EndPoint
226
     * @param method      Method Type
227
     * @param parameters  Parameters
228
     * @param files       Files that you want to upload
229
     * @param accessToken AccessToken of User
230
     * @param response    Response
231
     * @return Response
232
     * @throws BaseHttpException Exception
233
     */
234
    public Response sendRequest(String endpoint, String method, Map<String, String> parameters, Map<String, String>
235
            headers, Map<String, File> files, AccessToken accessToken, Response response) throws BaseHttpException {
236
        Request request = new Request(endpoint, method);
237
238
        request.setHeaders(headers)
239
                .setFiles(files)
240
                .setParameters(parameters)
241
                .setAccessToken(accessToken);
242
243
        return this.getClient().sendRequest(request, response);
244
    }
245
246
    /**
247
     * Get User Access Token (Login).
248
     *
249
     * @param email    User Email
250
     * @param password User Password
251
     * @return AccessToken
252
     * @throws BaseException Exception
253
     */
254
    public AccessToken getUserAccessToken(String email, String password) throws BaseException {
255
        try {
256
            Map<String, String> parameters = new HashMap<>();
257
            parameters.put("email", email);
258
            parameters.put("password", password);
259
260
            Response response = this.sendRequest("/users/login", Request.METHOD_POST, parameters);
261
            return (AccessToken) makeModel(AccessToken.class, response.getBody());
262
        } catch (BaseHttpException e) {
263
            throw new BaseException(500, e.getMessage());
264
        }
265
    }
266
267
    /**
268
     * Base UserService
269
     *
270
     * @return UserService
271
     */
272
    public UserService userService() {
273
        return this.userService;
274
    }
275
276
    /**
277
     * Base TeamService
278
     *
279
     * @return TeamService
280
     */
281
    public TeamService teamService() {
282
        return this.teamService;
283
    }
284
285
    /**
286
     * Base TeamMemberService
287
     *
288
     * @return TeamMemberService
289
     */
290
    public TeamMemberService teamMemberService() {
291
        return this.teamMemberService;
292
    }
293
294
    /**
295
     * Base ChannelService
296
     *
297
     * @return ChannelService
298
     */
299
    public ChannelService channelService() {
300
        return this.channelService;
301
    }
302
303
    /**
304
     * Base ChannelMemberService
305
     *
306
     * @return ChannelMemberService
307
     */
308
    public ChannelMemberService channelMemberService() {
309
        return this.channelMemberService;
310
    }
311
312
    /**
313
     * Base ThreadService
314
     *
315
     * @return ThreadService
316
     */
317
    public ThreadService threadService() {
318
        return this.threadService;
319
    }
320
321
    /**
322
     * Base MessageService
323
     *
324
     * @return MessageService
325
     */
326
    public MessageService messageService() {
327
        return this.messageService;
328
    }
329
330
    /**
331
     * Base PreferencesService
332
     *
333
     * @return PreferencesService
334
     */
335
    public PreferenceService preferenceService() {
336
        return this.preferenceService;
337
    }
338
339
    /**
340
     * Base PreferencesService
341
     *
342
     * @return PreferencesService
343
     */
344
    public InvitationService invitationService() {
345
        return this.invitationService;
346
    }
347
348
349
    /**
350
     * Generate All bootstrapServices
351
     */
352
    private void bootstrapServices() {
353
        this.userService = new UserService(this);
354
        this.teamService = new TeamService(this);
355
        this.teamMemberService = new TeamMemberService(this);
356
        this.channelService = new ChannelService(this);
357
        this.channelMemberService = new ChannelMemberService(this);
358
        this.threadService = new ThreadService(this);
359
        this.messageService = new MessageService(this);
360
        this.preferenceService = new PreferenceService(this);
361
        this.invitationService = new InvitationService(this);
362
    }
363
}
364