Skip to content

Commit

Permalink
remove unused content type checking
Browse files Browse the repository at this point in the history
  • Loading branch information
shortcuts committed May 11, 2022
1 parent ad0ff62 commit fc4ce88
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 48 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.algolia.utils;

import com.algolia.ApiClient;
import com.algolia.exceptions.*;
import com.algolia.utils.retry.RetryStrategy;
import com.algolia.utils.retry.StatefulHost;
Expand Down Expand Up @@ -99,20 +98,8 @@ private <T> T deserialize(Response response, Type returnType)
if (contentType == null) {
contentType = "application/json";
}
if (ApiClient.isJsonMime(contentType)) {
return JSON.deserialize(respBody, returnType);
} else if (returnType.equals(String.class)) {
// Expecting string, return the raw response body.
return (T) respBody;
} else {
throw new AlgoliaApiException(
"Content type \"" +
contentType +
"\" is not supported for type: " +
returnType,
response.code()
);
}

return JSON.deserialize(respBody, returnType);
}

public void setDebugging(boolean debugging) {
Expand Down
40 changes: 7 additions & 33 deletions templates/java/libraries/okhttp-gson/ApiClient.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -194,22 +194,6 @@ public class ApiClient {
}
}
/**
* Check if the given MIME is a JSON MIME.
* JSON MIME examples:
* application/json
* application/json; charset=UTF8
* APPLICATION/JSON
* application/vnd.company+json
* "* / *" is also default to JSON
* @param mime MIME (Multipurpose Internet Mail Extensions)
* @return True if the given MIME is JSON, false otherwise.
*/
public static boolean isJsonMime(String mime) {
String jsonMime = "(?i)^(application/json|[^;/ \t]+/[^;/ \t]+[+]json)[ \t]*(;.*)?$";
return mime != null && (mime.matches(jsonMime) || mime.equals("*/*"));
}
/**
* Escape the given string to be used as URL query value.
*
Expand All @@ -233,25 +217,15 @@ public class ApiClient {
* @throws AlgoliaRuntimeException If fail to serialize the given object
*/
public RequestBody serialize(Object obj) throws AlgoliaRuntimeException {
if (obj instanceof byte[]) {
// Binary (byte array) body parameter support.
return RequestBody.create(
(byte[]) obj,
MediaType.parse(this.contentType)
);
} else if (isJsonMime(this.contentType)) {
String content;
if (obj != null) {
content = JSON.serialize(obj);
} else {
content = null;
}
return RequestBody.create(content, MediaType.parse(this.contentType));
String content;
if (obj != null) {
content = JSON.serialize(obj);
} else {
throw new AlgoliaRuntimeException(
"Content type \"" + this.contentType + "\" is not supported"
);
content = null;
}
return RequestBody.create(content, MediaType.parse(this.contentType));
}
/**
Expand Down

0 comments on commit fc4ce88

Please sign in to comment.