Skip to content

Commit 98b53e2

Browse files
committed
Hook up self-host-demo client with MAUI demo
1 parent a29eb7e commit 98b53e2

File tree

1 file changed

+16
-8
lines changed

1 file changed

+16
-8
lines changed

demos/TodoSQLite/Data/NodeConnector.cs

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,17 @@ namespace TodoSQLite.Data;
1313

1414
public class NodeConnector : IPowerSyncBackendConnector
1515
{
16-
private readonly HttpClient httpClient;
16+
private static readonly string StorageFilePath = "user_id.txt"; // Simulating local storage
17+
private readonly HttpClient _httpClient;
1718

1819
public string BackendUrl { get; }
1920
public string PowerSyncUrl { get; }
20-
public string UserId { get; }
21+
public string UserId { get; private set; }
2122
private string? clientId;
2223

2324
public NodeConnector()
2425
{
25-
httpClient = new HttpClient();
26+
_httpClient = new HttpClient();
2627

2728
// Load or generate User ID
2829
UserId = LoadOrGenerateUserId();
@@ -35,15 +36,22 @@ public NodeConnector()
3536

3637
public string LoadOrGenerateUserId()
3738
{
38-
return "8ba3ec38-6cc8-449c-88c0-9275987ea5d2";
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;
3947
}
4048

4149
public async Task<PowerSyncCredentials?> FetchCredentials()
4250
{
43-
var tokenEndpoint = "api/auth/token";
44-
var url = $"{BackendUrl}/{tokenEndpoint}?user_id={UserId}";
51+
string tokenEndpoint = "api/auth/token";
52+
string url = $"{BackendUrl}/{tokenEndpoint}?user_id={UserId}";
4553

46-
var response = await httpClient.GetAsync(url);
54+
HttpResponseMessage response = await _httpClient.GetAsync(url);
4755
if (!response.IsSuccessStatusCode)
4856
{
4957
throw new Exception($"Received {response.StatusCode} from {tokenEndpoint}: {await response.Content.ReadAsStringAsync()}");
@@ -98,7 +106,7 @@ public async Task UploadData(IPowerSyncDatabase database)
98106
var payload = JsonSerializer.Serialize(new { batch });
99107
var content = new StringContent(payload, Encoding.UTF8, "application/json");
100108

101-
HttpResponseMessage response = await httpClient.PostAsync($"{BackendUrl}/api/data", content);
109+
HttpResponseMessage response = await _httpClient.PostAsync($"{BackendUrl}/api/data", content);
102110

103111
if (!response.IsSuccessStatusCode)
104112
{

0 commit comments

Comments
 (0)