API base requests

This commit is contained in:
Daniel Borges 2019-08-14 16:24:58 -03:00
parent 441d407005
commit ea128b0866
4 changed files with 206 additions and 0 deletions

View 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'];
}

View 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
View 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...
}
}

View File

@ -37,6 +37,7 @@ dependencies:
bloc: ^0.14.0 bloc: ^0.14.0
flutter_bloc: ^0.20.0 flutter_bloc: ^0.20.0
equatable: ^0.4.0 equatable: ^0.4.0
http: ^0.12.0+2
dev_dependencies: dev_dependencies:
flutter_test: flutter_test: