com.base.BaseClient   A
last analyzed

Complexity

Total Complexity 29

Size/Duplication

Total Lines 324
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 29
c 1
b 0
f 0
dl 0
loc 324
rs 10

38 Methods

Rating   Name   Duplication   Size   Complexity  
setAccessToken 0 3 ?
setHeaders 0 3 ?
sendRequest 0 5 ?
A buildUrl(String) 0 2 1
A getHttpClient() 0 2 1
A setApiUrl(String) 0 3 1
A setResponseHandler(HandlerInterface) 0 3 1
A buildHeaders(Request) 0 10 1
buildHeaders 0 10 ?
A setHttpClient(HttpClientInterface) 0 3 1
A getApiUrl() 0 5 2
A BaseClient() 0 5 1
addHeader 0 3 ?
getResponseHandler 0 2 ?
A BaseClient(HttpClientInterface) 0 3 1
setApiUrl 0 3 ?
setResponseHandler 0 3 ?
getDefaultHeaders 0 4 ?
setHttpClient 0 3 ?
A sendRequest(Request,Response) 0 5 1
A getHeaders() 0 2 1
A setClientId(String) 0 3 1
getAuthHeaders 0 22 ?
A setClientSecret(String) 0 3 1
A getClientId() 0 2 1
getApiUrl 0 5 ?
A getDefaultHeaders() 0 4 1
buildUrl 0 2 ?
A BaseClient(HttpClientInterface,HandlerInterface) 0 4 1
A setAccessToken(AccessToken) 0 3 1
A getResponseHandler() 0 2 1
B getAuthHeaders(Request) 0 22 5
A addHeader(String,String) 0 3 1
A setHeaders(Map) 0 3 1
A getAccessToken() 0 2 1
A getClientSecret() 0 2 1
getHttpClient 0 2 ?
A BaseClient(HttpClientInterface,HandlerInterface,Map) 0 3 1
1
package com.base;
2
3
import com.base.Auth.AccessToken;
4
import com.base.Exceptions.BaseHttpException;
5
import com.base.Http.Clients.HttpClientInterface;
6
import com.base.Http.Clients.OkHttpClient;
7
import com.base.Http.Request.Request;
8
import com.base.Http.Response.Handlers.BaseResponseHandler;
9
import com.base.Http.Response.Handlers.HandlerInterface;
10
import com.base.Http.Response.Response;
11
12
import java.util.HashMap;
13
import java.util.Map;
14
15
public final class BaseClient {
16
17
    /**
18
     * BaseClient Default API URL
19
     */
20
    private static final String DEFAULT_API_URL = "https://platform.baseapp.in/api";
21
22
    /**
23
     * Base Client apiUrl
24
     */
25
    private String apiUrl;
26
27
    /**
28
     * Base Client clientId
29
     */
30
    private String clientId;
31
32
    /**
33
     * Base Client clientSecret
34
     */
35
    private String clientSecret;
36
37
    /**
38
     * Base Client {@link AccessToken}
39
     */
40
    private AccessToken accessToken;
41
42
    /**
43
     * Base Client headers
44
     */
45
    private Map<String, String> headers;
46
47
    /**
48
     * Base Client {@link HttpClientInterface}
49
     */
50
    private HttpClientInterface httpClient;
51
52
    /**
53
     * Base Client {@link HandlerInterface}
54
     */
55
    private HandlerInterface responseHandler;
56
57
    /**
58
     * Generate {@link BaseClient} Object
59
     */
60
    public BaseClient() {
61
        this.httpClient = new OkHttpClient();
62
        this.apiUrl = BaseClient.DEFAULT_API_URL;
63
        this.headers = BaseClient.getDefaultHeaders();
64
        this.responseHandler = new BaseResponseHandler();
65
    }
66
67
    /**
68
     * Generate {@link BaseClient} with {@link HttpClientInterface}
69
     *
70
     * @param httpClient
71
     */
72
    public BaseClient(HttpClientInterface httpClient) {
73
        this();
74
        this.httpClient = httpClient;
75
    }
76
77
    /**
78
     * Generate {@link BaseClient} with {@link HttpClientInterface} and {@link HandlerInterface}
79
     *
80
     * @param httpClient
81
     * @param responseHandler
82
     */
83
    public BaseClient(HttpClientInterface httpClient, HandlerInterface responseHandler) {
84
        this();
85
        this.httpClient = httpClient;
86
        this.responseHandler = responseHandler;
87
    }
88
89
    /**
90
     * Generate {@link BaseClient} with {@link HttpClientInterface} and {@link HandlerInterface} and Headers
91
     *
92
     * @param httpClient
93
     * @param responseHandler
94
     * @param headers
95
     */
96
    public BaseClient(HttpClientInterface httpClient, HandlerInterface responseHandler, Map<String, String> headers) {
97
        this(httpClient, responseHandler);
98
        this.headers = headers;
99
    }
100
101
    /**
102
     * Set Default Headers for {@link Request}
103
     *
104
     * @return
105
     */
106
    private static Map<String, String> getDefaultHeaders() {
107
        Map<String, String> headers = new HashMap<>();
108
        headers.put("Accept", "application/json");
109
        return headers;
110
    }
111
112
    /**
113
     * Return {@link HttpClientInterface}
114
     *
115
     * @return HttpClientInterface
116
     */
117
    public HttpClientInterface getHttpClient() {
118
        return httpClient;
119
    }
120
121
122
    /**
123
     * Set Base Client HttpClient
124
     *
125
     * @param httpClient
126
     * @return
127
     */
128
    public BaseClient setHttpClient(HttpClientInterface httpClient) {
129
        this.httpClient = httpClient;
130
        return this;
131
    }
132
133
    /**
134
     * Return Base Client ResponseHandler
135
     *
136
     * @return
137
     */
138
    public HandlerInterface getResponseHandler() {
139
        return responseHandler;
140
    }
141
142
    /**
143
     * Set Base Client ResponseHandler
144
     *
145
     * @param responseHandler
146
     * @return
147
     */
148
    public BaseClient setResponseHandler(HandlerInterface responseHandler) {
149
        this.responseHandler = responseHandler;
150
        return this;
151
    }
152
153
    /**
154
     * Return Base Client Headers
155
     *
156
     * @return
157
     */
158
    public Map<String, String> getHeaders() {
159
        return this.headers;
160
    }
161
162
    /**
163
     * Set Base Client Headers
164
     *
165
     * @param headers
166
     * @return
167
     */
168
    public BaseClient setHeaders(Map<String, String> headers) {
169
        this.headers = headers;
170
        return this;
171
    }
172
173
    /**
174
     * Add {@link BaseClient} Headers
175
     *
176
     * @param key
177
     * @param value
178
     * @return
179
     */
180
    public BaseClient addHeader(String key, String value) {
181
        this.headers.put(key, value);
182
        return this;
183
    }
184
185
    /**
186
     * Return Base Client ApiUrl
187
     *
188
     * @return
189
     */
190
    public String getApiUrl() {
191
        if (this.apiUrl != null) {
192
            return apiUrl;
193
        }
194
        return BaseClient.DEFAULT_API_URL;
195
    }
196
197
    /**
198
     * Set Base Client ApiUrl
199
     *
200
     * @param apiUrl
201
     * @return
202
     */
203
    public BaseClient setApiUrl(String apiUrl) {
204
        this.apiUrl = apiUrl;
205
        return this;
206
    }
207
208
    /**
209
     * Return Base Client ClientId
210
     *
211
     * @return
212
     */
213
    public String getClientId() {
214
        return clientId;
215
    }
216
217
    /**
218
     * Set Base Client ClientId
219
     *
220
     * @param clientId
221
     * @return
222
     */
223
    public BaseClient setClientId(String clientId) {
224
        this.clientId = clientId;
225
        return this;
226
    }
227
228
    /**
229
     * Return Base Client ClientSecret
230
     *
231
     * @return
232
     */
233
    public String getClientSecret() {
234
        return clientSecret;
235
    }
236
237
    /**
238
     * Set Base Client ClientSecret
239
     *
240
     * @param clientSecret
241
     * @return
242
     */
243
    public BaseClient setClientSecret(String clientSecret) {
244
        this.clientSecret = clientSecret;
245
        return this;
246
    }
247
248
    /**
249
     * Return Base Client AccessToken
250
     *
251
     * @return
252
     */
253
    public AccessToken getAccessToken() {
254
        return this.accessToken;
255
    }
256
257
    /**
258
     * Set Base Client AccessToken
259
     *
260
     * @param accessToken
261
     * @return
262
     */
263
    public BaseClient setAccessToken(AccessToken accessToken) {
264
        this.accessToken = accessToken;
265
        return this;
266
    }
267
268
    /**
269
     * Send Request with {@link Request} and {@link Response}
270
     *
271
     * @param request
272
     * @param response
273
     * @return Response
274
     * @throws BaseHttpException Exception
275
     */
276
    public Response sendRequest(Request request, Response response) throws BaseHttpException {
277
        request.setUrl(this.buildUrl(request.getEndpoint()));
278
        request.setHeaders(this.buildHeaders(request));
279
280
        return this.getResponseHandler().handle(this.getHttpClient().send(request, response));
281
    }
282
283
    /**
284
     * Return BaseClient URL with endPoint and Default URL
285
     *
286
     * @param endpoint
287
     * @return URL
288
     */
289
    private String buildUrl(String endpoint) {
290
        return this.getApiUrl().concat(endpoint);
291
    }
292
293
    /**
294
     * Generate {@link Request} Headers
295
     *
296
     * @param request
297
     * @return
298
     */
299
    private Map<String, String> buildHeaders(Request request) {
300
        // Get All the Headers
301
        Map<String, String> allHeaders = this.getHeaders();
302
303
        // Add the Auth Headers
304
        allHeaders.putAll(this.getAuthHeaders(request));
305
        // Add the Custom Headers
306
        allHeaders.putAll(request.getHeaders());
307
308
        return allHeaders;
309
    }
310
311
    /**
312
     * Return Authentication Headers
313
     *
314
     * @param request
315
     * @return
316
     */
317
    private Map<String, String> getAuthHeaders(Request request) {
318
        Map<String, String> headers = new HashMap<>();
0 ignored issues
show
Comprehensibility introduced by
The variable headersshadows a field with the same name declared in line 45. Consider renaming it.
Loading history...
319
        AccessToken accessToken = this.getAccessToken();
0 ignored issues
show
Comprehensibility introduced by
The variable accessTokenshadows a field with the same name declared in line 40. Consider renaming it.
Loading history...
320
321
        if (request.getAccessToken() != null) {
322
            accessToken = request.getAccessToken();
323
        }
324
325
        if (accessToken != null) {
326
            headers.put("Authorization", "Bearer ".concat(accessToken.getAccessToken()));
327
        }
328
329
        if (this.getClientId() != null) {
330
            headers.put("X-CLIENT-ID", this.getClientId());
331
        }
332
333
        if (this.getClientSecret() != null) {
334
            headers.put("X-CLIENT-SECRET", this.getClientSecret());
335
        }
336
337
338
        return headers;
339
    }
340
}
341