github.com/phrase/openapi@v0.0.0-20240514140800-49e8a106740e/openapi-generator/templates/java/libraries/okhttp-gson/api.mustache (about) 1 {{>licenseInfo}} 2 3 package {{package}}; 4 5 import {{invokerPackage}}.ApiCallback; 6 import {{invokerPackage}}.ApiClient; 7 import {{invokerPackage}}.ApiException; 8 import {{invokerPackage}}.ApiResponse; 9 import {{invokerPackage}}.Configuration; 10 import {{invokerPackage}}.Pair; 11 import {{invokerPackage}}.ProgressRequestBody; 12 import {{invokerPackage}}.ProgressResponseBody; 13 {{#performBeanValidation}} 14 import {{invokerPackage}}.BeanValidationException; 15 {{/performBeanValidation}} 16 17 import com.google.gson.reflect.TypeToken; 18 19 import java.io.IOException; 20 21 {{#useBeanValidation}} 22 import javax.validation.constraints.*; 23 {{/useBeanValidation}} 24 {{#performBeanValidation}} 25 import javax.validation.ConstraintViolation; 26 import javax.validation.Validation; 27 import javax.validation.ValidatorFactory; 28 import javax.validation.executable.ExecutableValidator; 29 import java.util.Set; 30 import java.lang.reflect.Method; 31 import java.lang.reflect.Type; 32 {{/performBeanValidation}} 33 34 {{#imports}}import {{import}}; 35 {{/imports}} 36 37 import java.lang.reflect.Type; 38 {{^fullJavaUtil}} 39 import java.util.ArrayList; 40 import java.util.HashMap; 41 import java.util.List; 42 import java.util.Map; 43 {{/fullJavaUtil}} 44 45 {{#operations}} 46 public class {{classname}} { 47 private ApiClient localVarApiClient; 48 49 public {{classname}}() { 50 this(Configuration.getDefaultApiClient()); 51 } 52 53 public {{classname}}(ApiClient apiClient) { 54 this.localVarApiClient = apiClient; 55 } 56 57 public ApiClient getApiClient() { 58 return localVarApiClient; 59 } 60 61 public void setApiClient(ApiClient apiClient) { 62 this.localVarApiClient = apiClient; 63 } 64 65 {{#operation}} 66 {{^vendorExtensions.x-group-parameters}}/** 67 * Build call for {{operationId}}{{#allParams}} 68 * @param {{paramName}} {{description}}{{#required}} (required){{/required}}{{^required}} (optional{{^isContainer}}{{#defaultValue}}, default to {{.}}{{/defaultValue}}{{/isContainer}}){{/required}}{{/allParams}} 69 * @param _callback Callback for upload/download progress 70 * @return Call to execute 71 * @throws ApiException If fail to serialize the request body object 72 {{#responses.0}} 73 * @http.response.details 74 <table summary="Response Details" border="1"> 75 <tr><td> Status Code </td><td> Description </td><td> Response Headers </td></tr> 76 {{#responses}} 77 <tr><td> {{code}} </td><td> {{message}} </td><td> {{#headers}} * {{baseName}} - {{description}} <br> {{/headers}}{{^headers.0}} - {{/headers.0}} </td></tr> 78 {{/responses}} 79 </table> 80 {{/responses.0}} 81 {{#isDeprecated}} 82 * @deprecated 83 {{/isDeprecated}} 84 {{#externalDocs}} 85 * {{description}} 86 * @see <a href="{{url}}">{{summary}} Documentation</a> 87 {{/externalDocs}} 88 */ 89 {{#isDeprecated}} 90 @Deprecated 91 {{/isDeprecated}} 92 public{{/vendorExtensions.x-group-parameters}}{{#vendorExtensions.x-group-parameters}}private{{/vendorExtensions.x-group-parameters}} okhttp3.Call {{operationId}}Call({{#allParams}}{{{dataType}}} {{paramName}}, {{/allParams}}final ApiCallback _callback) throws ApiException { 93 Object localVarPostBody = {{#bodyParam}}{{paramName}}{{/bodyParam}}{{^bodyParam}}null{{/bodyParam}}; 94 95 // create path and map variables 96 String localVarPath = "{{{path}}}"{{#pathParams}} 97 .replaceAll("\\{" + "{{baseName}}" + "\\}", localVarApiClient.escapeString({{#collectionFormat}}localVarApiClient.collectionPathParameterToString("{{{collectionFormat}}}", {{{paramName}}}){{/collectionFormat}}{{^collectionFormat}}{{{paramName}}}.toString(){{/collectionFormat}})){{/pathParams}}; 98 99 {{javaUtilPrefix}}List<Pair> localVarQueryParams = new {{javaUtilPrefix}}ArrayList<Pair>(); 100 {{javaUtilPrefix}}List<Pair> localVarCollectionQueryParams = new {{javaUtilPrefix}}ArrayList<Pair>(); 101 {{#queryParams}} 102 if ({{paramName}} != null) { 103 {{#isDeepObject}} 104 localVarQueryParams.addAll(localVarApiClient.mappedParameterToPairs("{{baseName}}", {{paramName}})); 105 {{/isDeepObject}} 106 {{^isDeepObject}} 107 {{#collectionFormat}}localVarCollectionQueryParams.addAll(localVarApiClient.parameterToPairs("{{{collectionFormat}}}", {{/collectionFormat}}{{^collectionFormat}}localVarQueryParams.addAll(localVarApiClient.parameterToPair({{/collectionFormat}}"{{baseName}}", {{paramName}})); 108 {{/isDeepObject}} 109 } 110 111 {{/queryParams}} 112 {{javaUtilPrefix}}Map<String, String> localVarHeaderParams = new {{javaUtilPrefix}}HashMap<String, String>(); 113 {{#headerParams}} 114 if ({{paramName}} != null) { 115 localVarHeaderParams.put("{{baseName}}", localVarApiClient.parameterToString({{paramName}})); 116 } 117 118 {{/headerParams}} 119 {{javaUtilPrefix}}Map<String, String> localVarCookieParams = new {{javaUtilPrefix}}HashMap<String, String>(); 120 {{#cookieParams}} 121 if ({{paramName}} != null) { 122 localVarCookieParams.put("{{baseName}}", localVarApiClient.parameterToString({{paramName}})); 123 } 124 125 {{/cookieParams}} 126 {{javaUtilPrefix}}Map<String, Object> localVarFormParams = new {{javaUtilPrefix}}HashMap<String, Object>(); 127 {{#formParams}} 128 if ({{paramName}} != null) { 129 localVarFormParams.put("{{baseName}}", {{paramName}}); 130 } 131 132 {{/formParams}} 133 final String[] localVarAccepts = { 134 {{#produces}}"{{{mediaType}}}"{{^-last}}, {{/-last}}{{/produces}} 135 }; 136 final String localVarAccept = localVarApiClient.selectHeaderAccept(localVarAccepts); 137 if (localVarAccept != null) { 138 localVarHeaderParams.put("Accept", localVarAccept); 139 } 140 141 final String[] localVarContentTypes = { 142 {{#consumes}}"{{{mediaType}}}"{{^-last}}, {{/-last}}{{/consumes}} 143 }; 144 final String localVarContentType = localVarApiClient.selectHeaderContentType(localVarContentTypes); 145 localVarHeaderParams.put("Content-Type", localVarContentType); 146 147 String[] localVarAuthNames = new String[] { {{#authMethods}}"{{name}}"{{^-last}}, {{/-last}}{{/authMethods}} }; 148 return localVarApiClient.buildCall(localVarPath, "{{httpMethod}}", localVarQueryParams, localVarCollectionQueryParams, localVarPostBody, localVarHeaderParams, localVarCookieParams, localVarFormParams, localVarAuthNames, _callback); 149 } 150 151 {{#isDeprecated}} 152 @Deprecated 153 {{/isDeprecated}} 154 @SuppressWarnings("rawtypes") 155 private okhttp3.Call {{operationId}}ValidateBeforeCall({{#allParams}}{{{dataType}}} {{paramName}}, {{/allParams}}final ApiCallback _callback) throws ApiException { 156 {{^performBeanValidation}} 157 {{#allParams}}{{#required}} 158 // verify the required parameter '{{paramName}}' is set 159 if ({{paramName}} == null) { 160 throw new ApiException("Missing the required parameter '{{paramName}}' when calling {{operationId}}(Async)"); 161 } 162 {{/required}}{{/allParams}} 163 164 okhttp3.Call localVarCall = {{operationId}}Call({{#allParams}}{{paramName}}, {{/allParams}}_callback); 165 return localVarCall; 166 167 {{/performBeanValidation}} 168 {{#performBeanValidation}} 169 try { 170 ValidatorFactory factory = Validation.buildDefaultValidatorFactory(); 171 ExecutableValidator executableValidator = factory.getValidator().forExecutables(); 172 173 Object[] parameterValues = { {{#allParams}}{{paramName}}{{^-last}}, {{/-last}}{{/allParams}} }; 174 Method method = this.getClass().getMethod("{{operationId}}WithHttpInfo"{{#allParams}}, {{#isArray}}java.util.List{{/isArray}}{{#isMap}}java.util.Map{{/isMap}}{{^isArray}}{{^isMap}}{{{dataType}}}{{/isMap}}{{/isArray}}.class{{/allParams}}); 175 Set<ConstraintViolation<{{classname}}>> violations = executableValidator.validateParameters(this, method, 176 parameterValues); 177 178 if (violations.size() == 0) { 179 okhttp3.Call localVarCall = {{operationId}}Call({{#allParams}}{{paramName}}, {{/allParams}}_callback); 180 return localVarCall; 181 182 } else { 183 throw new BeanValidationException((Set) violations); 184 } 185 } catch (NoSuchMethodException e) { 186 e.printStackTrace(); 187 throw new ApiException(e.getMessage()); 188 } catch (SecurityException e) { 189 e.printStackTrace(); 190 throw new ApiException(e.getMessage()); 191 } 192 193 {{/performBeanValidation}} 194 } 195 196 {{^vendorExtensions.x-group-parameters}} 197 /** 198 * {{summary}} 199 * {{notes}}{{#allParams}} 200 * @param {{paramName}} {{description}}{{#required}} (required){{/required}}{{^required}} (optional{{^isContainer}}{{#defaultValue}}, default to {{.}}{{/defaultValue}}{{/isContainer}}){{/required}}{{/allParams}}{{#returnType}} 201 * @return {{returnType}}{{/returnType}} 202 * @throws ApiException If fail to call the API, e.g. server error or cannot deserialize the response body 203 {{#responses.0}} 204 * @http.response.details 205 <table summary="Response Details" border="1"> 206 <tr><td> Status Code </td><td> Description </td><td> Response Headers </td></tr> 207 {{#responses}} 208 <tr><td> {{code}} </td><td> {{message}} </td><td> {{#headers}} * {{baseName}} - {{description}} <br> {{/headers}}{{^headers.0}} - {{/headers.0}} </td></tr> 209 {{/responses}} 210 </table> 211 {{/responses.0}} 212 {{#isDeprecated}} 213 * @deprecated 214 {{/isDeprecated}} 215 {{#externalDocs}} 216 * {{description}} 217 * @see <a href="{{url}}">{{summary}} Documentation</a> 218 {{/externalDocs}} 219 */ 220 {{#isDeprecated}} 221 @Deprecated 222 {{/isDeprecated}} 223 public {{#returnType}}{{{returnType}}} {{/returnType}}{{^returnType}}void {{/returnType}}{{operationId}}({{#allParams}}{{{dataType}}} {{paramName}}{{^-last}}, {{/-last}}{{/allParams}}) throws ApiException { 224 {{#returnType}}ApiResponse<{{{returnType}}}> localVarResp = {{/returnType}}{{operationId}}WithHttpInfo({{#allParams}}{{paramName}}{{^-last}}, {{/-last}}{{/allParams}});{{#returnType}} 225 return localVarResp.getData();{{/returnType}} 226 } 227 {{/vendorExtensions.x-group-parameters}} 228 229 {{^vendorExtensions.x-group-parameters}}/** 230 * {{summary}} 231 * {{notes}}{{#allParams}} 232 * @param {{paramName}} {{description}}{{#required}} (required){{/required}}{{^required}} (optional{{^isContainer}}{{#defaultValue}}, default to {{.}}{{/defaultValue}}{{/isContainer}}){{/required}}{{/allParams}} 233 * @return ApiResponse<{{#returnType}}{{returnType}}{{/returnType}}{{^returnType}}Void{{/returnType}}> 234 * @throws ApiException If fail to call the API, e.g. server error or cannot deserialize the response body 235 {{#responses.0}} 236 * @http.response.details 237 <table summary="Response Details" border="1"> 238 <tr><td> Status Code </td><td> Description </td><td> Response Headers </td></tr> 239 {{#responses}} 240 <tr><td> {{code}} </td><td> {{message}} </td><td> {{#headers}} * {{baseName}} - {{description}} <br> {{/headers}}{{^headers.0}} - {{/headers.0}} </td></tr> 241 {{/responses}} 242 </table> 243 {{/responses.0}} 244 {{#isDeprecated}} 245 * @deprecated 246 {{/isDeprecated}} 247 {{#externalDocs}} 248 * {{description}} 249 * @see <a href="{{url}}">{{summary}} Documentation</a> 250 {{/externalDocs}} 251 */ 252 {{#isDeprecated}} 253 @Deprecated 254 {{/isDeprecated}} 255 public{{/vendorExtensions.x-group-parameters}}{{#vendorExtensions.x-group-parameters}}private{{/vendorExtensions.x-group-parameters}} ApiResponse<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}Void{{/returnType}}> {{operationId}}WithHttpInfo({{#allParams}}{{#useBeanValidation}}{{>beanValidationQueryParams}}{{/useBeanValidation}}{{{dataType}}} {{paramName}}{{^-last}}, {{/-last}}{{/allParams}}) throws ApiException { 256 okhttp3.Call localVarCall = {{operationId}}ValidateBeforeCall({{#allParams}}{{paramName}}, {{/allParams}}null); 257 {{#returnType}}Type localVarReturnType = new TypeToken<{{{returnType}}}>(){}.getType(); 258 return localVarApiClient.execute(localVarCall, localVarReturnType);{{/returnType}}{{^returnType}}return localVarApiClient.execute(localVarCall);{{/returnType}} 259 } 260 261 {{^vendorExtensions.x-group-parameters}}/** 262 * {{summary}} (asynchronously) 263 * {{notes}}{{#allParams}} 264 * @param {{paramName}} {{description}}{{#required}} (required){{/required}}{{^required}} (optional{{^isContainer}}{{#defaultValue}}, default to {{.}}{{/defaultValue}}{{/isContainer}}){{/required}}{{/allParams}} 265 * @param _callback The callback to be executed when the API call finishes 266 * @return The request call 267 * @throws ApiException If fail to process the API call, e.g. serializing the request body object 268 {{#responses.0}} 269 * @http.response.details 270 <table summary="Response Details" border="1"> 271 <tr><td> Status Code </td><td> Description </td><td> Response Headers </td></tr> 272 {{#responses}} 273 <tr><td> {{code}} </td><td> {{message}} </td><td> {{#headers}} * {{baseName}} - {{description}} <br> {{/headers}}{{^headers.0}} - {{/headers.0}} </td></tr> 274 {{/responses}} 275 </table> 276 {{/responses.0}} 277 {{#isDeprecated}} 278 * @deprecated 279 {{/isDeprecated}} 280 {{#externalDocs}} 281 * {{description}} 282 * @see <a href="{{url}}">{{summary}} Documentation</a> 283 {{/externalDocs}} 284 */ 285 {{#isDeprecated}} 286 @Deprecated 287 {{/isDeprecated}} 288 public{{/vendorExtensions.x-group-parameters}}{{#vendorExtensions.x-group-parameters}}private{{/vendorExtensions.x-group-parameters}} okhttp3.Call {{operationId}}Async({{#allParams}}{{{dataType}}} {{paramName}}, {{/allParams}}final ApiCallback<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}Void{{/returnType}}> _callback) throws ApiException { 289 290 okhttp3.Call localVarCall = {{operationId}}ValidateBeforeCall({{#allParams}}{{paramName}}, {{/allParams}}_callback); 291 {{#returnType}}Type localVarReturnType = new TypeToken<{{{returnType}}}>(){}.getType(); 292 localVarApiClient.executeAsync(localVarCall, localVarReturnType, _callback);{{/returnType}}{{^returnType}}localVarApiClient.executeAsync(localVarCall, _callback);{{/returnType}} 293 return localVarCall; 294 } 295 {{#vendorExtensions.x-group-parameters}} 296 297 public class API{{operationId}}Request { 298 {{#requiredParams}} 299 private final {{{dataType}}} {{paramName}}; 300 {{/requiredParams}} 301 {{#optionalParams}} 302 private {{{dataType}}} {{paramName}}; 303 {{/optionalParams}} 304 305 private API{{operationId}}Request({{#requiredParams}}{{{dataType}}} {{paramName}}{{^-last}}, {{/-last}}{{/requiredParams}}) { 306 {{#requiredParams}} 307 this.{{paramName}} = {{paramName}}; 308 {{/requiredParams}} 309 } 310 311 {{#optionalParams}} 312 /** 313 * Set {{paramName}} 314 * @param {{paramName}} {{description}} (optional{{^isContainer}}{{#defaultValue}}, default to {{.}}{{/defaultValue}}{{/isContainer}}) 315 * @return API{{operationId}}Request 316 */ 317 public API{{operationId}}Request {{paramName}}({{{dataType}}} {{paramName}}) { 318 this.{{paramName}} = {{paramName}}; 319 return this; 320 } 321 322 {{/optionalParams}} 323 /** 324 * Build call for {{operationId}} 325 * @param _callback ApiCallback API callback 326 * @return Call to execute 327 * @throws ApiException If fail to serialize the request body object 328 {{#responses.0}} 329 * @http.response.details 330 <table summary="Response Details" border="1"> 331 <tr><td> Status Code </td><td> Description </td><td> Response Headers </td></tr> 332 {{#responses}} 333 <tr><td> {{code}} </td><td> {{message}} </td><td> {{#headers}} * {{baseName}} - {{description}} <br> {{/headers}}{{^headers.0}} - {{/headers.0}} </td></tr> 334 {{/responses}} 335 </table> 336 {{/responses.0}} 337 {{#isDeprecated}} 338 * @deprecated 339 {{/isDeprecated}} 340 */ 341 {{#isDeprecated}} 342 @Deprecated 343 {{/isDeprecated}} 344 public okhttp3.Call buildCall(final ApiCallback _callback) throws ApiException { 345 return {{operationId}}Call({{#allParams}}{{paramName}}, {{/allParams}}_callback); 346 } 347 348 /** 349 * Execute {{operationId}} request{{#returnType}} 350 * @return {{returnType}}{{/returnType}} 351 * @throws ApiException If fail to call the API, e.g. server error or cannot deserialize the response body 352 {{#responses.0}} 353 * @http.response.details 354 <table summary="Response Details" border="1"> 355 <tr><td> Status Code </td><td> Description </td><td> Response Headers </td></tr> 356 {{#responses}} 357 <tr><td> {{code}} </td><td> {{message}} </td><td> {{#headers}} * {{baseName}} - {{description}} <br> {{/headers}}{{^headers.0}} - {{/headers.0}} </td></tr> 358 {{/responses}} 359 </table> 360 {{/responses.0}} 361 {{#isDeprecated}} 362 * @deprecated 363 {{/isDeprecated}} 364 */ 365 {{#isDeprecated}} 366 @Deprecated 367 {{/isDeprecated}} 368 public {{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}void{{/returnType}} execute() throws ApiException { 369 {{#returnType}}ApiResponse<{{{returnType}}}> localVarResp = {{/returnType}}{{operationId}}WithHttpInfo({{#allParams}}{{paramName}}{{^-last}}, {{/-last}}{{/allParams}});{{#returnType}} 370 return localVarResp.getData();{{/returnType}} 371 } 372 373 /** 374 * Execute {{operationId}} request with HTTP info returned 375 * @return ApiResponse<{{#returnType}}{{returnType}}{{/returnType}}{{^returnType}}Void{{/returnType}}> 376 * @throws ApiException If fail to call the API, e.g. server error or cannot deserialize the response body 377 {{#responses.0}} 378 * @http.response.details 379 <table summary="Response Details" border="1"> 380 <tr><td> Status Code </td><td> Description </td><td> Response Headers </td></tr> 381 {{#responses}} 382 <tr><td> {{code}} </td><td> {{message}} </td><td> {{#headers}} * {{baseName}} - {{description}} <br> {{/headers}}{{^headers.0}} - {{/headers.0}} </td></tr> 383 {{/responses}} 384 </table> 385 {{/responses.0}} 386 {{#isDeprecated}} 387 * @deprecated 388 {{/isDeprecated}} 389 */ 390 {{#isDeprecated}} 391 @Deprecated 392 {{/isDeprecated}} 393 public ApiResponse<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}Void{{/returnType}}> executeWithHttpInfo() throws ApiException { 394 return {{operationId}}WithHttpInfo({{#allParams}}{{paramName}}{{^-last}}, {{/-last}}{{/allParams}}); 395 } 396 397 /** 398 * Execute {{operationId}} request (asynchronously) 399 * @param _callback The callback to be executed when the API call finishes 400 * @return The request call 401 * @throws ApiException If fail to process the API call, e.g. serializing the request body object 402 {{#responses.0}} 403 * @http.response.details 404 <table summary="Response Details" border="1"> 405 <tr><td> Status Code </td><td> Description </td><td> Response Headers </td></tr> 406 {{#responses}} 407 <tr><td> {{code}} </td><td> {{message}} </td><td> {{#headers}} * {{baseName}} - {{description}} <br> {{/headers}}{{^headers.0}} - {{/headers.0}} </td></tr> 408 {{/responses}} 409 </table> 410 {{/responses.0}} 411 {{#isDeprecated}} 412 * @deprecated 413 {{/isDeprecated}} 414 */ 415 {{#isDeprecated}} 416 @Deprecated 417 {{/isDeprecated}} 418 public okhttp3.Call executeAsync(final ApiCallback<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}Void{{/returnType}}> _callback) throws ApiException { 419 return {{operationId}}Async({{#allParams}}{{paramName}}, {{/allParams}}_callback); 420 } 421 } 422 423 /** 424 * {{summary}} 425 * {{notes}}{{#requiredParams}} 426 * @param {{paramName}} {{description}} (required){{/requiredParams}} 427 * @return API{{operationId}}Request 428 {{#responses.0}} 429 * @http.response.details 430 <table summary="Response Details" border="1"> 431 <tr><td> Status Code </td><td> Description </td><td> Response Headers </td></tr> 432 {{#responses}} 433 <tr><td> {{code}} </td><td> {{message}} </td><td> {{#headers}} * {{baseName}} - {{description}} <br> {{/headers}}{{^headers.0}} - {{/headers.0}} </td></tr> 434 {{/responses}} 435 </table> 436 {{/responses.0}} 437 {{#isDeprecated}} 438 * @deprecated 439 {{/isDeprecated}} 440 {{#externalDocs}} 441 * {{description}} 442 * @see <a href="{{url}}">{{summary}} Documentation</a> 443 {{/externalDocs}} 444 */ 445 {{#isDeprecated}} 446 @Deprecated 447 {{/isDeprecated}} 448 public API{{operationId}}Request {{operationId}}({{#requiredParams}}{{{dataType}}} {{paramName}}{{^-last}}, {{/-last}}{{/requiredParams}}) { 449 return new API{{operationId}}Request({{#requiredParams}}{{paramName}}{{^-last}}, {{/-last}}{{/requiredParams}}); 450 } 451 {{/vendorExtensions.x-group-parameters}} 452 {{/operation}} 453 } 454 {{/operations}}