diff --git a/client/.gitignore b/client/.gitignore new file mode 100644 index 0000000..a242fcb --- /dev/null +++ b/client/.gitignore @@ -0,0 +1,31 @@ +# Miscellaneous +*.class +*.log +*.pyc +*.swp +.DS_Store +.atom/ +.buildlog/ +.history +.svn/ + +# IntelliJ related +*.iml +*.ipr +*.iws +.idea/ + +# VS Code related +.vscode/ + +# Flutter/Dart/Pub related +**/*.g.dart +**/doc/api/ +.dart_tool/ +pubspec.lock +.flutter-plugins +.flutter-plugins-dependencies +.packages +.pub-cache/ +.pub/ +/build/ diff --git a/client/lib/musicus_client.dart b/client/lib/musicus_client.dart new file mode 100644 index 0000000..8084f4d --- /dev/null +++ b/client/lib/musicus_client.dart @@ -0,0 +1 @@ +export 'src/client.dart'; \ No newline at end of file diff --git a/client/lib/src/client.dart b/client/lib/src/client.dart new file mode 100644 index 0000000..dccc760 --- /dev/null +++ b/client/lib/src/client.dart @@ -0,0 +1,149 @@ +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> getPersons() async { + final response = await _client.get('$host/persons'); + final json = jsonDecode(response.body); + return json.map((j) => Person.fromJson(j)).toList(); + } + + /// Get a person by ID. + Future 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 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> getInstruments() async { + final response = await _client.get('$host/instruments'); + final json = jsonDecode(response.body); + return json.map((j) => Instrument.fromJson(j)).toList(); + } + + /// Get an instrument by ID. + Future 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 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]. + Future> getWorks(int personId) async { + final response = await _client.get('$host/persons/$personId/works'); + final json = jsonDecode(response.body); + return json.map((j) => Work.fromJson(j)).toList(); + } + + /// Get a work by ID. + Future getWork(int id) async { + final response = await _client.get('$host/works/$id'); + final json = jsonDecode(response.body); + return Work.fromJson(json); + } + + /// Get all work parts of the work with the ID [workId]. + Future> getParts(int workId) async { + final response = await _client.get('$host/works/$workId/parts'); + final json = jsonDecode(response.body); + return json.map((j) => Work.fromJson(j)).toList(); + } + + /// Get all recordings of the work with the ID [workId]. + Future> getRecordings(int workId) async { + final response = await _client.get('$host/works/$workId/recordings'); + final json = jsonDecode(response.body); + return json.map((j) => Recording.fromJson(j)).toList(); + } + + /// Create or update a work. + Future putWork(WorkData data) async { + await _client.put( + '$host/works/${data.data.work.id}', + headers: {'Content-Type': 'application/json'}, + body: jsonEncode(data.toJson()), + ); + } + + /// Get a list of all ensembles. + Future> getEnsembles() async { + final response = await _client.get('$host/ensembles'); + final json = jsonDecode(response.body); + return json.map((j) => Ensemble.fromJson(j)).toList(); + } + + /// Get an ensemble by ID. + Future 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 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. + Future getRecording(int id) async { + final response = await _client.get('$host/recordings/$id'); + final json = jsonDecode(response.body); + return Recording.fromJson(json); + } + + /// Get all performances within the recording with the ID [recordingId]. + Future> getPerformances(int recordingId) async { + final response = + await _client.get('$host/recordings/$recordingId/performances'); + final json = jsonDecode(response.body); + return json.map((j) => Performance.fromJson(j)).toList(); + } + + /// Create or update a recording. + Future putRecording(RecordingData data) async { + await _client.put( + '$host/recordings/${data.recording.id}', + headers: {'Content-Type': 'application/json'}, + body: jsonEncode(data.toJson()), + ); + } + + /// Close the internal http client. + void dispose() { + _client.close(); + } +} diff --git a/client/pubspec.yaml b/client/pubspec.yaml new file mode 100644 index 0000000..6345d70 --- /dev/null +++ b/client/pubspec.yaml @@ -0,0 +1,8 @@ +name: musicus_client +description: Client library for the Musicus server. +version: 0.0.1 + +dependencies: + http: + musicus_database: + path: ../database \ No newline at end of file