Skip to content

Commit 9a172c1

Browse files
committed
Added EmblaRESTAPI class w. fn to clear user data
1 parent 0b59974 commit 9a172c1

File tree

2 files changed

+89
-0
lines changed

2 files changed

+89
-0
lines changed

lib/embla_core.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,6 @@ library embla_core;
2424
export 'package:embla_core/src/session.dart' show EmblaSession, EmblaSessionState;
2525
export 'package:embla_core/src/config.dart' show EmblaSessionConfig;
2626
export 'package:embla_core/src/speech.dart' show EmblaSpeechSynthesizer;
27+
export 'package:embla_core/src/api.dart' show EmblaRESTAPI;
2728
export 'package:embla_core/src/audio.dart' show AudioPlayer;
2829
export 'package:embla_core/src/recorder.dart' show AudioRecorder;

lib/src/api.dart

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
* This file is part of the EmblaCore Flutter package
3+
*
4+
* Copyright (c) 2023 Miðeind ehf. <[email protected]>
5+
* Original author: Sveinbjorn Thordarson
6+
*
7+
* This program is free software: you can redistribute it and/or modify
8+
* it under the terms of the GNU General Public License as published by
9+
* the Free Software Foundation, version 3.
10+
*
11+
* This program is distributed in the hope that it will be useful, but
12+
* WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14+
* General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU General Public License
17+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
18+
*/
19+
20+
/// Communication with REST API
21+
22+
import 'dart:convert' show json;
23+
24+
import 'package:http/http.dart' show Response;
25+
import 'package:http/http.dart' as http;
26+
27+
import './common.dart';
28+
29+
/// Send JSON POST request to API server.
30+
Future<Response?> _makeRequest(String apiURL, String apiKey, Map<String, dynamic> qargs,
31+
[void Function(Map? result)? handler]) async {
32+
dlog("Sending query POST request to $apiURL: ${qargs.toString()}");
33+
Response? response;
34+
try {
35+
final Map<String, String> headers = {
36+
"X-API-Key": apiKey,
37+
"Content-Type": "application/json",
38+
"Accept": "application/json"
39+
};
40+
response = await http
41+
.post(Uri.parse(apiURL), body: json.encode(qargs), headers: headers)
42+
.timeout(kRequestTimeout, onTimeout: () {
43+
handler!(null);
44+
return Response("Request timed out", 408);
45+
});
46+
} catch (e) {
47+
dlog("Error while making POST request: $e");
48+
response = null;
49+
}
50+
51+
// Handle null response
52+
if (response == null) {
53+
handler!(null);
54+
return null;
55+
}
56+
57+
// We have a valid response object
58+
dlog("Response status: ${response.statusCode}");
59+
// dlog("Response body: ${response.body}");
60+
if (handler != null) {
61+
// Parse JSON body and feed ensuing data structure to handler function
62+
dynamic arg = (response.statusCode == 200) ? json.decode(response.body) : null;
63+
// JSON response should be a dict, otherwise something's gone horribly wrong
64+
arg = (arg is Map) == false ? null : arg;
65+
handler(arg);
66+
}
67+
68+
return response;
69+
}
70+
71+
/// Static class wrapper for functions communicating with the Embla REST API.
72+
class EmblaRESTAPI {
73+
/// Send request to clear query history for a given device ID.
74+
/// Boolean [allData] param determines whether all device-specific
75+
/// data or only query history should be deleted server-side.
76+
static Future<void> clearUserData(String deviceID, String apiKey,
77+
{bool allData = false,
78+
String serverURL = kDefaultServer,
79+
void Function(Map? result)? completionHandler}) async {
80+
final Map<String, String> qargs = {
81+
'action': allData ? 'clear_all' : 'clear',
82+
'client_id': deviceID,
83+
};
84+
85+
final String apiURL = "$serverURL$kClearHistoryEndpoint";
86+
await _makeRequest(apiURL, apiKey, qargs, completionHandler);
87+
}
88+
}

0 commit comments

Comments
 (0)