|
1 | 1 | import 'dart:convert';
|
| 2 | +import 'dart:io'; |
| 3 | + |
2 | 4 | import 'package:http/http.dart' as http;
|
3 | 5 | import 'package:logging/logging.dart';
|
| 6 | +import 'package:shared_preferences/shared_preferences.dart'; |
| 7 | + |
| 8 | +import 'helpers/consts.dart'; |
4 | 9 |
|
5 | 10 | final log = Logger('powersync-test');
|
6 | 11 |
|
7 | 12 | class ApiClient {
|
8 | 13 | final String baseUrl;
|
9 | 14 |
|
10 |
| - ApiClient(this.baseUrl); |
| 15 | + const ApiClient(this.baseUrl); |
11 | 16 |
|
12 | 17 | Future<Map<String, dynamic>> authenticate(String username, String password) async {
|
13 | 18 | final response = await http.post(
|
14 |
| - Uri.parse('$baseUrl/api/auth/'), |
| 19 | + Uri.parse('$baseUrl/api/v2/login/'), |
15 | 20 | headers: {'Content-Type': 'application/json'},
|
16 | 21 | body: json.encode({'username': username, 'password': password}),
|
17 | 22 | );
|
18 | 23 | if (response.statusCode == 200) {
|
| 24 | + log.log(Level.ALL, response.body); |
19 | 25 | return json.decode(response.body);
|
20 |
| - } else { |
21 |
| - throw Exception('Failed to authenticate'); |
22 | 26 | }
|
| 27 | + throw Exception('Failed to authenticate'); |
23 | 28 | }
|
24 | 29 |
|
25 |
| - Future<Map<String, dynamic>> getToken(String userId) async { |
| 30 | + Future<Map<String, dynamic>> getWgerJWTToken() async { |
| 31 | + final response = await http.post( |
| 32 | + Uri.parse('$baseUrl/api/v2/token'), |
| 33 | + headers: {HttpHeaders.contentTypeHeader: 'application/json'}, |
| 34 | + body: json.encode({'username': 'admin', 'password': 'adminadmin'}), |
| 35 | + ); |
| 36 | + if (response.statusCode == 200) { |
| 37 | + log.log(Level.ALL, response.body); |
| 38 | + return json.decode(response.body); |
| 39 | + } |
| 40 | + throw Exception('Failed to fetch token'); |
| 41 | + } |
| 42 | + |
| 43 | + /// Returns a powersync JWT token token |
| 44 | + /// |
| 45 | + /// Note that at the moment we use the permanent API token for authentication |
| 46 | + /// but this should be probably changed to the wger API JWT tokens in the |
| 47 | + /// future since they are not permanent and could be easily revoked. |
| 48 | + Future<Map<String, dynamic>> getPowersyncToken() async { |
| 49 | + final prefs = await SharedPreferences.getInstance(); |
| 50 | + final apiData = json.decode(prefs.getString(PREFS_USER)!); |
| 51 | + |
26 | 52 | final response = await http.get(
|
27 |
| - Uri.parse('$baseUrl/api/get_powersync_token/'), |
28 |
| - headers: {'Content-Type': 'application/json'}, |
| 53 | + Uri.parse('$baseUrl/api/v2/powersync-token'), |
| 54 | + headers: { |
| 55 | + HttpHeaders.contentTypeHeader: 'application/json', |
| 56 | + HttpHeaders.authorizationHeader: 'Token ${apiData["token"]}', |
| 57 | + }, |
29 | 58 | );
|
30 | 59 | if (response.statusCode == 200) {
|
| 60 | + log.log(Level.ALL, response.body); |
31 | 61 | return json.decode(response.body);
|
32 |
| - } else { |
33 |
| - throw Exception('Failed to fetch token'); |
34 | 62 | }
|
| 63 | + throw Exception('Failed to fetch token'); |
35 | 64 | }
|
36 | 65 |
|
37 | 66 | Future<void> upsert(Map<String, dynamic> record) async {
|
38 | 67 | await http.put(
|
39 |
| - Uri.parse('$baseUrl/api/upload_data/'), |
| 68 | + Uri.parse('$baseUrl/api/upload-powersync-data'), |
40 | 69 | headers: {'Content-Type': 'application/json'},
|
41 | 70 | body: json.encode(record),
|
42 | 71 | );
|
43 | 72 | }
|
44 | 73 |
|
45 | 74 | Future<void> update(Map<String, dynamic> record) async {
|
46 | 75 | await http.patch(
|
47 |
| - Uri.parse('$baseUrl/api/upload_data/'), |
| 76 | + Uri.parse('$baseUrl/api/upload-powersync-data'), |
48 | 77 | headers: {'Content-Type': 'application/json'},
|
49 | 78 | body: json.encode(record),
|
50 | 79 | );
|
51 | 80 | }
|
52 | 81 |
|
53 | 82 | Future<void> delete(Map<String, dynamic> record) async {
|
54 | 83 | await http.delete(
|
55 |
| - Uri.parse('$baseUrl/api/upload_data/'), |
| 84 | + Uri.parse('$baseUrl/api/v2/upload-powersync-data'), |
56 | 85 | headers: {'Content-Type': 'application/json'},
|
57 | 86 | body: json.encode(record),
|
58 | 87 | );
|
|
0 commit comments