[Fix/Dart2] Resolve an exception with status 204 and no body. (#7647)

* Resolve an exception situation when a remote server returns 204 with no body.

* Only return a value when needed.

* Use HttpStatus codes instead of magic numbers.

* Drop checking for a body as it will consume too much memory.

* Cosmetic changes.
This commit is contained in:
Noor Dawod 2020-10-13 17:30:49 +02:00 committed by GitHub
parent 177e536bdf
commit 3c869d5b12
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 125 additions and 124 deletions

View File

@ -50,16 +50,19 @@ class {{{classname}}} {
{{/hasMore}} {{/hasMore}}
{{/allParams}} {{/allParams}}
{{#returnType}}Future<Response> {{/returnType}}{{^returnType}}Future {{/returnType}}{{{nickname}}}WithHttpInfo({{#allParams}}{{#required}}{{{dataType}}} {{{paramName}}}{{#hasMore}}, {{/hasMore}}{{/required}}{{/allParams}}{{#hasOptionalParams}}{ {{#allParams}}{{^required}}{{{dataType}}} {{{paramName}}}{{#hasMore}}, {{/hasMore}}{{/required}}{{/allParams}} }{{/hasOptionalParams}}) async { {{#returnType}}Future<Response> {{/returnType}}{{^returnType}}Future {{/returnType}}{{{nickname}}}WithHttpInfo({{#allParams}}{{#required}}{{{dataType}}} {{{paramName}}}{{#hasMore}}, {{/hasMore}}{{/required}}{{/allParams}}{{#hasOptionalParams}}{ {{#allParams}}{{^required}}{{{dataType}}} {{{paramName}}}{{#hasMore}}, {{/hasMore}}{{/required}}{{/allParams}} }{{/hasOptionalParams}}) async {
{{#hasParams}}
// Verify required params are set. // Verify required params are set.
{{#allParams}} {{#allParams}}
{{#required}} {{#required}}
if ({{{paramName}}} == null) { if ({{{paramName}}} == null) {
throw ApiException(400, 'Missing required param: {{{paramName}}}'); throw ApiException(HttpStatus.badRequest, 'Missing required param: {{{paramName}}}');
} }
{{/required}} {{/required}}
{{/allParams}} {{/allParams}}
final path = '{{{path}}}'.replaceAll('{format}', 'json'){{#pathParams}}.replaceAll('{' + '{{{baseName}}}' + '}', {{{paramName}}}.toString()){{/pathParams}}; {{/hasParams}}
final path = '{{{path}}}'.replaceAll('{format}', 'json'){{#pathParams}}
.replaceAll('{' + '{{{baseName}}}' + '}', {{{paramName}}}.toString()){{/pathParams}};
Object postBody{{#bodyParam}} = {{{paramName}}}{{/bodyParam}}; Object postBody{{#bodyParam}} = {{{paramName}}}{{/bodyParam}};
@ -162,31 +165,30 @@ class {{{classname}}} {
{{/allParams}} {{/allParams}}
{{#returnType}}Future<{{{returnType}}}> {{/returnType}}{{^returnType}}Future {{/returnType}}{{{nickname}}}({{#allParams}}{{#required}}{{{dataType}}} {{{paramName}}}{{#hasMore}}, {{/hasMore}}{{/required}}{{/allParams}}{{#hasOptionalParams}}{ {{#allParams}}{{^required}}{{{dataType}}} {{{paramName}}}{{#hasMore}}, {{/hasMore}}{{/required}}{{/allParams}} }{{/hasOptionalParams}}) async { {{#returnType}}Future<{{{returnType}}}> {{/returnType}}{{^returnType}}Future {{/returnType}}{{{nickname}}}({{#allParams}}{{#required}}{{{dataType}}} {{{paramName}}}{{#hasMore}}, {{/hasMore}}{{/required}}{{/allParams}}{{#hasOptionalParams}}{ {{#allParams}}{{^required}}{{{dataType}}} {{{paramName}}}{{#hasMore}}, {{/hasMore}}{{/required}}{{/allParams}} }{{/hasOptionalParams}}) async {
final response = await {{{nickname}}}WithHttpInfo({{#allParams}}{{#required}}{{{paramName}}}{{#hasMore}}, {{/hasMore}}{{/required}}{{/allParams}}{{#hasOptionalParams}} {{#allParams}}{{^required}}{{{paramName}}}: {{{paramName}}}{{#hasMore}}, {{/hasMore}}{{/required}}{{/allParams}} {{/hasOptionalParams}}); final response = await {{{nickname}}}WithHttpInfo({{#allParams}}{{#required}}{{{paramName}}}{{#hasMore}}, {{/hasMore}}{{/required}}{{/allParams}}{{#hasOptionalParams}} {{#allParams}}{{^required}}{{{paramName}}}: {{{paramName}}}{{#hasMore}}, {{/hasMore}}{{/required}}{{/allParams}} {{/hasOptionalParams}});
if (response.statusCode >= 400) { if (response.statusCode >= HttpStatus.badRequest) {
throw ApiException(response.statusCode, _decodeBodyBytes(response)); throw ApiException(response.statusCode, _decodeBodyBytes(response));
} }
if (response.body != null) { {{#returnType}}
// When a remote server returns no body with a status of 204, we shall not decode it.
// At the time of writing this, `dart:convert` will throw an "Unexpected end of input"
// FormatException when trying to decode an empty string.
if (response.body != null && response.statusCode != HttpStatus.noContent) {
{{#isListContainer}} {{#isListContainer}}
{{#returnType}}
return (apiClient.deserialize(_decodeBodyBytes(response), '{{{returnType}}}') as List) return (apiClient.deserialize(_decodeBodyBytes(response), '{{{returnType}}}') as List)
.map((item) => item as {{{returnBaseType}}}) .map((item) => item as {{{returnBaseType}}})
.toList(growable: false); .toList(growable: false);
{{/returnType}}
{{/isListContainer}} {{/isListContainer}}
{{^isListContainer}} {{^isListContainer}}
{{#isMap}} {{#isMap}}
{{#returnType}}
return {{{returnType}}}.from(apiClient.deserialize(_decodeBodyBytes(response), '{{{returnType}}}')); return {{{returnType}}}.from(apiClient.deserialize(_decodeBodyBytes(response), '{{{returnType}}}'));
{{/returnType}}
{{/isMap}} {{/isMap}}
{{^isMap}} {{^isMap}}
{{#returnType}}
return apiClient.deserialize(_decodeBodyBytes(response), '{{{returnType}}}') as {{{returnType}}}; return apiClient.deserialize(_decodeBodyBytes(response), '{{{returnType}}}') as {{{returnType}}};
{{/returnType}}
{{/isMap}} {{/isMap}}
{{/isListContainer}} {{/isListContainer}}
} }
return{{#returnType}} null{{/returnType}}; return null;
{{/returnType}}
} }
{{/operation}} {{/operation}}
} }

View File

@ -125,16 +125,16 @@ class ApiClient {
case 'GET': return await _client.get(url, headers: nullableHeaderParams); case 'GET': return await _client.get(url, headers: nullableHeaderParams);
} }
} on SocketException catch (e, trace) { } on SocketException catch (e, trace) {
throw ApiException.withInner(400, 'Socket operation failed: $method $path', e, trace); throw ApiException.withInner(HttpStatus.badRequest, 'Socket operation failed: $method $path', e, trace);
} on TlsException catch (e, trace) { } on TlsException catch (e, trace) {
throw ApiException.withInner(400, 'TLS/SSL communication failed: $method $path', e, trace); throw ApiException.withInner(HttpStatus.badRequest, 'TLS/SSL communication failed: $method $path', e, trace);
} on IOException catch (e, trace) { } on IOException catch (e, trace) {
throw ApiException.withInner(400, 'I/O operation failed: $method $path', e, trace); throw ApiException.withInner(HttpStatus.badRequest, 'I/O operation failed: $method $path', e, trace);
} on Exception catch (e, trace) { } on Exception catch (e, trace) {
throw ApiException.withInner(400, 'Exception occurred: $method $path', e, trace); throw ApiException.withInner(HttpStatus.badRequest, 'Exception occurred: $method $path', e, trace);
} }
throw ApiException(400, 'Invalid HTTP operation: $method $path'); throw ApiException(HttpStatus.badRequest, 'Invalid HTTP operation: $method $path');
} }
dynamic _deserialize(dynamic value, String targetType, {bool growable}) { dynamic _deserialize(dynamic value, String targetType, {bool growable}) {
@ -182,9 +182,9 @@ class ApiClient {
break; break;
} }
} on Exception catch (e, stack) { } on Exception catch (e, stack) {
throw ApiException.withInner(500, 'Exception during deserialization.', e, stack); throw ApiException.withInner(HttpStatus.internalServerError, 'Exception during deserialization.', e, stack);
} }
throw ApiException(500, 'Could not find a suitable class for deserialization'); throw ApiException(HttpStatus.internalServerError, 'Could not find a suitable class for deserialization');
} }
/// Update query and header parameters based on authentication settings. /// Update query and header parameters based on authentication settings.

View File

@ -55,7 +55,7 @@ String parameterToString(dynamic value) {
/// content type. Otherwise, returns the decoded body as decoded by dart:http package. /// content type. Otherwise, returns the decoded body as decoded by dart:http package.
String _decodeBodyBytes(Response response) { String _decodeBodyBytes(Response response) {
final contentType = response.headers['content-type']; final contentType = response.headers['content-type'];
return contentType != null && contentType.contains('application/json') return contentType != null && contentType.toLowerCase().startsWith('application/json')
? utf8.decode(response.bodyBytes) ? response.bodyBytes == null ? null : utf8.decode(response.bodyBytes)
: response.body; : response.body;
} }

View File

@ -26,7 +26,7 @@ class PetApi {
Future addPetWithHttpInfo(Pet body) async { Future addPetWithHttpInfo(Pet body) async {
// Verify required params are set. // Verify required params are set.
if (body == null) { if (body == null) {
throw ApiException(400, 'Missing required param: body'); throw ApiException(HttpStatus.badRequest, 'Missing required param: body');
} }
final path = '/pet'.replaceAll('{format}', 'json'); final path = '/pet'.replaceAll('{format}', 'json');
@ -74,12 +74,9 @@ class PetApi {
/// Pet object that needs to be added to the store /// Pet object that needs to be added to the store
Future addPet(Pet body) async { Future addPet(Pet body) async {
final response = await addPetWithHttpInfo(body); final response = await addPetWithHttpInfo(body);
if (response.statusCode >= 400) { if (response.statusCode >= HttpStatus.badRequest) {
throw ApiException(response.statusCode, _decodeBodyBytes(response)); throw ApiException(response.statusCode, _decodeBodyBytes(response));
} }
if (response.body != null) {
}
return;
} }
/// Deletes a pet /// Deletes a pet
@ -95,10 +92,11 @@ class PetApi {
Future deletePetWithHttpInfo(int petId, { String apiKey }) async { Future deletePetWithHttpInfo(int petId, { String apiKey }) async {
// Verify required params are set. // Verify required params are set.
if (petId == null) { if (petId == null) {
throw ApiException(400, 'Missing required param: petId'); throw ApiException(HttpStatus.badRequest, 'Missing required param: petId');
} }
final path = '/pet/{petId}'.replaceAll('{format}', 'json').replaceAll('{' + 'petId' + '}', petId.toString()); final path = '/pet/{petId}'.replaceAll('{format}', 'json')
.replaceAll('{' + 'petId' + '}', petId.toString());
Object postBody; Object postBody;
@ -146,12 +144,9 @@ class PetApi {
/// * [String] apiKey: /// * [String] apiKey:
Future deletePet(int petId, { String apiKey }) async { Future deletePet(int petId, { String apiKey }) async {
final response = await deletePetWithHttpInfo(petId, apiKey: apiKey ); final response = await deletePetWithHttpInfo(petId, apiKey: apiKey );
if (response.statusCode >= 400) { if (response.statusCode >= HttpStatus.badRequest) {
throw ApiException(response.statusCode, _decodeBodyBytes(response)); throw ApiException(response.statusCode, _decodeBodyBytes(response));
} }
if (response.body != null) {
}
return;
} }
/// Finds Pets by status /// Finds Pets by status
@ -167,7 +162,7 @@ class PetApi {
Future<Response> findPetsByStatusWithHttpInfo(List<String> status) async { Future<Response> findPetsByStatusWithHttpInfo(List<String> status) async {
// Verify required params are set. // Verify required params are set.
if (status == null) { if (status == null) {
throw ApiException(400, 'Missing required param: status'); throw ApiException(HttpStatus.badRequest, 'Missing required param: status');
} }
final path = '/pet/findByStatus'.replaceAll('{format}', 'json'); final path = '/pet/findByStatus'.replaceAll('{format}', 'json');
@ -218,10 +213,13 @@ class PetApi {
/// Status values that need to be considered for filter /// Status values that need to be considered for filter
Future<List<Pet>> findPetsByStatus(List<String> status) async { Future<List<Pet>> findPetsByStatus(List<String> status) async {
final response = await findPetsByStatusWithHttpInfo(status); final response = await findPetsByStatusWithHttpInfo(status);
if (response.statusCode >= 400) { if (response.statusCode >= HttpStatus.badRequest) {
throw ApiException(response.statusCode, _decodeBodyBytes(response)); throw ApiException(response.statusCode, _decodeBodyBytes(response));
} }
if (response.body != null) { // When a remote server returns no body with a status of 204, we shall not decode it.
// At the time of writing this, `dart:convert` will throw an "Unexpected end of input"
// FormatException when trying to decode an empty string.
if (response.body != null && response.statusCode != HttpStatus.noContent) {
return (apiClient.deserialize(_decodeBodyBytes(response), 'List<Pet>') as List) return (apiClient.deserialize(_decodeBodyBytes(response), 'List<Pet>') as List)
.map((item) => item as Pet) .map((item) => item as Pet)
.toList(growable: false); .toList(growable: false);
@ -242,7 +240,7 @@ class PetApi {
Future<Response> findPetsByTagsWithHttpInfo(List<String> tags) async { Future<Response> findPetsByTagsWithHttpInfo(List<String> tags) async {
// Verify required params are set. // Verify required params are set.
if (tags == null) { if (tags == null) {
throw ApiException(400, 'Missing required param: tags'); throw ApiException(HttpStatus.badRequest, 'Missing required param: tags');
} }
final path = '/pet/findByTags'.replaceAll('{format}', 'json'); final path = '/pet/findByTags'.replaceAll('{format}', 'json');
@ -293,10 +291,13 @@ class PetApi {
/// Tags to filter by /// Tags to filter by
Future<List<Pet>> findPetsByTags(List<String> tags) async { Future<List<Pet>> findPetsByTags(List<String> tags) async {
final response = await findPetsByTagsWithHttpInfo(tags); final response = await findPetsByTagsWithHttpInfo(tags);
if (response.statusCode >= 400) { if (response.statusCode >= HttpStatus.badRequest) {
throw ApiException(response.statusCode, _decodeBodyBytes(response)); throw ApiException(response.statusCode, _decodeBodyBytes(response));
} }
if (response.body != null) { // When a remote server returns no body with a status of 204, we shall not decode it.
// At the time of writing this, `dart:convert` will throw an "Unexpected end of input"
// FormatException when trying to decode an empty string.
if (response.body != null && response.statusCode != HttpStatus.noContent) {
return (apiClient.deserialize(_decodeBodyBytes(response), 'List<Pet>') as List) return (apiClient.deserialize(_decodeBodyBytes(response), 'List<Pet>') as List)
.map((item) => item as Pet) .map((item) => item as Pet)
.toList(growable: false); .toList(growable: false);
@ -317,10 +318,11 @@ class PetApi {
Future<Response> getPetByIdWithHttpInfo(int petId) async { Future<Response> getPetByIdWithHttpInfo(int petId) async {
// Verify required params are set. // Verify required params are set.
if (petId == null) { if (petId == null) {
throw ApiException(400, 'Missing required param: petId'); throw ApiException(HttpStatus.badRequest, 'Missing required param: petId');
} }
final path = '/pet/{petId}'.replaceAll('{format}', 'json').replaceAll('{' + 'petId' + '}', petId.toString()); final path = '/pet/{petId}'.replaceAll('{format}', 'json')
.replaceAll('{' + 'petId' + '}', petId.toString());
Object postBody; Object postBody;
@ -367,10 +369,13 @@ class PetApi {
/// ID of pet to return /// ID of pet to return
Future<Pet> getPetById(int petId) async { Future<Pet> getPetById(int petId) async {
final response = await getPetByIdWithHttpInfo(petId); final response = await getPetByIdWithHttpInfo(petId);
if (response.statusCode >= 400) { if (response.statusCode >= HttpStatus.badRequest) {
throw ApiException(response.statusCode, _decodeBodyBytes(response)); throw ApiException(response.statusCode, _decodeBodyBytes(response));
} }
if (response.body != null) { // When a remote server returns no body with a status of 204, we shall not decode it.
// At the time of writing this, `dart:convert` will throw an "Unexpected end of input"
// FormatException when trying to decode an empty string.
if (response.body != null && response.statusCode != HttpStatus.noContent) {
return apiClient.deserialize(_decodeBodyBytes(response), 'Pet') as Pet; return apiClient.deserialize(_decodeBodyBytes(response), 'Pet') as Pet;
} }
return null; return null;
@ -387,7 +392,7 @@ class PetApi {
Future updatePetWithHttpInfo(Pet body) async { Future updatePetWithHttpInfo(Pet body) async {
// Verify required params are set. // Verify required params are set.
if (body == null) { if (body == null) {
throw ApiException(400, 'Missing required param: body'); throw ApiException(HttpStatus.badRequest, 'Missing required param: body');
} }
final path = '/pet'.replaceAll('{format}', 'json'); final path = '/pet'.replaceAll('{format}', 'json');
@ -435,12 +440,9 @@ class PetApi {
/// Pet object that needs to be added to the store /// Pet object that needs to be added to the store
Future updatePet(Pet body) async { Future updatePet(Pet body) async {
final response = await updatePetWithHttpInfo(body); final response = await updatePetWithHttpInfo(body);
if (response.statusCode >= 400) { if (response.statusCode >= HttpStatus.badRequest) {
throw ApiException(response.statusCode, _decodeBodyBytes(response)); throw ApiException(response.statusCode, _decodeBodyBytes(response));
} }
if (response.body != null) {
}
return;
} }
/// Updates a pet in the store with form data /// Updates a pet in the store with form data
@ -460,10 +462,11 @@ class PetApi {
Future updatePetWithFormWithHttpInfo(int petId, { String name, String status }) async { Future updatePetWithFormWithHttpInfo(int petId, { String name, String status }) async {
// Verify required params are set. // Verify required params are set.
if (petId == null) { if (petId == null) {
throw ApiException(400, 'Missing required param: petId'); throw ApiException(HttpStatus.badRequest, 'Missing required param: petId');
} }
final path = '/pet/{petId}'.replaceAll('{format}', 'json').replaceAll('{' + 'petId' + '}', petId.toString()); final path = '/pet/{petId}'.replaceAll('{format}', 'json')
.replaceAll('{' + 'petId' + '}', petId.toString());
Object postBody; Object postBody;
@ -528,12 +531,9 @@ class PetApi {
/// Updated status of the pet /// Updated status of the pet
Future updatePetWithForm(int petId, { String name, String status }) async { Future updatePetWithForm(int petId, { String name, String status }) async {
final response = await updatePetWithFormWithHttpInfo(petId, name: name, status: status ); final response = await updatePetWithFormWithHttpInfo(petId, name: name, status: status );
if (response.statusCode >= 400) { if (response.statusCode >= HttpStatus.badRequest) {
throw ApiException(response.statusCode, _decodeBodyBytes(response)); throw ApiException(response.statusCode, _decodeBodyBytes(response));
} }
if (response.body != null) {
}
return;
} }
/// uploads an image /// uploads an image
@ -553,10 +553,11 @@ class PetApi {
Future<Response> uploadFileWithHttpInfo(int petId, { String additionalMetadata, MultipartFile file }) async { Future<Response> uploadFileWithHttpInfo(int petId, { String additionalMetadata, MultipartFile file }) async {
// Verify required params are set. // Verify required params are set.
if (petId == null) { if (petId == null) {
throw ApiException(400, 'Missing required param: petId'); throw ApiException(HttpStatus.badRequest, 'Missing required param: petId');
} }
final path = '/pet/{petId}/uploadImage'.replaceAll('{format}', 'json').replaceAll('{' + 'petId' + '}', petId.toString()); final path = '/pet/{petId}/uploadImage'.replaceAll('{format}', 'json')
.replaceAll('{' + 'petId' + '}', petId.toString());
Object postBody; Object postBody;
@ -619,10 +620,13 @@ class PetApi {
/// file to upload /// file to upload
Future<ApiResponse> uploadFile(int petId, { String additionalMetadata, MultipartFile file }) async { Future<ApiResponse> uploadFile(int petId, { String additionalMetadata, MultipartFile file }) async {
final response = await uploadFileWithHttpInfo(petId, additionalMetadata: additionalMetadata, file: file ); final response = await uploadFileWithHttpInfo(petId, additionalMetadata: additionalMetadata, file: file );
if (response.statusCode >= 400) { if (response.statusCode >= HttpStatus.badRequest) {
throw ApiException(response.statusCode, _decodeBodyBytes(response)); throw ApiException(response.statusCode, _decodeBodyBytes(response));
} }
if (response.body != null) { // When a remote server returns no body with a status of 204, we shall not decode it.
// At the time of writing this, `dart:convert` will throw an "Unexpected end of input"
// FormatException when trying to decode an empty string.
if (response.body != null && response.statusCode != HttpStatus.noContent) {
return apiClient.deserialize(_decodeBodyBytes(response), 'ApiResponse') as ApiResponse; return apiClient.deserialize(_decodeBodyBytes(response), 'ApiResponse') as ApiResponse;
} }
return null; return null;

View File

@ -28,10 +28,11 @@ class StoreApi {
Future deleteOrderWithHttpInfo(String orderId) async { Future deleteOrderWithHttpInfo(String orderId) async {
// Verify required params are set. // Verify required params are set.
if (orderId == null) { if (orderId == null) {
throw ApiException(400, 'Missing required param: orderId'); throw ApiException(HttpStatus.badRequest, 'Missing required param: orderId');
} }
final path = '/store/order/{orderId}'.replaceAll('{format}', 'json').replaceAll('{' + 'orderId' + '}', orderId.toString()); final path = '/store/order/{orderId}'.replaceAll('{format}', 'json')
.replaceAll('{' + 'orderId' + '}', orderId.toString());
Object postBody; Object postBody;
@ -78,12 +79,9 @@ class StoreApi {
/// ID of the order that needs to be deleted /// ID of the order that needs to be deleted
Future deleteOrder(String orderId) async { Future deleteOrder(String orderId) async {
final response = await deleteOrderWithHttpInfo(orderId); final response = await deleteOrderWithHttpInfo(orderId);
if (response.statusCode >= 400) { if (response.statusCode >= HttpStatus.badRequest) {
throw ApiException(response.statusCode, _decodeBodyBytes(response)); throw ApiException(response.statusCode, _decodeBodyBytes(response));
} }
if (response.body != null) {
}
return;
} }
/// Returns pet inventories by status /// Returns pet inventories by status
@ -92,8 +90,6 @@ class StoreApi {
/// ///
/// Note: This method returns the HTTP [Response]. /// Note: This method returns the HTTP [Response].
Future<Response> getInventoryWithHttpInfo() async { Future<Response> getInventoryWithHttpInfo() async {
// Verify required params are set.
final path = '/store/inventory'.replaceAll('{format}', 'json'); final path = '/store/inventory'.replaceAll('{format}', 'json');
Object postBody; Object postBody;
@ -136,10 +132,13 @@ class StoreApi {
/// Returns a map of status codes to quantities /// Returns a map of status codes to quantities
Future<Map<String, int>> getInventory() async { Future<Map<String, int>> getInventory() async {
final response = await getInventoryWithHttpInfo(); final response = await getInventoryWithHttpInfo();
if (response.statusCode >= 400) { if (response.statusCode >= HttpStatus.badRequest) {
throw ApiException(response.statusCode, _decodeBodyBytes(response)); throw ApiException(response.statusCode, _decodeBodyBytes(response));
} }
if (response.body != null) { // When a remote server returns no body with a status of 204, we shall not decode it.
// At the time of writing this, `dart:convert` will throw an "Unexpected end of input"
// FormatException when trying to decode an empty string.
if (response.body != null && response.statusCode != HttpStatus.noContent) {
return Map<String, int>.from(apiClient.deserialize(_decodeBodyBytes(response), 'Map<String, int>')); return Map<String, int>.from(apiClient.deserialize(_decodeBodyBytes(response), 'Map<String, int>'));
} }
return null; return null;
@ -158,10 +157,11 @@ class StoreApi {
Future<Response> getOrderByIdWithHttpInfo(int orderId) async { Future<Response> getOrderByIdWithHttpInfo(int orderId) async {
// Verify required params are set. // Verify required params are set.
if (orderId == null) { if (orderId == null) {
throw ApiException(400, 'Missing required param: orderId'); throw ApiException(HttpStatus.badRequest, 'Missing required param: orderId');
} }
final path = '/store/order/{orderId}'.replaceAll('{format}', 'json').replaceAll('{' + 'orderId' + '}', orderId.toString()); final path = '/store/order/{orderId}'.replaceAll('{format}', 'json')
.replaceAll('{' + 'orderId' + '}', orderId.toString());
Object postBody; Object postBody;
@ -208,10 +208,13 @@ class StoreApi {
/// ID of pet that needs to be fetched /// ID of pet that needs to be fetched
Future<Order> getOrderById(int orderId) async { Future<Order> getOrderById(int orderId) async {
final response = await getOrderByIdWithHttpInfo(orderId); final response = await getOrderByIdWithHttpInfo(orderId);
if (response.statusCode >= 400) { if (response.statusCode >= HttpStatus.badRequest) {
throw ApiException(response.statusCode, _decodeBodyBytes(response)); throw ApiException(response.statusCode, _decodeBodyBytes(response));
} }
if (response.body != null) { // When a remote server returns no body with a status of 204, we shall not decode it.
// At the time of writing this, `dart:convert` will throw an "Unexpected end of input"
// FormatException when trying to decode an empty string.
if (response.body != null && response.statusCode != HttpStatus.noContent) {
return apiClient.deserialize(_decodeBodyBytes(response), 'Order') as Order; return apiClient.deserialize(_decodeBodyBytes(response), 'Order') as Order;
} }
return null; return null;
@ -228,7 +231,7 @@ class StoreApi {
Future<Response> placeOrderWithHttpInfo(Order body) async { Future<Response> placeOrderWithHttpInfo(Order body) async {
// Verify required params are set. // Verify required params are set.
if (body == null) { if (body == null) {
throw ApiException(400, 'Missing required param: body'); throw ApiException(HttpStatus.badRequest, 'Missing required param: body');
} }
final path = '/store/order'.replaceAll('{format}', 'json'); final path = '/store/order'.replaceAll('{format}', 'json');
@ -276,10 +279,13 @@ class StoreApi {
/// order placed for purchasing the pet /// order placed for purchasing the pet
Future<Order> placeOrder(Order body) async { Future<Order> placeOrder(Order body) async {
final response = await placeOrderWithHttpInfo(body); final response = await placeOrderWithHttpInfo(body);
if (response.statusCode >= 400) { if (response.statusCode >= HttpStatus.badRequest) {
throw ApiException(response.statusCode, _decodeBodyBytes(response)); throw ApiException(response.statusCode, _decodeBodyBytes(response));
} }
if (response.body != null) { // When a remote server returns no body with a status of 204, we shall not decode it.
// At the time of writing this, `dart:convert` will throw an "Unexpected end of input"
// FormatException when trying to decode an empty string.
if (response.body != null && response.statusCode != HttpStatus.noContent) {
return apiClient.deserialize(_decodeBodyBytes(response), 'Order') as Order; return apiClient.deserialize(_decodeBodyBytes(response), 'Order') as Order;
} }
return null; return null;

View File

@ -28,7 +28,7 @@ class UserApi {
Future createUserWithHttpInfo(User body) async { Future createUserWithHttpInfo(User body) async {
// Verify required params are set. // Verify required params are set.
if (body == null) { if (body == null) {
throw ApiException(400, 'Missing required param: body'); throw ApiException(HttpStatus.badRequest, 'Missing required param: body');
} }
final path = '/user'.replaceAll('{format}', 'json'); final path = '/user'.replaceAll('{format}', 'json');
@ -78,12 +78,9 @@ class UserApi {
/// Created user object /// Created user object
Future createUser(User body) async { Future createUser(User body) async {
final response = await createUserWithHttpInfo(body); final response = await createUserWithHttpInfo(body);
if (response.statusCode >= 400) { if (response.statusCode >= HttpStatus.badRequest) {
throw ApiException(response.statusCode, _decodeBodyBytes(response)); throw ApiException(response.statusCode, _decodeBodyBytes(response));
} }
if (response.body != null) {
}
return;
} }
/// Creates list of users with given input array /// Creates list of users with given input array
@ -97,7 +94,7 @@ class UserApi {
Future createUsersWithArrayInputWithHttpInfo(List<User> body) async { Future createUsersWithArrayInputWithHttpInfo(List<User> body) async {
// Verify required params are set. // Verify required params are set.
if (body == null) { if (body == null) {
throw ApiException(400, 'Missing required param: body'); throw ApiException(HttpStatus.badRequest, 'Missing required param: body');
} }
final path = '/user/createWithArray'.replaceAll('{format}', 'json'); final path = '/user/createWithArray'.replaceAll('{format}', 'json');
@ -145,12 +142,9 @@ class UserApi {
/// List of user object /// List of user object
Future createUsersWithArrayInput(List<User> body) async { Future createUsersWithArrayInput(List<User> body) async {
final response = await createUsersWithArrayInputWithHttpInfo(body); final response = await createUsersWithArrayInputWithHttpInfo(body);
if (response.statusCode >= 400) { if (response.statusCode >= HttpStatus.badRequest) {
throw ApiException(response.statusCode, _decodeBodyBytes(response)); throw ApiException(response.statusCode, _decodeBodyBytes(response));
} }
if (response.body != null) {
}
return;
} }
/// Creates list of users with given input array /// Creates list of users with given input array
@ -164,7 +158,7 @@ class UserApi {
Future createUsersWithListInputWithHttpInfo(List<User> body) async { Future createUsersWithListInputWithHttpInfo(List<User> body) async {
// Verify required params are set. // Verify required params are set.
if (body == null) { if (body == null) {
throw ApiException(400, 'Missing required param: body'); throw ApiException(HttpStatus.badRequest, 'Missing required param: body');
} }
final path = '/user/createWithList'.replaceAll('{format}', 'json'); final path = '/user/createWithList'.replaceAll('{format}', 'json');
@ -212,12 +206,9 @@ class UserApi {
/// List of user object /// List of user object
Future createUsersWithListInput(List<User> body) async { Future createUsersWithListInput(List<User> body) async {
final response = await createUsersWithListInputWithHttpInfo(body); final response = await createUsersWithListInputWithHttpInfo(body);
if (response.statusCode >= 400) { if (response.statusCode >= HttpStatus.badRequest) {
throw ApiException(response.statusCode, _decodeBodyBytes(response)); throw ApiException(response.statusCode, _decodeBodyBytes(response));
} }
if (response.body != null) {
}
return;
} }
/// Delete user /// Delete user
@ -233,10 +224,11 @@ class UserApi {
Future deleteUserWithHttpInfo(String username) async { Future deleteUserWithHttpInfo(String username) async {
// Verify required params are set. // Verify required params are set.
if (username == null) { if (username == null) {
throw ApiException(400, 'Missing required param: username'); throw ApiException(HttpStatus.badRequest, 'Missing required param: username');
} }
final path = '/user/{username}'.replaceAll('{format}', 'json').replaceAll('{' + 'username' + '}', username.toString()); final path = '/user/{username}'.replaceAll('{format}', 'json')
.replaceAll('{' + 'username' + '}', username.toString());
Object postBody; Object postBody;
@ -283,12 +275,9 @@ class UserApi {
/// The name that needs to be deleted /// The name that needs to be deleted
Future deleteUser(String username) async { Future deleteUser(String username) async {
final response = await deleteUserWithHttpInfo(username); final response = await deleteUserWithHttpInfo(username);
if (response.statusCode >= 400) { if (response.statusCode >= HttpStatus.badRequest) {
throw ApiException(response.statusCode, _decodeBodyBytes(response)); throw ApiException(response.statusCode, _decodeBodyBytes(response));
} }
if (response.body != null) {
}
return;
} }
/// Get user by user name /// Get user by user name
@ -302,10 +291,11 @@ class UserApi {
Future<Response> getUserByNameWithHttpInfo(String username) async { Future<Response> getUserByNameWithHttpInfo(String username) async {
// Verify required params are set. // Verify required params are set.
if (username == null) { if (username == null) {
throw ApiException(400, 'Missing required param: username'); throw ApiException(HttpStatus.badRequest, 'Missing required param: username');
} }
final path = '/user/{username}'.replaceAll('{format}', 'json').replaceAll('{' + 'username' + '}', username.toString()); final path = '/user/{username}'.replaceAll('{format}', 'json')
.replaceAll('{' + 'username' + '}', username.toString());
Object postBody; Object postBody;
@ -350,10 +340,13 @@ class UserApi {
/// The name that needs to be fetched. Use user1 for testing. /// The name that needs to be fetched. Use user1 for testing.
Future<User> getUserByName(String username) async { Future<User> getUserByName(String username) async {
final response = await getUserByNameWithHttpInfo(username); final response = await getUserByNameWithHttpInfo(username);
if (response.statusCode >= 400) { if (response.statusCode >= HttpStatus.badRequest) {
throw ApiException(response.statusCode, _decodeBodyBytes(response)); throw ApiException(response.statusCode, _decodeBodyBytes(response));
} }
if (response.body != null) { // When a remote server returns no body with a status of 204, we shall not decode it.
// At the time of writing this, `dart:convert` will throw an "Unexpected end of input"
// FormatException when trying to decode an empty string.
if (response.body != null && response.statusCode != HttpStatus.noContent) {
return apiClient.deserialize(_decodeBodyBytes(response), 'User') as User; return apiClient.deserialize(_decodeBodyBytes(response), 'User') as User;
} }
return null; return null;
@ -373,10 +366,10 @@ class UserApi {
Future<Response> loginUserWithHttpInfo(String username, String password) async { Future<Response> loginUserWithHttpInfo(String username, String password) async {
// Verify required params are set. // Verify required params are set.
if (username == null) { if (username == null) {
throw ApiException(400, 'Missing required param: username'); throw ApiException(HttpStatus.badRequest, 'Missing required param: username');
} }
if (password == null) { if (password == null) {
throw ApiException(400, 'Missing required param: password'); throw ApiException(HttpStatus.badRequest, 'Missing required param: password');
} }
final path = '/user/login'.replaceAll('{format}', 'json'); final path = '/user/login'.replaceAll('{format}', 'json');
@ -429,10 +422,13 @@ class UserApi {
/// The password for login in clear text /// The password for login in clear text
Future<String> loginUser(String username, String password) async { Future<String> loginUser(String username, String password) async {
final response = await loginUserWithHttpInfo(username, password); final response = await loginUserWithHttpInfo(username, password);
if (response.statusCode >= 400) { if (response.statusCode >= HttpStatus.badRequest) {
throw ApiException(response.statusCode, _decodeBodyBytes(response)); throw ApiException(response.statusCode, _decodeBodyBytes(response));
} }
if (response.body != null) { // When a remote server returns no body with a status of 204, we shall not decode it.
// At the time of writing this, `dart:convert` will throw an "Unexpected end of input"
// FormatException when trying to decode an empty string.
if (response.body != null && response.statusCode != HttpStatus.noContent) {
return apiClient.deserialize(_decodeBodyBytes(response), 'String') as String; return apiClient.deserialize(_decodeBodyBytes(response), 'String') as String;
} }
return null; return null;
@ -442,8 +438,6 @@ class UserApi {
/// ///
/// Note: This method returns the HTTP [Response]. /// Note: This method returns the HTTP [Response].
Future logoutUserWithHttpInfo() async { Future logoutUserWithHttpInfo() async {
// Verify required params are set.
final path = '/user/logout'.replaceAll('{format}', 'json'); final path = '/user/logout'.replaceAll('{format}', 'json');
Object postBody; Object postBody;
@ -484,12 +478,9 @@ class UserApi {
/// Logs out current logged in user session /// Logs out current logged in user session
Future logoutUser() async { Future logoutUser() async {
final response = await logoutUserWithHttpInfo(); final response = await logoutUserWithHttpInfo();
if (response.statusCode >= 400) { if (response.statusCode >= HttpStatus.badRequest) {
throw ApiException(response.statusCode, _decodeBodyBytes(response)); throw ApiException(response.statusCode, _decodeBodyBytes(response));
} }
if (response.body != null) {
}
return;
} }
/// Updated user /// Updated user
@ -508,13 +499,14 @@ class UserApi {
Future updateUserWithHttpInfo(String username, User body) async { Future updateUserWithHttpInfo(String username, User body) async {
// Verify required params are set. // Verify required params are set.
if (username == null) { if (username == null) {
throw ApiException(400, 'Missing required param: username'); throw ApiException(HttpStatus.badRequest, 'Missing required param: username');
} }
if (body == null) { if (body == null) {
throw ApiException(400, 'Missing required param: body'); throw ApiException(HttpStatus.badRequest, 'Missing required param: body');
} }
final path = '/user/{username}'.replaceAll('{format}', 'json').replaceAll('{' + 'username' + '}', username.toString()); final path = '/user/{username}'.replaceAll('{format}', 'json')
.replaceAll('{' + 'username' + '}', username.toString());
Object postBody = body; Object postBody = body;
@ -564,11 +556,8 @@ class UserApi {
/// Updated user object /// Updated user object
Future updateUser(String username, User body) async { Future updateUser(String username, User body) async {
final response = await updateUserWithHttpInfo(username, body); final response = await updateUserWithHttpInfo(username, body);
if (response.statusCode >= 400) { if (response.statusCode >= HttpStatus.badRequest) {
throw ApiException(response.statusCode, _decodeBodyBytes(response)); throw ApiException(response.statusCode, _decodeBodyBytes(response));
} }
if (response.body != null) {
}
return;
} }
} }

View File

@ -118,16 +118,16 @@ class ApiClient {
case 'GET': return await _client.get(url, headers: nullableHeaderParams); case 'GET': return await _client.get(url, headers: nullableHeaderParams);
} }
} on SocketException catch (e, trace) { } on SocketException catch (e, trace) {
throw ApiException.withInner(400, 'Socket operation failed: $method $path', e, trace); throw ApiException.withInner(HttpStatus.badRequest, 'Socket operation failed: $method $path', e, trace);
} on TlsException catch (e, trace) { } on TlsException catch (e, trace) {
throw ApiException.withInner(400, 'TLS/SSL communication failed: $method $path', e, trace); throw ApiException.withInner(HttpStatus.badRequest, 'TLS/SSL communication failed: $method $path', e, trace);
} on IOException catch (e, trace) { } on IOException catch (e, trace) {
throw ApiException.withInner(400, 'I/O operation failed: $method $path', e, trace); throw ApiException.withInner(HttpStatus.badRequest, 'I/O operation failed: $method $path', e, trace);
} on Exception catch (e, trace) { } on Exception catch (e, trace) {
throw ApiException.withInner(400, 'Exception occurred: $method $path', e, trace); throw ApiException.withInner(HttpStatus.badRequest, 'Exception occurred: $method $path', e, trace);
} }
throw ApiException(400, 'Invalid HTTP operation: $method $path'); throw ApiException(HttpStatus.badRequest, 'Invalid HTTP operation: $method $path');
} }
dynamic _deserialize(dynamic value, String targetType, {bool growable}) { dynamic _deserialize(dynamic value, String targetType, {bool growable}) {
@ -176,9 +176,9 @@ class ApiClient {
break; break;
} }
} on Exception catch (e, stack) { } on Exception catch (e, stack) {
throw ApiException.withInner(500, 'Exception during deserialization.', e, stack); throw ApiException.withInner(HttpStatus.internalServerError, 'Exception during deserialization.', e, stack);
} }
throw ApiException(500, 'Could not find a suitable class for deserialization'); throw ApiException(HttpStatus.internalServerError, 'Could not find a suitable class for deserialization');
} }
/// Update query and header parameters based on authentication settings. /// Update query and header parameters based on authentication settings.

View File

@ -55,7 +55,7 @@ String parameterToString(dynamic value) {
/// content type. Otherwise, returns the decoded body as decoded by dart:http package. /// content type. Otherwise, returns the decoded body as decoded by dart:http package.
String _decodeBodyBytes(Response response) { String _decodeBodyBytes(Response response) {
final contentType = response.headers['content-type']; final contentType = response.headers['content-type'];
return contentType != null && contentType.contains('application/json') return contentType != null && contentType.toLowerCase().startsWith('application/json')
? utf8.decode(response.bodyBytes) ? response.bodyBytes == null ? null : utf8.decode(response.bodyBytes)
: response.body; : response.body;
} }