2020-04-25 13:22:19 +02:00
|
|
|
import 'dart:convert';
|
|
|
|
|
|
|
|
|
|
import 'package:http/http.dart' as http;
|
|
|
|
|
import 'package:musicus_database/musicus_database.dart';
|
|
|
|
|
|
|
|
|
|
/// A simple http client for the Musicus server.
|
|
|
|
|
class MusicusClient {
|
|
|
|
|
/// The URL of the Musicus server to connect to.
|
|
|
|
|
final String host;
|
|
|
|
|
|
|
|
|
|
final _client = http.Client();
|
|
|
|
|
|
|
|
|
|
MusicusClient(this.host);
|
|
|
|
|
|
|
|
|
|
/// Get a list of all available persons.
|
|
|
|
|
Future<List<Person>> getPersons() async {
|
|
|
|
|
final response = await _client.get('$host/persons');
|
|
|
|
|
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('$host/persons/$id');
|
|
|
|
|
final json = jsonDecode(response.body);
|
|
|
|
|
return Person.fromJson(json);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Create or update a person.
|
|
|
|
|
Future<void> putPerson(Person person) async {
|
|
|
|
|
await _client.put(
|
|
|
|
|
'$host/persons/${person.id}',
|
|
|
|
|
headers: {'Content-Type': 'application/json'},
|
|
|
|
|
body: jsonEncode(person.toJson()),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Get a list of all available instruments.
|
|
|
|
|
Future<List<Instrument>> getInstruments() async {
|
|
|
|
|
final response = await _client.get('$host/instruments');
|
|
|
|
|
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('$host/instruments/$id');
|
|
|
|
|
final json = jsonDecode(response.body);
|
|
|
|
|
return Instrument.fromJson(json);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Create or update an instrument.
|
|
|
|
|
Future<void> putInstrument(Instrument instrument) async {
|
|
|
|
|
await _client.put(
|
|
|
|
|
'$host/instruments/${instrument.id}',
|
|
|
|
|
headers: {'Content-Type': 'application/json'},
|
|
|
|
|
body: jsonEncode(instrument.toJson()),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Get all works composed by the person with the ID [personId].
|
2020-04-25 17:30:37 +02:00
|
|
|
Future<List<WorkInfo>> getWorks(int personId) async {
|
2020-04-25 13:22:19 +02:00
|
|
|
final response = await _client.get('$host/persons/$personId/works');
|
|
|
|
|
final json = jsonDecode(response.body);
|
2020-04-25 17:30:37 +02:00
|
|
|
return json.map<WorkInfo>((j) => WorkInfo.fromJson(j)).toList();
|
2020-04-25 13:22:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Get a work by ID.
|
2020-04-25 17:30:37 +02:00
|
|
|
Future<WorkInfo> getWork(int id) async {
|
2020-04-25 13:22:19 +02:00
|
|
|
final response = await _client.get('$host/works/$id');
|
|
|
|
|
final json = jsonDecode(response.body);
|
2020-04-25 17:30:37 +02:00
|
|
|
return WorkInfo.fromJson(json);
|
2020-04-25 13:22:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Get all recordings of the work with the ID [workId].
|
2020-04-25 17:30:37 +02:00
|
|
|
Future<List<RecordingInfo>> getRecordings(int workId) async {
|
2020-04-25 13:22:19 +02:00
|
|
|
final response = await _client.get('$host/works/$workId/recordings');
|
|
|
|
|
final json = jsonDecode(response.body);
|
2020-04-25 17:30:37 +02:00
|
|
|
return json.map<RecordingInfo>((j) => RecordingInfo.fromJson(j)).toList();
|
2020-04-25 13:22:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Create or update a work.
|
2020-04-26 16:48:05 +02:00
|
|
|
///
|
2020-04-25 17:30:37 +02:00
|
|
|
/// The new or updated work is returned.
|
2020-04-26 16:48:05 +02:00
|
|
|
Future<void> putWork(WorkInfo workInfo) async {
|
2020-04-26 16:53:48 +02:00
|
|
|
await _client.put(
|
2020-04-26 16:48:05 +02:00
|
|
|
'$host/works/${workInfo.work.id}',
|
2020-04-25 13:22:19 +02:00
|
|
|
headers: {'Content-Type': 'application/json'},
|
2020-04-26 16:48:05 +02:00
|
|
|
body: jsonEncode(workInfo.toJson()),
|
2020-04-25 13:22:19 +02:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Get a list of all ensembles.
|
|
|
|
|
Future<List<Ensemble>> getEnsembles() async {
|
|
|
|
|
final response = await _client.get('$host/ensembles');
|
|
|
|
|
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('$host/ensembles/$id');
|
|
|
|
|
final json = jsonDecode(response.body);
|
|
|
|
|
return Ensemble.fromJson(json);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Create or update an ensemble.
|
|
|
|
|
Future<void> putEnsemble(Ensemble ensemble) async {
|
|
|
|
|
await _client.put(
|
|
|
|
|
'$host/ensembles/${ensemble.id}',
|
|
|
|
|
headers: {'Content-Type': 'application/json'},
|
|
|
|
|
body: jsonEncode(ensemble.toJson()),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Get a recording by ID.
|
2020-04-25 17:30:37 +02:00
|
|
|
Future<RecordingInfo> getRecording(int id) async {
|
2020-04-25 13:22:19 +02:00
|
|
|
final response = await _client.get('$host/recordings/$id');
|
|
|
|
|
final json = jsonDecode(response.body);
|
2020-04-25 17:30:37 +02:00
|
|
|
return RecordingInfo.fromJson(json);
|
2020-04-25 13:22:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Create or update a recording.
|
2020-04-26 16:48:05 +02:00
|
|
|
Future<void> putRecording(RecordingInfo recordingInfo) async {
|
|
|
|
|
await _client.put(
|
|
|
|
|
'$host/recordings/${recordingInfo.recording.id}',
|
2020-04-25 13:22:19 +02:00
|
|
|
headers: {'Content-Type': 'application/json'},
|
2020-04-26 16:48:05 +02:00
|
|
|
body: jsonEncode(recordingInfo.toJson()),
|
2020-04-25 13:22:19 +02:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Close the internal http client.
|
|
|
|
|
void dispose() {
|
|
|
|
|
_client.close();
|
|
|
|
|
}
|
|
|
|
|
}
|