-
Notifications
You must be signed in to change notification settings - Fork 21
fix(java): use Map for query parameters #484
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
a0b6c74
27f5aa7
1657a3a
bd9eb13
3f2680c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -190,111 +190,6 @@ public class ApiClient { | |
} | ||
} | ||
|
||
/** | ||
* Formats the specified query parameter to a list containing a single {@code Pair} object. | ||
* | ||
* Note that {@code value} must not be a collection. | ||
* | ||
* @param name The name of the parameter. | ||
* @param value The value of the parameter. | ||
* @return A list containing a single {@code Pair} object. | ||
*/ | ||
public List<Pair> parameterToPair(String name, Object value) { | ||
List<Pair> params = new ArrayList<Pair>(); | ||
|
||
// preconditions | ||
if (name == null || name.isEmpty() || value == null) { | ||
return params; | ||
} | ||
|
||
params.add(new Pair(name, parameterToString(value))); | ||
return params; | ||
} | ||
|
||
/** | ||
* Formats the specified collection query parameters to a list of {@code Pair} objects. | ||
* | ||
* Note that the values of each of the returned Pair objects are percent-encoded. | ||
* | ||
* @param collectionFormat The collection format of the parameter. | ||
* @param name The name of the parameter. | ||
* @param value The value of the parameter. | ||
* @return A list of {@code Pair} objects. | ||
*/ | ||
public List<Pair> parameterToPairs(String collectionFormat, String name, Collection value) { | ||
List<Pair> params = new ArrayList<Pair>(); | ||
|
||
// preconditions | ||
if (name == null || name.isEmpty() || value == null || value.isEmpty()) { | ||
return params; | ||
} | ||
|
||
// create the params based on the collection format | ||
if ("multi".equals(collectionFormat)) { | ||
for (Object item : value) { | ||
params.add(new Pair(name, escapeString(parameterToString(item)))); | ||
} | ||
return params; | ||
} | ||
|
||
// collectionFormat is assumed to be "csv" by default | ||
String delimiter = ","; | ||
|
||
// escape all delimiters except commas, which are URI reserved | ||
// characters | ||
if ("ssv".equals(collectionFormat)) { | ||
delimiter = escapeString(" "); | ||
} else if ("tsv".equals(collectionFormat)) { | ||
delimiter = escapeString("\t"); | ||
} else if ("pipes".equals(collectionFormat)) { | ||
delimiter = escapeString("|"); | ||
} | ||
|
||
StringBuilder sb = new StringBuilder(); | ||
for (Object item : value) { | ||
sb.append(delimiter); | ||
sb.append(escapeString(parameterToString(item))); | ||
} | ||
|
||
params.add(new Pair(name, sb.substring(delimiter.length()))); | ||
|
||
return params; | ||
} | ||
|
||
/** | ||
* Formats the specified collection path parameter to a string value. | ||
* | ||
* @param collectionFormat The collection format of the parameter. | ||
* @param value The value of the parameter. | ||
* @return String representation of the parameter | ||
*/ | ||
public String collectionPathParameterToString(String collectionFormat, Collection value) { | ||
// create the value based on the collection format | ||
if ("multi".equals(collectionFormat)) { | ||
// not valid for path params | ||
return parameterToString(value); | ||
} | ||
|
||
// collectionFormat is assumed to be "csv" by default | ||
String delimiter = ","; | ||
|
||
if ("ssv".equals(collectionFormat)) { | ||
delimiter = " "; | ||
} else if ("tsv".equals(collectionFormat)) { | ||
delimiter = "\t"; | ||
} else if ("pipes".equals(collectionFormat)) { | ||
delimiter = "|"; | ||
} | ||
|
||
StringBuilder sb = new StringBuilder() ; | ||
for (Object item : value) { | ||
sb.append(delimiter); | ||
sb.append(parameterToString(item)); | ||
} | ||
|
||
return sb.substring(delimiter.length()); | ||
} | ||
|
||
/** | ||
* Check if the given MIME is a JSON MIME. | ||
* JSON MIME examples: | ||
|
@@ -534,7 +429,7 @@ public class ApiClient { | |
* @return The HTTP call | ||
* @throws AlgoliaRuntimeException If fail to serialize the request body object | ||
*/ | ||
public Call buildCall(String path, String method, List<Pair> queryParams, Object body, Map<String, String> headerParams, ApiCallback callback) throws AlgoliaRuntimeException { | ||
public Call buildCall(String path, String method, Map<String, String> queryParams, Object body, Map<String, String> headerParams, ApiCallback callback) throws AlgoliaRuntimeException { | ||
Request request = buildRequest(path, method, queryParams, body, headerParams, callback); | ||
|
||
return requester.newCall(request); | ||
|
@@ -552,19 +447,17 @@ public class ApiClient { | |
* @return The HTTP request | ||
* @throws AlgoliaRuntimeException If fail to serialize the request body object | ||
*/ | ||
public Request buildRequest(String path, String method, List<Pair> queryParams, Object body, Map<String, String> headerParams, ApiCallback callback) throws AlgoliaRuntimeException { | ||
public Request buildRequest(String path, String method, Map<String, String> queryParams, Object body, Map<String, String> headerParams, ApiCallback callback) throws AlgoliaRuntimeException { | ||
headerParams.put("X-Algolia-Application-Id", this.appId); | ||
headerParams.put("X-Algolia-API-Key", this.apiKey); | ||
headerParams.put("Accept", "application/json"); | ||
headerParams.put("Content-Type", "application/json"); | ||
|
||
final String url = buildUrl(path, queryParams); | ||
final Request.Builder reqBuilder = new Request.Builder().url(url); | ||
processHeaderParams(headerParams, reqBuilder); | ||
|
||
String contentType = (String) headerParams.get("Content-Type"); | ||
// ensuring a default content type | ||
if (contentType == null) { | ||
contentType = "application/json"; | ||
} | ||
Comment on lines
-564
to
-567
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since we set it right above, it can't be null |
||
|
||
RequestBody reqBody; | ||
if (!HttpMethod.permitsRequestBody(method)) { | ||
|
@@ -604,7 +497,7 @@ public class ApiClient { | |
* @param queryParams The query parameters | ||
* @return The full URL | ||
*/ | ||
public String buildUrl(String path, List<Pair> queryParams) { | ||
public String buildUrl(String path, Map<String, String> queryParams) { | ||
final StringBuilder url = new StringBuilder(); | ||
|
||
//The real host will be assigned by the retry strategy | ||
|
@@ -613,7 +506,7 @@ public class ApiClient { | |
if (queryParams != null && !queryParams.isEmpty()) { | ||
// support (constant) query string in `path`, e.g. "/posts?draft=1" | ||
String prefix = path.contains("?") ? "&" : "?"; | ||
for (Pair param : queryParams) { | ||
for (Entry<String, String> param : queryParams.entrySet()) { | ||
if (param.getValue() != null) { | ||
if (prefix != null) { | ||
url.append(prefix); | ||
|
@@ -622,7 +515,7 @@ public class ApiClient { | |
url.append("&"); | ||
} | ||
String value = parameterToString(param.getValue()); | ||
url.append(escapeString(param.getName())).append("=").append(escapeString(value)); | ||
url.append(escapeString(param.getKey())).append("=").append(escapeString(value)); | ||
} | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -149,6 +149,7 @@ export function create{{capitalizedApiName}}(options: CreateClientOptions{{#hasR | |
{{/queryParams.0}} | ||
{{/bodyParams.0}} | ||
{{/allParams.0}} | ||
* @param requestOptions - The requestOptions to send along with the query, they will be merged with the transporter requestOptions. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was missing in the JS client |
||
*/ | ||
{{nickname}}( | ||
{{#allParams.0}} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Those were in every methods so I've moved them here