musicus_mobile/client/lib/src/client.dart

398 lines
9.7 KiB
Dart
Raw Normal View History

2020-04-25 13:22:19 +02:00
import 'dart:convert';
import 'dart:io';
2020-04-25 13:22:19 +02:00
import 'package:http/http.dart' as http;
import 'package:meta/meta.dart';
2020-04-25 13:22:19 +02:00
import 'package:musicus_database/musicus_database.dart';
/// A simple http client for the Musicus server.
class MusicusClient {
/// URI scheme to use for the connection.
///
/// This will be used as the scheme parameter when creating Uri objects.
final String scheme;
/// The host name of the Musicus server to connect to.
///
/// This will be used as the host parameter when creating Uri objects.
2020-04-25 13:22:19 +02:00
final String host;
/// This will be used as the port parameter when creating Uri objects.
final int port;
2020-04-25 13:22:19 +02:00
final _client = http.Client();
MusicusClient({
this.scheme = 'https',
this.port = 443,
@required this.host,
}) : assert(scheme != null),
assert(port != null),
assert(host != null);
/// Get a list of persons.
///
/// You can get another page using the [page] parameter. If a non empty
/// [search] string is provided, the persons will get filtered based on that
/// string.
Future<List<Person>> getPersons([int page, String search]) async {
final params = <String, String>{};
if (page != null) {
params['p'] = page.toString();
}
if (search != null) {
params['s'] = search;
}
final response = await _client.get(Uri(
scheme: scheme,
host: host,
port: port,
path: '/persons',
queryParameters: params,
));
2020-04-25 13:22:19 +02:00
final json = jsonDecode(response.body);
return json.map<Person>((j) => Person.fromJson(j)).toList();
}
/// Get a person by ID.
Future<Person> getPerson(int id) async {
final response = await _client.get(Uri(
scheme: scheme,
host: host,
port: port,
path: '/persons/$id',
));
2020-04-25 13:22:19 +02:00
final json = jsonDecode(response.body);
return Person.fromJson(json);
}
/// Delete a person by ID.
Future<void> deletePerson(int id) async {
await _client.delete(Uri(
scheme: scheme,
host: host,
port: port,
path: '/persons/$id',
));
}
2020-04-25 13:22:19 +02:00
/// Create or update a person.
///
/// Returns true, if the operation was successful.
Future<bool> putPerson(Person person) async {
try {
final response = await _client.put(
Uri(
scheme: scheme,
host: host,
port: port,
path: '/persons/${person.id}',
),
headers: {'Content-Type': 'application/json'},
body: jsonEncode(person.toJson()),
);
return response.statusCode == HttpStatus.ok;
} on Exception {
return false;
}
2020-04-25 13:22:19 +02:00
}
/// Get a list of instruments.
///
/// You can get another page using the [page] parameter. If a non empty
/// [search] string is provided, the results will get filtered based on that
/// string.
Future<List<Instrument>> getInstruments([int page, String search]) async {
final params = <String, String>{};
if (page != null) {
params['p'] = page.toString();
}
if (search != null) {
params['s'] = search;
}
final response = await _client.get(Uri(
scheme: scheme,
host: host,
port: port,
path: '/instruments',
queryParameters: params,
));
2020-04-25 13:22:19 +02:00
final json = jsonDecode(response.body);
return json.map<Instrument>((j) => Instrument.fromJson(j)).toList();
}
/// Get an instrument by ID.
Future<Instrument> getInstrument(int id) async {
final response = await _client.get(Uri(
scheme: scheme,
host: host,
port: port,
path: '/instruments/$id',
));
2020-04-25 13:22:19 +02:00
final json = jsonDecode(response.body);
return Instrument.fromJson(json);
}
/// Create or update an instrument.
///
/// Returns true, if the operation was successful.
Future<bool> putInstrument(Instrument instrument) async {
try {
final response = await _client.put(
Uri(
scheme: scheme,
host: host,
port: port,
path: '/instruments/${instrument.id}',
),
headers: {'Content-Type': 'application/json'},
body: jsonEncode(instrument.toJson()),
);
return response.statusCode == HttpStatus.ok;
} on Exception {
return false;
}
2020-04-25 13:22:19 +02:00
}
/// Delete an instrument by ID.
Future<void> deleteInstrument(int id) async {
await _client.delete(Uri(
scheme: scheme,
host: host,
port: port,
path: '/instruments/$id',
));
}
/// Get a list of works written by the person with the ID [personId].
///
/// You can get another page using the [page] parameter. If a non empty
/// [search] string is provided, the results will get filtered based on that
/// string.
Future<List<WorkInfo>> getWorks(int personId,
[int page, String search]) async {
final params = <String, String>{};
if (page != null) {
params['p'] = page.toString();
}
if (search != null) {
params['s'] = search;
}
final response = await _client.get(Uri(
scheme: scheme,
host: host,
port: port,
path: '/persons/$personId/works',
queryParameters: params,
));
2020-04-25 13:22:19 +02:00
final json = jsonDecode(response.body);
return json.map<WorkInfo>((j) => WorkInfo.fromJson(j)).toList();
2020-04-25 13:22:19 +02:00
}
/// Get a work by ID.
Future<WorkInfo> getWork(int id) async {
final response = await _client.get(Uri(
scheme: scheme,
host: host,
port: port,
path: '/works/$id',
));
2020-04-25 13:22:19 +02:00
final json = jsonDecode(response.body);
return WorkInfo.fromJson(json);
2020-04-25 13:22:19 +02:00
}
/// Delete a work by ID.
Future<void> deleteWork(int id) async {
await _client.delete(Uri(
scheme: scheme,
host: host,
port: port,
path: '/works/$id',
));
}
/// Get a list of recordings of the work with the ID [workId].
///
/// You can get another page using the [page] parameter.
Future<List<RecordingInfo>> getRecordings(int workId, [int page]) async {
final params = <String, String>{};
if (page != null) {
params['p'] = page.toString();
}
final response = await _client.get(Uri(
scheme: scheme,
host: host,
port: port,
path: '/works/$workId/recordings',
queryParameters: params,
));
2020-04-25 13:22:19 +02:00
final json = jsonDecode(response.body);
return json.map<RecordingInfo>((j) => RecordingInfo.fromJson(j)).toList();
2020-04-25 13:22:19 +02:00
}
/// Create or update a work.
///
/// Returns true, if the operation was successful.
Future<bool> putWork(WorkInfo workInfo) async {
try {
final response = await _client.put(
Uri(
scheme: scheme,
host: host,
port: port,
path: '/works/${workInfo.work.id}',
),
headers: {'Content-Type': 'application/json'},
body: jsonEncode(workInfo.toJson()),
);
return response.statusCode == HttpStatus.ok;
} on Exception {
return false;
}
2020-04-25 13:22:19 +02:00
}
/// Get a list of ensembles.
///
/// You can get another page using the [page] parameter. If a non empty
/// [search] string is provided, the results will get filtered based on that
/// string.
Future<List<Ensemble>> getEnsembles([int page, String search]) async {
final params = <String, String>{};
if (page != null) {
params['p'] = page.toString();
}
if (search != null) {
params['s'] = search;
}
final response = await _client.get(Uri(
scheme: scheme,
host: host,
port: port,
path: '/ensembles',
queryParameters: params,
));
2020-04-25 13:22:19 +02:00
final json = jsonDecode(response.body);
return json.map<Ensemble>((j) => Ensemble.fromJson(j)).toList();
}
/// Get an ensemble by ID.
Future<Ensemble> getEnsemble(int id) async {
final response = await _client.get(Uri(
scheme: scheme,
host: host,
port: port,
path: '/ensembles/$id',
));
2020-04-25 13:22:19 +02:00
final json = jsonDecode(response.body);
return Ensemble.fromJson(json);
}
/// Create or update an ensemble.
///
/// Returns true, if the operation was successful.
Future<bool> putEnsemble(Ensemble ensemble) async {
try {
final response = await _client.put(
Uri(
scheme: scheme,
host: host,
port: port,
path: '/ensembles/${ensemble.id}',
),
headers: {'Content-Type': 'application/json'},
body: jsonEncode(ensemble.toJson()),
);
return response.statusCode == HttpStatus.ok;
} on Exception {
return false;
}
2020-04-25 13:22:19 +02:00
}
/// Delete an ensemble by ID.
Future<void> deleteEnsemble(int id) async {
await _client.delete(Uri(
scheme: scheme,
host: host,
port: port,
path: '/ensembles/$id',
));
}
2020-04-25 13:22:19 +02:00
/// Get a recording by ID.
Future<RecordingInfo> getRecording(int id) async {
final response = await _client.get(Uri(
scheme: scheme,
host: host,
port: port,
path: '/recordings/$id',
));
2020-04-25 13:22:19 +02:00
final json = jsonDecode(response.body);
return RecordingInfo.fromJson(json);
2020-04-25 13:22:19 +02:00
}
/// Create or update a recording.
///
/// Returns true, if the operation was successful.
Future<bool> putRecording(RecordingInfo recordingInfo) async {
try {
final response = await _client.put(
Uri(
scheme: scheme,
host: host,
port: port,
path: '/recordings/${recordingInfo.recording.id}',
),
headers: {'Content-Type': 'application/json'},
body: jsonEncode(recordingInfo.toJson()),
);
return response.statusCode == HttpStatus.ok;
} on Exception {
return false;
}
2020-04-25 13:22:19 +02:00
}
/// Delete a recording by ID.
Future<void> deleteRecording(int id) async {
await _client.delete(Uri(
scheme: scheme,
host: host,
port: port,
path: '/recordings/$id',
));
}
2020-04-25 13:22:19 +02:00
/// Close the internal http client.
void dispose() {
_client.close();
}
}