-
Notifications
You must be signed in to change notification settings - Fork 21
fix(specs): allow POST
methods to send read
requests
#525
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 5 commits
5ad13a6
b98e6e0
7a8718c
76d9388
02cc889
1711080
f7f2a0c
95010e9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package com.algolia.utils; | ||
|
||
public class UseReadTransporter {} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,7 @@ | ||
package {{invokerPackage}}; | ||
|
||
import com.algolia.utils.Requester; | ||
import com.algolia.exceptions.*; | ||
import com.algolia.utils.AlgoliaAgent; | ||
import com.algolia.utils.JSON; | ||
import com.algolia.utils.RequestOptions; | ||
import com.algolia.utils.*; | ||
|
||
import okhttp3.*; | ||
import okhttp3.internal.http.HttpMethod; | ||
|
@@ -270,11 +267,12 @@ public class ApiClient { | |
* @param body The request body object | ||
* @param headerParams The header parameters | ||
* @param requestOptions The requestOptions to send along with the query, they will be merged with the transporter requestOptions. | ||
* @param useReadTransporter Some POST methods in the Algolia REST API uses the `read` transporter. This information is defined at the spec level. | ||
* @return The HTTP call | ||
* @throws AlgoliaRuntimeException If fail to serialize the request body object | ||
*/ | ||
public Call buildCall(String path, String method, Map<String, Object> queryParameters, Object body, Map<String, String> headerParams, RequestOptions requestOptions) throws AlgoliaRuntimeException { | ||
Request request = buildRequest(path, method, queryParameters, body, headerParams, requestOptions); | ||
public Call buildCall(String path, String method, Map<String, Object> queryParameters, Object body, Map<String, String> headerParams, RequestOptions requestOptions, Boolean useReadTransporter) throws AlgoliaRuntimeException { | ||
Request request = buildRequest(path, method, queryParameters, body, headerParams, requestOptions, useReadTransporter); | ||
|
||
return requester.newCall(request); | ||
} | ||
|
@@ -288,39 +286,52 @@ public class ApiClient { | |
* @param body The request body object | ||
* @param headerParams The header parameters | ||
* @param requestOptions The requestOptions to send along with the query, they will be merged with the transporter requestOptions. | ||
* @param useReadTransporter Some POST methods in the Algolia REST API uses the `read` transporter. This information is defined at the spec level. | ||
* @return The HTTP request | ||
* @throws AlgoliaRuntimeException If fail to serialize the request body object | ||
*/ | ||
public Request buildRequest(String path, String method, Map<String, Object> queryParameters, Object body, Map<String, String> headerParams, RequestOptions requestOptions) throws AlgoliaRuntimeException { | ||
boolean hasRequestOptions = requestOptions != null; | ||
final String url = buildUrl( | ||
path, | ||
queryParameters, | ||
hasRequestOptions ? requestOptions.getExtraQueryParameters() : null | ||
); | ||
final Request.Builder reqBuilder = new Request.Builder().url(url); | ||
processHeaderParams( | ||
headerParams, | ||
hasRequestOptions ? requestOptions.getExtraHeaders() : null, | ||
reqBuilder | ||
); | ||
public Request buildRequest( | ||
String path, | ||
String method, | ||
Map<String, Object> queryParameters, | ||
Object body, | ||
Map<String, String> headerParams, | ||
RequestOptions requestOptions, | ||
Boolean useReadTransporter | ||
) throws AlgoliaRuntimeException { | ||
boolean hasRequestOptions = requestOptions != null; | ||
final String url = buildUrl( | ||
path, | ||
queryParameters, | ||
hasRequestOptions ? requestOptions.getExtraQueryParameters() : null | ||
); | ||
final Request.Builder reqBuilder = new Request.Builder().url(url); | ||
processHeaderParams( | ||
headerParams, | ||
hasRequestOptions ? requestOptions.getExtraHeaders() : null, | ||
reqBuilder | ||
); | ||
|
||
RequestBody reqBody; | ||
if (!HttpMethod.permitsRequestBody(method)) { | ||
reqBody = null; | ||
} else if (body == null) { | ||
if ("DELETE".equals(method)) { | ||
// allow calling DELETE without sending a request body | ||
reqBody = null; | ||
} else { | ||
// use an empty request body (for POST, PUT and PATCH) | ||
reqBody = RequestBody.create("", MediaType.parse(this.contentType)); | ||
} | ||
} else { | ||
reqBody = serialize(body); | ||
} | ||
RequestBody reqBody; | ||
if (!HttpMethod.permitsRequestBody(method)) { | ||
reqBody = null; | ||
} else if (body == null) { | ||
if ("DELETE".equals(method)) { | ||
// allow calling DELETE without sending a request body | ||
reqBody = null; | ||
} else { | ||
// use an empty request body (for POST, PUT and PATCH) | ||
reqBody = RequestBody.create("", MediaType.parse(this.contentType)); | ||
} | ||
} else { | ||
reqBody = serialize(body); | ||
} | ||
|
||
return reqBuilder.method(method, reqBody).build(); | ||
if (useReadTransporter) { | ||
reqBuilder.tag(new UseReadTransporter()); | ||
} | ||
Comment on lines
+330
to
+332
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 is the only difference, the rest is indenting sorry |
||
|
||
return reqBuilder.method(method, reqBody).build(); | ||
} | ||
|
||
/** | ||
|
@@ -401,18 +412,4 @@ public class ApiClient { | |
} | ||
} | ||
} | ||
|
||
/** | ||
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. It was not used, could you confirm? 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. I concur 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. Ok my lord |
||
* Build a form-encoding request body with the given form parameters. | ||
* | ||
* @param formParams Form parameters in the form of Map | ||
* @return RequestBody | ||
*/ | ||
public RequestBody buildRequestBodyFormEncoding(Map<String, Object> formParams) { | ||
okhttp3.FormBody.Builder formBuilder = new okhttp3.FormBody.Builder(); | ||
for (Entry<String, Object> param : formParams.entrySet()) { | ||
formBuilder.add(param.getKey(), parameterToString(param.getValue())); | ||
} | ||
return formBuilder.build(); | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.