client: Catch exceptions in put methods

This commit is contained in:
Elias Projahn 2020-04-26 18:31:07 +02:00
parent 9d4554b67f
commit 37a1353297

View file

@ -31,13 +31,17 @@ class MusicusClient {
/// ///
/// Returns true, if the operation was successful. /// Returns true, if the operation was successful.
Future<bool> putPerson(Person person) async { Future<bool> putPerson(Person person) async {
final response = await _client.put( try {
'$host/persons/${person.id}', final response = await _client.put(
headers: {'Content-Type': 'application/json'}, '$host/persons/${person.id}',
body: jsonEncode(person.toJson()), headers: {'Content-Type': 'application/json'},
); body: jsonEncode(person.toJson()),
);
return response.statusCode == HttpStatus.ok; return response.statusCode == HttpStatus.ok;
} on Exception {
return false;
}
} }
/// Get a list of all available instruments. /// Get a list of all available instruments.
@ -58,13 +62,17 @@ class MusicusClient {
/// ///
/// Returns true, if the operation was successful. /// Returns true, if the operation was successful.
Future<bool> putInstrument(Instrument instrument) async { Future<bool> putInstrument(Instrument instrument) async {
final response = await _client.put( try {
'$host/instruments/${instrument.id}', final response = await _client.put(
headers: {'Content-Type': 'application/json'}, '$host/instruments/${instrument.id}',
body: jsonEncode(instrument.toJson()), headers: {'Content-Type': 'application/json'},
); body: jsonEncode(instrument.toJson()),
);
return response.statusCode == HttpStatus.ok; return response.statusCode == HttpStatus.ok;
} on Exception {
return false;
}
} }
/// Get all works composed by the person with the ID [personId]. /// Get all works composed by the person with the ID [personId].
@ -92,13 +100,17 @@ class MusicusClient {
/// ///
/// Returns true, if the operation was successful. /// Returns true, if the operation was successful.
Future<bool> putWork(WorkInfo workInfo) async { Future<bool> putWork(WorkInfo workInfo) async {
final response = await _client.put( try {
'$host/works/${workInfo.work.id}', final response = await _client.put(
headers: {'Content-Type': 'application/json'}, '$host/works/${workInfo.work.id}',
body: jsonEncode(workInfo.toJson()), headers: {'Content-Type': 'application/json'},
); body: jsonEncode(workInfo.toJson()),
);
return response.statusCode == HttpStatus.ok; return response.statusCode == HttpStatus.ok;
} on Exception {
return false;
}
} }
/// Get a list of all ensembles. /// Get a list of all ensembles.
@ -119,13 +131,17 @@ class MusicusClient {
/// ///
/// Returns true, if the operation was successful. /// Returns true, if the operation was successful.
Future<bool> putEnsemble(Ensemble ensemble) async { Future<bool> putEnsemble(Ensemble ensemble) async {
final response = await _client.put( try {
'$host/ensembles/${ensemble.id}', final response = await _client.put(
headers: {'Content-Type': 'application/json'}, '$host/ensembles/${ensemble.id}',
body: jsonEncode(ensemble.toJson()), headers: {'Content-Type': 'application/json'},
); body: jsonEncode(ensemble.toJson()),
);
return response.statusCode == HttpStatus.ok; return response.statusCode == HttpStatus.ok;
} on Exception {
return false;
}
} }
/// Get a recording by ID. /// Get a recording by ID.
@ -139,13 +155,17 @@ class MusicusClient {
/// ///
/// Returns true, if the operation was successful. /// Returns true, if the operation was successful.
Future<bool> putRecording(RecordingInfo recordingInfo) async { Future<bool> putRecording(RecordingInfo recordingInfo) async {
final response = await _client.put( try {
'$host/recordings/${recordingInfo.recording.id}', final response = await _client.put(
headers: {'Content-Type': 'application/json'}, '$host/recordings/${recordingInfo.recording.id}',
body: jsonEncode(recordingInfo.toJson()), headers: {'Content-Type': 'application/json'},
); body: jsonEncode(recordingInfo.toJson()),
);
return response.statusCode == HttpStatus.ok; return response.statusCode == HttpStatus.ok;
} on Exception {
return false;
}
} }
/// Close the internal http client. /// Close the internal http client.