forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathintegrations.dart
More file actions
129 lines (108 loc) · 3.73 KB
/
Copy pathintegrations.dart
File metadata and controls
129 lines (108 loc) · 3.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import 'dart:convert';
import 'package:omi/backend/http/shared.dart';
import 'package:omi/backend/schema/gen/imports_integrations_wire.g.dart' as wire;
import 'package:omi/env/env.dart';
import 'package:omi/utils/logger.dart';
/// Response model for integration
class IntegrationResponse {
final bool connected;
final String appKey;
IntegrationResponse({required this.connected, required this.appKey});
factory IntegrationResponse.fromJson(Map<String, dynamic> json) {
return IntegrationResponse.fromGenerated(wire.GeneratedIntegrationResponse.fromJson(json));
}
factory IntegrationResponse.fromGenerated(wire.GeneratedIntegrationResponse generated) {
return IntegrationResponse(connected: generated.connected, appKey: generated.appKey);
}
wire.GeneratedIntegrationResponse toGenerated() {
return wire.GeneratedIntegrationResponse(connected: connected, appKey: appKey);
}
}
/// Get integration connection status
Future<IntegrationResponse?> getIntegration(String appKey) async {
var response = await makeApiCall(
url: '${Env.apiBaseUrl}v1/integrations/$appKey',
headers: {},
method: 'GET',
body: '',
);
if (response == null) return null;
if (response.statusCode == 200) {
var body = utf8.decode(response.bodyBytes);
return IntegrationResponse.fromGenerated(
wire.GeneratedIntegrationResponse.fromJson(jsonDecode(body) as Map<String, dynamic>),
);
} else {
Logger.debug('getIntegration error ${response.statusCode}');
return null;
}
}
/// Save integration connection details
Future<bool> saveIntegration(String appKey, Map<String, dynamic> details) async {
var response = await makeApiCall(
url: '${Env.apiBaseUrl}v1/integrations/$appKey',
headers: {},
method: 'PUT',
body: jsonEncode(details),
);
if (response == null) return false;
if (response.statusCode == 200) {
return true;
} else {
Logger.debug('saveIntegration error ${response.statusCode}');
return false;
}
}
/// Delete integration connection
Future<bool> deleteIntegration(String appKey) async {
var response = await makeApiCall(
url: '${Env.apiBaseUrl}v1/integrations/$appKey',
headers: {},
method: 'DELETE',
body: '',
);
if (response == null) return false;
if (response.statusCode == 204 || response.statusCode == 200) {
return true;
} else {
Logger.debug('deleteIntegration error ${response.statusCode}');
return false;
}
}
/// Get OAuth URL for an integration
Future<String?> getIntegrationOAuthUrl(String appKey) async {
var response = await makeApiCall(
url: '${Env.apiBaseUrl}v1/integrations/$appKey/oauth-url',
headers: {},
method: 'GET',
body: '',
);
if (response == null) return null;
if (response.statusCode == 200) {
var body = utf8.decode(response.bodyBytes);
final data = wire.GeneratedOAuthUrlResponse.fromJson(jsonDecode(body) as Map<String, dynamic>);
return data.authUrl;
} else {
Logger.debug('getIntegrationOAuthUrl error ${response.statusCode}');
return null;
}
}
/// Sync Apple Health data to backend
/// This sends health data collected from HealthKit to the server
Future<bool> syncAppleHealthData(Map<String, dynamic> healthData) async {
var response = await makeApiCall(
url: '${Env.apiBaseUrl}v1/integrations/apple-health/sync',
headers: {},
method: 'PUT',
body: jsonEncode(healthData),
);
if (response == null) return false;
if (response.statusCode == 200) {
final data = wire.GeneratedAppleHealthSyncResponse.fromJson(jsonDecode(response.body) as Map<String, dynamic>);
Logger.debug('Apple Health data synced successfully');
return data.status == 'ok';
} else {
Logger.debug('syncAppleHealthData error ${response.statusCode}');
return false;
}
}