|
1 | | -using PowerSync.Common.Client; |
2 | | -using PowerSync.Common.Client.Connection; |
3 | | -using PowerSync.Common.DB.Crud; |
4 | | - |
5 | | -namespace TodoSQLite.Data; |
6 | | - |
7 | | -using System; |
8 | | -using System.Collections.Generic; |
9 | | -using System.Net.Http; |
10 | | -using System.Text; |
11 | | -using System.Text.Json; |
12 | | -using System.Threading.Tasks; |
13 | | - |
14 | | -public class NodeConnector : IPowerSyncBackendConnector |
15 | | -{ |
16 | | - private static readonly string StorageFilePath = "user_id.txt"; // Simulating local storage |
17 | | - private readonly HttpClient _httpClient; |
18 | | - |
19 | | - public string BackendUrl { get; } |
20 | | - public string PowerSyncUrl { get; } |
21 | | - public string UserId { get; private set; } |
22 | | - private string? clientId; |
23 | | - |
24 | | - public NodeConnector() |
25 | | - { |
26 | | - _httpClient = new HttpClient(); |
27 | | - |
28 | | - // Load or generate User ID |
29 | | - UserId = LoadOrGenerateUserId(); |
30 | | - |
31 | | - BackendUrl = "http://localhost:6060"; |
32 | | - PowerSyncUrl = "http://localhost:8080"; |
33 | | - |
34 | | - clientId = null; |
35 | | - } |
36 | | - |
37 | | - public string LoadOrGenerateUserId() |
38 | | - { |
39 | | - if (File.Exists(StorageFilePath)) |
40 | | - { |
41 | | - return File.ReadAllText(StorageFilePath); |
42 | | - } |
43 | | - |
44 | | - string newUserId = Guid.NewGuid().ToString(); |
45 | | - File.WriteAllText(StorageFilePath, newUserId); |
46 | | - return newUserId; |
47 | | - } |
48 | | - |
49 | | - public async Task<PowerSyncCredentials?> FetchCredentials() |
50 | | - { |
51 | | - string tokenEndpoint = "api/auth/token"; |
52 | | - string url = $"{BackendUrl}/{tokenEndpoint}?user_id={UserId}"; |
53 | | - |
54 | | - HttpResponseMessage response = await _httpClient.GetAsync(url); |
55 | | - if (!response.IsSuccessStatusCode) |
56 | | - { |
57 | | - throw new Exception($"Received {response.StatusCode} from {tokenEndpoint}: {await response.Content.ReadAsStringAsync()}"); |
58 | | - } |
59 | | - |
60 | | - string responseBody = await response.Content.ReadAsStringAsync(); |
61 | | - var jsonResponse = JsonSerializer.Deserialize<Dictionary<string, string>>(responseBody); |
62 | | - |
63 | | - if (jsonResponse == null || !jsonResponse.ContainsKey("token")) |
64 | | - { |
65 | | - throw new Exception("Invalid response received from authentication endpoint."); |
66 | | - } |
67 | | - |
68 | | - return new PowerSyncCredentials(PowerSyncUrl, jsonResponse["token"]); |
69 | | - } |
70 | | - |
71 | | - public async Task UploadData(IPowerSyncDatabase database) |
72 | | - { |
73 | | - CrudTransaction? transaction; |
74 | | - try |
75 | | - { |
76 | | - transaction = await database.GetNextCrudTransaction(); |
77 | | - } |
78 | | - catch (Exception ex) |
79 | | - { |
80 | | - Console.WriteLine($"UploadData Error: {ex.Message}"); |
81 | | - return; |
82 | | - } |
83 | | - |
84 | | - if (transaction == null) |
85 | | - { |
86 | | - return; |
87 | | - } |
88 | | - |
89 | | - clientId ??= await database.GetClientId(); |
90 | | - |
91 | | - try |
92 | | - { |
93 | | - var batch = new List<object>(); |
94 | | - |
95 | | - foreach (var operation in transaction.Crud) |
96 | | - { |
97 | | - batch.Add(new |
98 | | - { |
99 | | - op = operation.Op.ToString(), |
100 | | - table = operation.Table, |
101 | | - id = operation.Id, |
102 | | - data = operation.OpData |
103 | | - }); |
104 | | - } |
105 | | - |
106 | | - var payload = JsonSerializer.Serialize(new { batch }); |
107 | | - var content = new StringContent(payload, Encoding.UTF8, "application/json"); |
108 | | - |
109 | | - HttpResponseMessage response = await _httpClient.PostAsync($"{BackendUrl}/api/data", content); |
110 | | - |
111 | | - if (!response.IsSuccessStatusCode) |
112 | | - { |
113 | | - throw new Exception($"Received {response.StatusCode} from /api/data: {await response.Content.ReadAsStringAsync()}"); |
114 | | - } |
115 | | - |
116 | | - await transaction.Complete(); |
117 | | - } |
118 | | - catch (Exception ex) |
119 | | - { |
120 | | - Console.WriteLine($"UploadData Error: {ex.Message}"); |
121 | | - throw; |
122 | | - } |
123 | | - } |
124 | | -} |
| 1 | +using PowerSync.Common.Client; |
| 2 | +using PowerSync.Common.Client.Connection; |
| 3 | +using PowerSync.Common.DB.Crud; |
| 4 | + |
| 5 | +namespace MAUITodo.Data; |
| 6 | + |
| 7 | +using System; |
| 8 | +using System.Collections.Generic; |
| 9 | +using System.Net.Http; |
| 10 | +using System.Text; |
| 11 | +using System.Text.Json; |
| 12 | +using System.Threading.Tasks; |
| 13 | + |
| 14 | +public class NodeConnector : IPowerSyncBackendConnector |
| 15 | +{ |
| 16 | + private static readonly string StorageFilePath = "user_id.txt"; // Simulating local storage |
| 17 | + private readonly HttpClient _httpClient; |
| 18 | + |
| 19 | + public string BackendUrl { get; } |
| 20 | + public string PowerSyncUrl { get; } |
| 21 | + public string UserId { get; private set; } |
| 22 | + private string? clientId; |
| 23 | + |
| 24 | + public NodeConnector() |
| 25 | + { |
| 26 | + _httpClient = new HttpClient(); |
| 27 | + |
| 28 | + // Load or generate User ID |
| 29 | + UserId = LoadOrGenerateUserId(); |
| 30 | + |
| 31 | + BackendUrl = "http://localhost:6060"; |
| 32 | + PowerSyncUrl = "http://localhost:8080"; |
| 33 | + |
| 34 | + clientId = null; |
| 35 | + } |
| 36 | + |
| 37 | + public string LoadOrGenerateUserId() |
| 38 | + { |
| 39 | + if (File.Exists(StorageFilePath)) |
| 40 | + { |
| 41 | + return File.ReadAllText(StorageFilePath); |
| 42 | + } |
| 43 | + |
| 44 | + string newUserId = Guid.NewGuid().ToString(); |
| 45 | + File.WriteAllText(StorageFilePath, newUserId); |
| 46 | + return newUserId; |
| 47 | + } |
| 48 | + |
| 49 | + public async Task<PowerSyncCredentials?> FetchCredentials() |
| 50 | + { |
| 51 | + string tokenEndpoint = "api/auth/token"; |
| 52 | + string url = $"{BackendUrl}/{tokenEndpoint}?user_id={UserId}"; |
| 53 | + |
| 54 | + HttpResponseMessage response = await _httpClient.GetAsync(url); |
| 55 | + if (!response.IsSuccessStatusCode) |
| 56 | + { |
| 57 | + throw new Exception($"Received {response.StatusCode} from {tokenEndpoint}: {await response.Content.ReadAsStringAsync()}"); |
| 58 | + } |
| 59 | + |
| 60 | + string responseBody = await response.Content.ReadAsStringAsync(); |
| 61 | + var jsonResponse = JsonSerializer.Deserialize<Dictionary<string, string>>(responseBody); |
| 62 | + |
| 63 | + if (jsonResponse == null || !jsonResponse.ContainsKey("token")) |
| 64 | + { |
| 65 | + throw new Exception("Invalid response received from authentication endpoint."); |
| 66 | + } |
| 67 | + |
| 68 | + return new PowerSyncCredentials(PowerSyncUrl, jsonResponse["token"]); |
| 69 | + } |
| 70 | + |
| 71 | + public async Task UploadData(IPowerSyncDatabase database) |
| 72 | + { |
| 73 | + CrudTransaction? transaction; |
| 74 | + try |
| 75 | + { |
| 76 | + transaction = await database.GetNextCrudTransaction(); |
| 77 | + } |
| 78 | + catch (Exception ex) |
| 79 | + { |
| 80 | + Console.WriteLine($"UploadData Error: {ex.Message}"); |
| 81 | + return; |
| 82 | + } |
| 83 | + |
| 84 | + if (transaction == null) |
| 85 | + { |
| 86 | + return; |
| 87 | + } |
| 88 | + |
| 89 | + clientId ??= await database.GetClientId(); |
| 90 | + |
| 91 | + try |
| 92 | + { |
| 93 | + var batch = new List<object>(); |
| 94 | + |
| 95 | + foreach (var operation in transaction.Crud) |
| 96 | + { |
| 97 | + batch.Add(new |
| 98 | + { |
| 99 | + op = operation.Op.ToString(), |
| 100 | + table = operation.Table, |
| 101 | + id = operation.Id, |
| 102 | + data = operation.OpData |
| 103 | + }); |
| 104 | + } |
| 105 | + |
| 106 | + var payload = JsonSerializer.Serialize(new { batch }); |
| 107 | + var content = new StringContent(payload, Encoding.UTF8, "application/json"); |
| 108 | + |
| 109 | + HttpResponseMessage response = await _httpClient.PostAsync($"{BackendUrl}/api/data", content); |
| 110 | + |
| 111 | + if (!response.IsSuccessStatusCode) |
| 112 | + { |
| 113 | + throw new Exception($"Received {response.StatusCode} from /api/data: {await response.Content.ReadAsStringAsync()}"); |
| 114 | + } |
| 115 | + |
| 116 | + await transaction.Complete(); |
| 117 | + } |
| 118 | + catch (Exception ex) |
| 119 | + { |
| 120 | + Console.WriteLine($"UploadData Error: {ex.Message}"); |
| 121 | + throw; |
| 122 | + } |
| 123 | + } |
| 124 | +} |
0 commit comments