mirror of
https://gitlab.com/openlp/openlp-mobile-remote.git
synced 2024-12-22 20:02:53 +00:00
API base requests
This commit is contained in:
parent
441d407005
commit
ea128b0866
34
lib/src/models/poll_state.dart
Normal file
34
lib/src/models/poll_state.dart
Normal file
@ -0,0 +1,34 @@
|
||||
// OpenLP Mobile Remote
|
||||
// ---------------------------------------------------------------------------
|
||||
// Copyright (c) 2008-2019 OpenLP Developers
|
||||
// ---------------------------------------------------------------------------
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a
|
||||
// copy of this software and associated documentation files (the "Software"),
|
||||
// to deal in the Software without restriction, including without limitation
|
||||
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
||||
// and/or sell copies of the Software, and to permit persons to whom the
|
||||
// Software is furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
// DEALINGS IN THE SOFTWARE.
|
||||
|
||||
class PollState {
|
||||
final String chordNotation;
|
||||
final int serviceItemCount;
|
||||
final int slideIndex;
|
||||
final String currentItem;
|
||||
|
||||
PollState.fromJson(Map<String, dynamic> json)
|
||||
: serviceItemCount = json['service'],
|
||||
slideIndex = json['slide'],
|
||||
currentItem = json['item'],
|
||||
chordNotation = json['chordNotation'];
|
||||
}
|
32
lib/src/models/search_result.dart
Normal file
32
lib/src/models/search_result.dart
Normal file
@ -0,0 +1,32 @@
|
||||
// OpenLP Mobile Remote
|
||||
// ---------------------------------------------------------------------------
|
||||
// Copyright (c) 2008-2019 OpenLP Developers
|
||||
// ---------------------------------------------------------------------------
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a
|
||||
// copy of this software and associated documentation files (the "Software"),
|
||||
// to deal in the Software without restriction, including without limitation
|
||||
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
||||
// and/or sell copies of the Software, and to permit persons to whom the
|
||||
// Software is furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
// DEALINGS IN THE SOFTWARE.
|
||||
|
||||
class SearchResult {
|
||||
final int id;
|
||||
final String text;
|
||||
|
||||
SearchResult({this.id, this.text});
|
||||
|
||||
SearchResult.fromJson(List<dynamic> json)
|
||||
: id = json[0],
|
||||
text = json[1];
|
||||
}
|
139
lib/src/network/api.dart
Normal file
139
lib/src/network/api.dart
Normal file
@ -0,0 +1,139 @@
|
||||
// OpenLP Mobile Remote
|
||||
// ---------------------------------------------------------------------------
|
||||
// Copyright (c) 2008-2019 OpenLP Developers
|
||||
// ---------------------------------------------------------------------------
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a
|
||||
// copy of this software and associated documentation files (the "Software"),
|
||||
// to deal in the Software without restriction, including without limitation
|
||||
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
||||
// and/or sell copies of the Software, and to permit persons to whom the
|
||||
// Software is furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
// DEALINGS IN THE SOFTWARE.
|
||||
|
||||
import 'dart:convert' as convert;
|
||||
|
||||
import 'package:http/http.dart' as http;
|
||||
|
||||
import '../models/plugin.dart';
|
||||
import '../models/poll_state.dart';
|
||||
import '../models/search_result.dart';
|
||||
|
||||
Map<String, String> _headers() {
|
||||
final needsAuth = false;
|
||||
final username = 'openlp';
|
||||
final password = 'password';
|
||||
|
||||
Map<String, String> headers = {};
|
||||
if (needsAuth) {
|
||||
headers['Authorization'] =
|
||||
'Basic ${convert.base64.encode('$username:$password'.codeUnits)}';
|
||||
}
|
||||
return headers;
|
||||
}
|
||||
|
||||
Uri _uri(String path, {Map<String, String> queryParams}) {
|
||||
final useHttps = false;
|
||||
final address = '192.168.0.184';
|
||||
final port = 4316;
|
||||
|
||||
return Uri(
|
||||
scheme: useHttps ? 'https' : 'http',
|
||||
host: address,
|
||||
port: port,
|
||||
path: path,
|
||||
queryParameters: queryParams,
|
||||
);
|
||||
}
|
||||
|
||||
Future<http.Response> _get(String path,
|
||||
{Map<String, String> queryParams}) async {
|
||||
Uri uri = _uri(path, queryParams: queryParams);
|
||||
final response = await http.get(uri, headers: _headers());
|
||||
return response;
|
||||
}
|
||||
|
||||
Future<PollState> pollState() async {
|
||||
final response = await _get('api/poll');
|
||||
if (response.statusCode != 200) {
|
||||
return null;
|
||||
}
|
||||
final jsonResponse = convert.jsonDecode(response.body);
|
||||
return PollState.fromJson(jsonResponse['results']);
|
||||
}
|
||||
|
||||
Future<List<SearchResult>> search(Plugin plugin, String query) async {
|
||||
final response = await _get('api/${plugin.id}/search', queryParams: {
|
||||
'data': '{"request":{"text":"$query"}}',
|
||||
});
|
||||
if (response.statusCode != 200) {
|
||||
return [];
|
||||
}
|
||||
final jsonResponse = convert.jsonDecode(response.body);
|
||||
return (jsonResponse['results']['items'] as List)
|
||||
.map((i) => SearchResult.fromJson(i))
|
||||
.toList();
|
||||
}
|
||||
|
||||
Future<void> projectItem(Plugin plugin, String itemId) async {
|
||||
final response = await _get('api/${plugin.id}/live', queryParams: {
|
||||
'data': '{"request": {"id": $itemId}}',
|
||||
});
|
||||
if (response.statusCode != 200) {
|
||||
// Do some stuff...
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> addItemToService(Plugin plugin, String itemId) async {
|
||||
final response = await _get('api/${plugin.id}/add', queryParams: {
|
||||
'data': '{"request": {"id": $itemId}}',
|
||||
});
|
||||
if (response.statusCode != 200) {
|
||||
// Do some stuff...
|
||||
}
|
||||
}
|
||||
|
||||
Future<bool> alert(String text) async {
|
||||
final response = await _get('api/alert', queryParams: {
|
||||
'data': '{"request": {"text": "$text"}}',
|
||||
});
|
||||
if (response.statusCode != 200) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
Future<bool> changeDisplayState(String state) async {
|
||||
final response = await _get('api/display/$state');
|
||||
if (response.statusCode != 200) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
Future<void> setServiceItem(int itemIndex) async {
|
||||
final response = await _get('api/service/set', queryParams: {
|
||||
'data': '{"request": {"id": $itemIndex}}',
|
||||
});
|
||||
if (response.statusCode != 200) {
|
||||
// Do some stuff...
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> setLiveSlide(int slideIndex) async {
|
||||
final response = await _get('api/controller/live/set', queryParams: {
|
||||
'data': '{"request": {"id": $slideIndex}}',
|
||||
});
|
||||
if (response.statusCode != 200) {
|
||||
// Do some stuff...
|
||||
}
|
||||
}
|
@ -37,6 +37,7 @@ dependencies:
|
||||
bloc: ^0.14.0
|
||||
flutter_bloc: ^0.20.0
|
||||
equatable: ^0.4.0
|
||||
http: ^0.12.0+2
|
||||
|
||||
dev_dependencies:
|
||||
flutter_test:
|
||||
|
Loading…
Reference in New Issue
Block a user