Skip to content

Commit 477d5c4

Browse files
committed
Rebuild Java okhttp-gson client of Petstore sample
1 parent 460c8ca commit 477d5c4

File tree

1 file changed

+4
-24
lines changed
  • samples/client/petstore/java/okhttp-gson/src/main/java/io/swagger/client

1 file changed

+4
-24
lines changed

samples/client/petstore/java/okhttp-gson/src/main/java/io/swagger/client/ApiClient.java

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import com.squareup.okhttp.MultipartBuilder;
1111
import com.squareup.okhttp.MediaType;
1212
import com.squareup.okhttp.Headers;
13+
import com.squareup.okhttp.internal.http.HttpMethod;
1314

1415
import java.lang.reflect.Type;
1516

@@ -697,7 +698,7 @@ public <T> T handleResponse(Response response, Type returnType) throws ApiExcept
697698
* Build HTTP call with the given options.
698699
*
699700
* @param path The sub-path of the HTTP URL
700-
* @param method The request method, one of "GET", "HEAD", "POST", "PUT", "PATCH" and "DELETE"
701+
* @param method The request method, one of "GET", "HEAD", "OPTIONS", "POST", "PUT", "PATCH" and "DELETE"
701702
* @param queryParams The query parameters
702703
* @param body The request body object
703704
* @param headerParams The header parameters
@@ -717,7 +718,7 @@ public Call buildCall(String path, String method, List<Pair> queryParams, Object
717718
if (contentType == null) contentType = "application/json";
718719

719720
RequestBody reqBody;
720-
if ("GET".equals(method) || "HEAD".equals(method)) {
721+
if (!HttpMethod.permitsRequestBody(method)) {
721722
reqBody = null;
722723
} else if ("application/x-www-form-urlencoded".equals(contentType)) {
723724
reqBody = buildRequestBodyFormEncoding(formParams);
@@ -735,28 +736,7 @@ public Call buildCall(String path, String method, List<Pair> queryParams, Object
735736
reqBody = RequestBody.create(MediaType.parse(contentType), serialize(body, contentType));
736737
}
737738

738-
Request request;
739-
if ("GET".equals(method)) {
740-
request = reqBuilder.get().build();
741-
} else if ("HEAD".equals(method)) {
742-
request = reqBuilder.head().build();
743-
} else if ("POST".equals(method)) {
744-
request = reqBuilder.post(reqBody).build();
745-
} else if ("PUT".equals(method)) {
746-
request = reqBuilder.put(reqBody).build();
747-
} else if ("PATCH".equals(method)) {
748-
request = reqBuilder.patch(reqBody).build();
749-
} else if ("DELETE".equals(method)) {
750-
if (reqBody == null) {
751-
// calling DELETE without sending a request body
752-
request = reqBuilder.delete().build();
753-
} else {
754-
request = reqBuilder.delete(reqBody).build();
755-
}
756-
} else {
757-
throw new ApiException("unknown method type: " + method);
758-
}
759-
739+
Request request = reqBuilder.method(method, reqBody).build();
760740
return httpClient.newCall(request);
761741
}
762742

0 commit comments

Comments
 (0)