client: Support API in subfolder

This commit is contained in:
Elias Projahn 2020-05-01 16:30:14 +02:00
parent 55a8be5dbb
commit 4f7a99d2a1

View file

@ -20,16 +20,34 @@ class MusicusClient {
/// This will be used as the port parameter when creating Uri objects. /// This will be used as the port parameter when creating Uri objects.
final int port; final int port;
/// Base path to the root location of the Musicus API.
final String basePath;
final _client = http.Client(); final _client = http.Client();
MusicusClient({ MusicusClient({
this.scheme = 'https', this.scheme = 'https',
this.port = 443,
@required this.host, @required this.host,
this.port = 443,
this.basePath,
}) : assert(scheme != null), }) : assert(scheme != null),
assert(port != null), assert(port != null),
assert(host != null); assert(host != null);
/// Create an URI using member variables and parameters.
Uri createUri({
@required String path,
Map<String, String> params,
}) {
return Uri(
scheme: scheme,
host: host,
port: port,
path: basePath != null ? basePath + path : path,
queryParameters: params,
);
}
/// Get a list of persons. /// Get a list of persons.
/// ///
/// You can get another page using the [page] parameter. If a non empty /// You can get another page using the [page] parameter. If a non empty
@ -46,12 +64,9 @@ class MusicusClient {
params['s'] = search; params['s'] = search;
} }
final response = await _client.get(Uri( final response = await _client.get(createUri(
scheme: scheme,
host: host,
port: port,
path: '/persons', path: '/persons',
queryParameters: params, params: params,
)); ));
final json = jsonDecode(response.body); final json = jsonDecode(response.body);
@ -60,10 +75,7 @@ class MusicusClient {
/// Get a person by ID. /// Get a person by ID.
Future<Person> getPerson(int id) async { Future<Person> getPerson(int id) async {
final response = await _client.get(Uri( final response = await _client.get(createUri(
scheme: scheme,
host: host,
port: port,
path: '/persons/$id', path: '/persons/$id',
)); ));
@ -73,10 +85,7 @@ class MusicusClient {
/// Delete a person by ID. /// Delete a person by ID.
Future<void> deletePerson(int id) async { Future<void> deletePerson(int id) async {
await _client.delete(Uri( await _client.delete(createUri(
scheme: scheme,
host: host,
port: port,
path: '/persons/$id', path: '/persons/$id',
)); ));
} }
@ -87,10 +96,7 @@ class MusicusClient {
Future<bool> putPerson(Person person) async { Future<bool> putPerson(Person person) async {
try { try {
final response = await _client.put( final response = await _client.put(
Uri( createUri(
scheme: scheme,
host: host,
port: port,
path: '/persons/${person.id}', path: '/persons/${person.id}',
), ),
headers: {'Content-Type': 'application/json'}, headers: {'Content-Type': 'application/json'},
@ -119,12 +125,9 @@ class MusicusClient {
params['s'] = search; params['s'] = search;
} }
final response = await _client.get(Uri( final response = await _client.get(createUri(
scheme: scheme,
host: host,
port: port,
path: '/instruments', path: '/instruments',
queryParameters: params, params: params,
)); ));
final json = jsonDecode(response.body); final json = jsonDecode(response.body);
@ -133,10 +136,7 @@ class MusicusClient {
/// Get an instrument by ID. /// Get an instrument by ID.
Future<Instrument> getInstrument(int id) async { Future<Instrument> getInstrument(int id) async {
final response = await _client.get(Uri( final response = await _client.get(createUri(
scheme: scheme,
host: host,
port: port,
path: '/instruments/$id', path: '/instruments/$id',
)); ));
@ -150,10 +150,7 @@ class MusicusClient {
Future<bool> putInstrument(Instrument instrument) async { Future<bool> putInstrument(Instrument instrument) async {
try { try {
final response = await _client.put( final response = await _client.put(
Uri( createUri(
scheme: scheme,
host: host,
port: port,
path: '/instruments/${instrument.id}', path: '/instruments/${instrument.id}',
), ),
headers: {'Content-Type': 'application/json'}, headers: {'Content-Type': 'application/json'},
@ -168,10 +165,7 @@ class MusicusClient {
/// Delete an instrument by ID. /// Delete an instrument by ID.
Future<void> deleteInstrument(int id) async { Future<void> deleteInstrument(int id) async {
await _client.delete(Uri( await _client.delete(createUri(
scheme: scheme,
host: host,
port: port,
path: '/instruments/$id', path: '/instruments/$id',
)); ));
} }
@ -193,12 +187,9 @@ class MusicusClient {
params['s'] = search; params['s'] = search;
} }
final response = await _client.get(Uri( final response = await _client.get(createUri(
scheme: scheme,
host: host,
port: port,
path: '/persons/$personId/works', path: '/persons/$personId/works',
queryParameters: params, params: params,
)); ));
final json = jsonDecode(response.body); final json = jsonDecode(response.body);
@ -207,10 +198,7 @@ class MusicusClient {
/// Get a work by ID. /// Get a work by ID.
Future<WorkInfo> getWork(int id) async { Future<WorkInfo> getWork(int id) async {
final response = await _client.get(Uri( final response = await _client.get(createUri(
scheme: scheme,
host: host,
port: port,
path: '/works/$id', path: '/works/$id',
)); ));
@ -220,10 +208,7 @@ class MusicusClient {
/// Delete a work by ID. /// Delete a work by ID.
Future<void> deleteWork(int id) async { Future<void> deleteWork(int id) async {
await _client.delete(Uri( await _client.delete(createUri(
scheme: scheme,
host: host,
port: port,
path: '/works/$id', path: '/works/$id',
)); ));
} }
@ -238,12 +223,9 @@ class MusicusClient {
params['p'] = page.toString(); params['p'] = page.toString();
} }
final response = await _client.get(Uri( final response = await _client.get(createUri(
scheme: scheme,
host: host,
port: port,
path: '/works/$workId/recordings', path: '/works/$workId/recordings',
queryParameters: params, params: params,
)); ));
final json = jsonDecode(response.body); final json = jsonDecode(response.body);
@ -256,10 +238,7 @@ class MusicusClient {
Future<bool> putWork(WorkInfo workInfo) async { Future<bool> putWork(WorkInfo workInfo) async {
try { try {
final response = await _client.put( final response = await _client.put(
Uri( createUri(
scheme: scheme,
host: host,
port: port,
path: '/works/${workInfo.work.id}', path: '/works/${workInfo.work.id}',
), ),
headers: {'Content-Type': 'application/json'}, headers: {'Content-Type': 'application/json'},
@ -288,12 +267,9 @@ class MusicusClient {
params['s'] = search; params['s'] = search;
} }
final response = await _client.get(Uri( final response = await _client.get(createUri(
scheme: scheme,
host: host,
port: port,
path: '/ensembles', path: '/ensembles',
queryParameters: params, params: params,
)); ));
final json = jsonDecode(response.body); final json = jsonDecode(response.body);
@ -302,10 +278,7 @@ class MusicusClient {
/// Get an ensemble by ID. /// Get an ensemble by ID.
Future<Ensemble> getEnsemble(int id) async { Future<Ensemble> getEnsemble(int id) async {
final response = await _client.get(Uri( final response = await _client.get(createUri(
scheme: scheme,
host: host,
port: port,
path: '/ensembles/$id', path: '/ensembles/$id',
)); ));
@ -319,10 +292,7 @@ class MusicusClient {
Future<bool> putEnsemble(Ensemble ensemble) async { Future<bool> putEnsemble(Ensemble ensemble) async {
try { try {
final response = await _client.put( final response = await _client.put(
Uri( createUri(
scheme: scheme,
host: host,
port: port,
path: '/ensembles/${ensemble.id}', path: '/ensembles/${ensemble.id}',
), ),
headers: {'Content-Type': 'application/json'}, headers: {'Content-Type': 'application/json'},
@ -337,20 +307,14 @@ class MusicusClient {
/// Delete an ensemble by ID. /// Delete an ensemble by ID.
Future<void> deleteEnsemble(int id) async { Future<void> deleteEnsemble(int id) async {
await _client.delete(Uri( await _client.delete(createUri(
scheme: scheme,
host: host,
port: port,
path: '/ensembles/$id', path: '/ensembles/$id',
)); ));
} }
/// Get a recording by ID. /// Get a recording by ID.
Future<RecordingInfo> getRecording(int id) async { Future<RecordingInfo> getRecording(int id) async {
final response = await _client.get(Uri( final response = await _client.get(createUri(
scheme: scheme,
host: host,
port: port,
path: '/recordings/$id', path: '/recordings/$id',
)); ));
@ -364,10 +328,7 @@ class MusicusClient {
Future<bool> putRecording(RecordingInfo recordingInfo) async { Future<bool> putRecording(RecordingInfo recordingInfo) async {
try { try {
final response = await _client.put( final response = await _client.put(
Uri( createUri(
scheme: scheme,
host: host,
port: port,
path: '/recordings/${recordingInfo.recording.id}', path: '/recordings/${recordingInfo.recording.id}',
), ),
headers: {'Content-Type': 'application/json'}, headers: {'Content-Type': 'application/json'},
@ -382,10 +343,7 @@ class MusicusClient {
/// Delete a recording by ID. /// Delete a recording by ID.
Future<void> deleteRecording(int id) async { Future<void> deleteRecording(int id) async {
await _client.delete(Uri( await _client.delete(createUri(
scheme: scheme,
host: host,
port: port,
path: '/recordings/$id', path: '/recordings/$id',
)); ));
} }