Skip to content

Commit ee9c021

Browse files
chore: generated code for commit c5ac185. [skip ci]
Co-authored-by: Clément Vannicatte <[email protected]>
1 parent c5ac185 commit ee9c021

File tree

10 files changed

+6068
-590
lines changed

10 files changed

+6068
-590
lines changed

clients/algoliasearch-client-java-2/algoliasearch-core/src/main/java/com/algolia/ApiClient.java

Lines changed: 77 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.algolia.exceptions.*;
44
import com.algolia.utils.JSON;
5+
import com.algolia.utils.RequestOptions;
56
import com.algolia.utils.Requester;
67
import com.algolia.utils.UserAgent;
78
import java.io.IOException;
@@ -22,7 +23,7 @@ public class ApiClient {
2223
private boolean debugging = false;
2324
private Map<String, String> defaultHeaderMap = new HashMap<String, String>();
2425

25-
private String appId, apiKey;
26+
private String contentType;
2627

2728
private DateFormat dateFormat;
2829

@@ -38,6 +39,8 @@ public ApiClient(
3839
String clientName,
3940
UserAgent.Segment[] segments
4041
) {
42+
this.contentType = "application/json";
43+
4144
UserAgent ua = new UserAgent("0.0.1");
4245
ua.addSegment(new UserAgent.Segment(clientName, "0.0.1"));
4346
if (segments != null) {
@@ -47,8 +50,11 @@ public ApiClient(
4750
}
4851
setUserAgent(ua.toString());
4952

50-
this.appId = appId;
51-
this.apiKey = apiKey;
53+
defaultHeaderMap.put("X-Algolia-Application-Id", appId);
54+
defaultHeaderMap.put("X-Algolia-API-Key", apiKey);
55+
defaultHeaderMap.put("Accept", this.contentType);
56+
defaultHeaderMap.put("Content-Type", this.contentType);
57+
5258
this.requester = requester;
5359
}
5460

@@ -190,19 +196,6 @@ public String parameterToString(Object param) {
190196
}
191197
}
192198

193-
/**
194-
* Check if the given MIME is a JSON MIME. JSON MIME examples: application/json application/json;
195-
* charset=UTF8 APPLICATION/JSON application/vnd.company+json "* / *" is also default to JSON
196-
*
197-
* @param mime MIME (Multipurpose Internet Mail Extensions)
198-
* @return True if the given MIME is JSON, false otherwise.
199-
*/
200-
public static boolean isJsonMime(String mime) {
201-
String jsonMime =
202-
"(?i)^(application/json|[^;/ \t]+/[^;/ \t]+[+]json)[ \t]*(;.*)?$";
203-
return mime != null && (mime.matches(jsonMime) || mime.equals("*/*"));
204-
}
205-
206199
/**
207200
* Escape the given string to be used as URL query value.
208201
*
@@ -222,28 +215,19 @@ public String escapeString(String str) {
222215
* request Content-Type.
223216
*
224217
* @param obj The Java object
225-
* @param contentType The request Content-Type
226218
* @return The serialized request body
227219
* @throws AlgoliaRuntimeException If fail to serialize the given object
228220
*/
229-
public RequestBody serialize(Object obj, String contentType)
230-
throws AlgoliaRuntimeException {
231-
if (obj instanceof byte[]) {
232-
// Binary (byte array) body parameter support.
233-
return RequestBody.create((byte[]) obj, MediaType.parse(contentType));
234-
} else if (isJsonMime(contentType)) {
235-
String content;
236-
if (obj != null) {
237-
content = JSON.serialize(obj);
238-
} else {
239-
content = null;
240-
}
241-
return RequestBody.create(content, MediaType.parse(contentType));
221+
public RequestBody serialize(Object obj) throws AlgoliaRuntimeException {
222+
String content;
223+
224+
if (obj != null) {
225+
content = JSON.serialize(obj);
242226
} else {
243-
throw new AlgoliaRuntimeException(
244-
"Content type \"" + contentType + "\" is not supported"
245-
);
227+
content = null;
246228
}
229+
230+
return RequestBody.create(content, MediaType.parse(this.contentType));
247231
}
248232

249233
/**
@@ -291,6 +275,8 @@ public void onResponse(Call call, Response response)
291275
* @param queryParams The query parameters
292276
* @param body The request body object
293277
* @param headerParams The header parameters
278+
* @param requestOptions The requestOptions to send along with the query, they will be merged with
279+
* the transporter requestOptions.
294280
* @return The HTTP call
295281
* @throws AlgoliaRuntimeException If fail to serialize the request body object
296282
*/
@@ -299,14 +285,16 @@ public Call buildCall(
299285
String method,
300286
Map<String, String> queryParams,
301287
Object body,
302-
Map<String, String> headerParams
288+
Map<String, String> headerParams,
289+
RequestOptions requestOptions
303290
) throws AlgoliaRuntimeException {
304291
Request request = buildRequest(
305292
path,
306293
method,
307294
queryParams,
308295
body,
309-
headerParams
296+
headerParams,
297+
requestOptions
310298
);
311299

312300
return requester.newCall(request);
@@ -321,6 +309,8 @@ public Call buildCall(
321309
* @param queryParams The query parameters
322310
* @param body The request body object
323311
* @param headerParams The header parameters
312+
* @param requestOptions The requestOptions to send along with the query, they will be merged with
313+
* the transporter requestOptions.
324314
* @return The HTTP request
325315
* @throws AlgoliaRuntimeException If fail to serialize the request body object
326316
*/
@@ -329,20 +319,21 @@ public Request buildRequest(
329319
String method,
330320
Map<String, String> queryParams,
331321
Object body,
332-
Map<String, String> headerParams
322+
Map<String, String> headerParams,
323+
RequestOptions requestOptions
333324
) throws AlgoliaRuntimeException {
334-
headerParams.put("X-Algolia-Application-Id", this.appId);
335-
headerParams.put("X-Algolia-API-Key", this.apiKey);
336-
headerParams.put("Accept", "application/json");
337-
headerParams.put("Content-Type", "application/json");
338-
339-
String contentType = "application/json";
340-
headerParams.put("Accept", contentType);
341-
headerParams.put("Content-Type", contentType);
342-
343-
final String url = buildUrl(path, queryParams);
325+
boolean hasRequestOptions = requestOptions != null;
326+
final String url = buildUrl(
327+
path,
328+
queryParams,
329+
hasRequestOptions ? requestOptions.getExtraQueryParams() : null
330+
);
344331
final Request.Builder reqBuilder = new Request.Builder().url(url);
345-
processHeaderParams(headerParams, reqBuilder);
332+
processHeaderParams(
333+
headerParams,
334+
hasRequestOptions ? requestOptions.getExtraHeaders() : null,
335+
reqBuilder
336+
);
346337

347338
RequestBody reqBody;
348339
if (!HttpMethod.permitsRequestBody(method)) {
@@ -353,10 +344,10 @@ public Request buildRequest(
353344
reqBody = null;
354345
} else {
355346
// use an empty request body (for POST, PUT and PATCH)
356-
reqBody = RequestBody.create("", MediaType.parse(contentType));
347+
reqBody = RequestBody.create("", MediaType.parse(this.contentType));
357348
}
358349
} else {
359-
reqBody = serialize(body, contentType);
350+
reqBody = serialize(body);
360351
}
361352

362353
return reqBuilder.method(method, reqBody).build();
@@ -367,14 +358,38 @@ public Request buildRequest(
367358
*
368359
* @param path The sub path
369360
* @param queryParams The query parameters
361+
* @param extraQueryParams The query parameters, coming from the requestOptions
370362
* @return The full URL
371363
*/
372-
public String buildUrl(String path, Map<String, String> queryParams) {
373-
final StringBuilder url = new StringBuilder();
364+
public String buildUrl(
365+
String path,
366+
Map<String, String> queryParams,
367+
Map<String, String> extraQueryParams
368+
) {
369+
StringBuilder url = new StringBuilder();
374370

375371
// The real host will be assigned by the retry strategy
376372
url.append("http://temp.path").append(path);
377373

374+
url = parseQueryParameters(path, url, queryParams);
375+
url = parseQueryParameters(path, url, extraQueryParams);
376+
377+
return url.toString();
378+
}
379+
380+
/**
381+
* Parses the given map of Query Parameters to a given URL.
382+
*
383+
* @param path The sub path
384+
* @param url The url to add queryParams to
385+
* @param queryParams The query parameters
386+
* @return The URL
387+
*/
388+
public StringBuilder parseQueryParameters(
389+
String path,
390+
StringBuilder url,
391+
Map<String, String> queryParams
392+
) {
378393
if (queryParams != null && !queryParams.isEmpty()) {
379394
// support (constant) query string in `path`, e.g. "/posts?draft=1"
380395
String prefix = path.contains("?") ? "&" : "?";
@@ -395,17 +410,19 @@ public String buildUrl(String path, Map<String, String> queryParams) {
395410
}
396411
}
397412

398-
return url.toString();
413+
return url;
399414
}
400415

401416
/**
402417
* Set header parameters to the request builder, including default headers.
403418
*
404419
* @param headerParams Header parameters in the form of Map
420+
* @param extraHeaderParams Header parameters in the form of Map, coming from RequestOptions
405421
* @param reqBuilder Request.Builder
406422
*/
407423
public void processHeaderParams(
408424
Map<String, String> headerParams,
425+
Map<String, String> extraHeaderParams,
409426
Request.Builder reqBuilder
410427
) {
411428
for (Entry<String, String> param : headerParams.entrySet()) {
@@ -419,6 +436,14 @@ public void processHeaderParams(
419436
);
420437
}
421438
}
439+
if (extraHeaderParams != null) {
440+
for (Entry<String, String> header : extraHeaderParams.entrySet()) {
441+
reqBuilder.header(
442+
header.getKey(),
443+
parameterToString(header.getValue())
444+
);
445+
}
446+
}
422447
}
423448

424449
/**

0 commit comments

Comments
 (0)