forked from loafle/openapi-generator-original
Java API invocation flexibility (#18078)
* add direct invocation methods for java (httpclient) * add direct invocation methods for java (resttemplate) * handle methods only if endpoints exist for api client * preserve previous newline to minimize changes * update httpclient/resttemplate samples * add common methods in base class * regenerate samples with base class
This commit is contained in:
@@ -16,6 +16,7 @@ pom.xml
|
||||
settings.gradle
|
||||
src/main/AndroidManifest.xml
|
||||
src/main/java/org/openapitools/client/ApiClient.java
|
||||
src/main/java/org/openapitools/client/BaseApi.java
|
||||
src/main/java/org/openapitools/client/JavaTimeFormatter.java
|
||||
src/main/java/org/openapitools/client/RFC3339DateFormat.java
|
||||
src/main/java/org/openapitools/client/ServerConfiguration.java
|
||||
|
||||
@@ -0,0 +1,74 @@
|
||||
package org.openapitools.client;
|
||||
|
||||
import org.springframework.web.client.RestClientException;
|
||||
import org.springframework.core.ParameterizedTypeReference;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
|
||||
@javax.annotation.Generated(value = "org.openapitools.codegen.languages.JavaClientCodegen", comments = "Generator version: 7.5.0-SNAPSHOT")
|
||||
public abstract class BaseApi {
|
||||
|
||||
protected ApiClient apiClient;
|
||||
|
||||
public BaseApi() {
|
||||
this(new ApiClient());
|
||||
}
|
||||
|
||||
public BaseApi(ApiClient apiClient) {
|
||||
this.apiClient = apiClient;
|
||||
}
|
||||
|
||||
public ApiClient getApiClient() {
|
||||
return apiClient;
|
||||
}
|
||||
|
||||
public void setApiClient(ApiClient apiClient) {
|
||||
this.apiClient = apiClient;
|
||||
}
|
||||
|
||||
/**
|
||||
* Directly invoke the API for the given URL. Useful if the API returns direct links/URLs for subsequent requests.
|
||||
* @param url The URL for the request, either full URL or only the path.
|
||||
* @param method The HTTP method for the request.
|
||||
* @return ResponseEntity<Void>
|
||||
* @throws RestClientException if an error occurs while attempting to invoke the API
|
||||
*/
|
||||
public ResponseEntity<Void> invokeAPI(String url, HttpMethod method) throws RestClientException {
|
||||
return invokeAPI(url, method, null, new ParameterizedTypeReference<Void>() {});
|
||||
}
|
||||
|
||||
/**
|
||||
* Directly invoke the API for the given URL. Useful if the API returns direct links/URLs for subsequent requests.
|
||||
* @param url The URL for the request, either full URL or only the path.
|
||||
* @param method The HTTP method for the request.
|
||||
* @param request The request object.
|
||||
* @return ResponseEntity<Void>
|
||||
* @throws RestClientException if an error occurs while attempting to invoke the API
|
||||
*/
|
||||
public ResponseEntity<Void> invokeAPI(String url, HttpMethod method, Object request) throws RestClientException {
|
||||
return invokeAPI(url, method, request, new ParameterizedTypeReference<Void>() {});
|
||||
}
|
||||
|
||||
/**
|
||||
* Directly invoke the API for the given URL. Useful if the API returns direct links/URLs for subsequent requests.
|
||||
* @param url The URL for the request, either full URL or only the path.
|
||||
* @param method The HTTP method for the request.
|
||||
* @param returnType The return type.
|
||||
* @return ResponseEntity in the specified type.
|
||||
* @throws RestClientException if an error occurs while attempting to invoke the API
|
||||
*/
|
||||
public <T> ResponseEntity<T> invokeAPI(String url, HttpMethod method, ParameterizedTypeReference<T> returnType) throws RestClientException {
|
||||
return invokeAPI(url, method, null, returnType);
|
||||
}
|
||||
|
||||
/**
|
||||
* Directly invoke the API for the given URL. Useful if the API returns direct links/URLs for subsequent requests.
|
||||
* @param url The URL for the request, either full URL or only the path.
|
||||
* @param method The HTTP method for the request.
|
||||
* @param request The request object.
|
||||
* @param returnType The return type.
|
||||
* @return ResponseEntity in the specified type.
|
||||
* @throws RestClientException if an error occurs while attempting to invoke the API
|
||||
*/
|
||||
public abstract <T> ResponseEntity<T> invokeAPI(String url, HttpMethod method, Object request, ParameterizedTypeReference<T> returnType) throws RestClientException;
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
package org.openapitools.client.api;
|
||||
|
||||
import org.openapitools.client.ApiClient;
|
||||
import org.openapitools.client.BaseApi;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
@@ -26,23 +27,14 @@ import org.springframework.http.MediaType;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
|
||||
@javax.annotation.Generated(value = "org.openapitools.codegen.languages.JavaClientCodegen", comments = "Generator version: 7.5.0-SNAPSHOT")
|
||||
public class ResourceApi {
|
||||
private ApiClient apiClient;
|
||||
public class ResourceApi extends BaseApi {
|
||||
|
||||
public ResourceApi() {
|
||||
this(new ApiClient());
|
||||
super(new ApiClient());
|
||||
}
|
||||
|
||||
public ResourceApi(ApiClient apiClient) {
|
||||
this.apiClient = apiClient;
|
||||
}
|
||||
|
||||
public ApiClient getApiClient() {
|
||||
return apiClient;
|
||||
}
|
||||
|
||||
public void setApiClient(ApiClient apiClient) {
|
||||
this.apiClient = apiClient;
|
||||
super(apiClient);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -84,4 +76,27 @@ public class ResourceApi {
|
||||
ParameterizedTypeReference<org.springframework.core.io.Resource> localReturnType = new ParameterizedTypeReference<org.springframework.core.io.Resource>() {};
|
||||
return apiClient.invokeAPI("/resource", HttpMethod.GET, Collections.<String, Object>emptyMap(), localVarQueryParams, localVarPostBody, localVarHeaderParams, localVarCookieParams, localVarFormParams, localVarAccept, localVarContentType, localVarAuthNames, localReturnType);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> ResponseEntity<T> invokeAPI(String url, HttpMethod method, Object request, ParameterizedTypeReference<T> returnType) throws RestClientException {
|
||||
String localVarPath = url.replace(apiClient.getBasePath(), "");
|
||||
Object localVarPostBody = request;
|
||||
|
||||
final Map<String, Object> uriVariables = new HashMap<String, Object>();
|
||||
final MultiValueMap<String, String> localVarQueryParams = new LinkedMultiValueMap<String, String>();
|
||||
final HttpHeaders localVarHeaderParams = new HttpHeaders();
|
||||
final MultiValueMap<String, String> localVarCookieParams = new LinkedMultiValueMap<String, String>();
|
||||
final MultiValueMap<String, Object> localVarFormParams = new LinkedMultiValueMap<String, Object>();
|
||||
|
||||
final String[] localVarAccepts = {
|
||||
"application/octet-stream"
|
||||
};
|
||||
final List<MediaType> localVarAccept = apiClient.selectHeaderAccept(localVarAccepts);
|
||||
final String[] localVarContentTypes = { };
|
||||
final MediaType localVarContentType = apiClient.selectHeaderContentType(localVarContentTypes);
|
||||
|
||||
String[] localVarAuthNames = new String[] { };
|
||||
|
||||
return apiClient.invokeAPI(localVarPath, method, uriVariables, localVarQueryParams, localVarPostBody, localVarHeaderParams, localVarCookieParams, localVarFormParams, localVarAccept, localVarContentType, localVarAuthNames, returnType);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user