|
1 | | -namespace PowerSync.Common.IntegrationTests; |
2 | | - |
3 | | -using System; |
4 | | -using System.Collections.Generic; |
5 | | -using System.Net.Http; |
6 | | -using System.Text; |
7 | | -using System.Text.Json; |
8 | | -using System.Threading.Tasks; |
9 | | - |
10 | | -using PowerSync.Common.DB.Crud; |
11 | | - |
12 | | -public class NodeClient |
13 | | -{ |
14 | | - private readonly HttpClient _httpClient; |
15 | | - private readonly string _backendUrl; |
16 | | - private readonly string _userId; |
17 | | - |
18 | | - public NodeClient(string userId) |
19 | | - { |
20 | | - _httpClient = new HttpClient(); |
21 | | - _backendUrl = "http://localhost:6060"; |
22 | | - _userId = userId; |
23 | | - } |
24 | | - |
25 | | - public NodeClient(string backendUrl, string userId) |
26 | | - { |
27 | | - _httpClient = new HttpClient(); |
28 | | - _backendUrl = backendUrl; |
29 | | - _userId = userId; |
30 | | - } |
31 | | - |
32 | | - public Task<string> CreateList(string id, string name) |
33 | | - { |
34 | | - return CreateItem("lists", id, name); |
35 | | - } |
36 | | - |
37 | | - async Task<string> CreateItem(string table, string id, string name) |
38 | | - { |
39 | | - var data = new Dictionary<string, object> |
40 | | - { |
41 | | - { "created_at", DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss") }, |
42 | | - { "name", name }, |
43 | | - { "owner_id", _userId } |
44 | | - }; |
45 | | - |
46 | | - var batch = new[] |
47 | | - { |
48 | | - new |
49 | | - { |
50 | | - op = UpdateType.PUT.ToString(), |
51 | | - table = table, |
52 | | - id = id, |
53 | | - data = data |
54 | | - } |
55 | | - }; |
56 | | - |
57 | | - var payload = JsonSerializer.Serialize(new { batch }); |
58 | | - var content = new StringContent(payload, Encoding.UTF8, "application/json"); |
59 | | - |
60 | | - HttpResponseMessage response = await _httpClient.PostAsync($"{_backendUrl}/api/data", content); |
61 | | - |
62 | | - if (!response.IsSuccessStatusCode) |
63 | | - { |
64 | | - Console.WriteLine(await response.Content.ReadAsStringAsync()); |
65 | | - throw new Exception( |
66 | | - $"Failed to create item. Status: {response.StatusCode}, " + |
67 | | - $"Response: {await response.Content.ReadAsStringAsync()}" |
68 | | - ); |
69 | | - } |
70 | | - |
71 | | - return await response.Content.ReadAsStringAsync(); |
72 | | - } |
73 | | - |
74 | | - public Task<string> DeleteList(string id) |
75 | | - { |
76 | | - return DeleteItem("lists", id); |
77 | | - } |
78 | | - |
79 | | - public Task<string> CreateTodo(string id, string listId, string description) |
80 | | - { |
81 | | - return CreateTodoItem("todos", id, listId, description); |
82 | | - } |
83 | | - |
84 | | - async Task<string> CreateTodoItem(string table, string id, string listId, string description) |
85 | | - { |
86 | | - var data = new Dictionary<string, object> |
87 | | - { |
88 | | - { "created_at", DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss") }, |
89 | | - { "description", description }, |
90 | | - { "list_id", listId }, |
91 | | - { "created_by", _userId }, |
92 | | - { "completed", 0 }, |
93 | | - }; |
94 | | - |
95 | | - var batch = new[] |
96 | | - { |
97 | | - new |
98 | | - { |
99 | | - op = UpdateType.PUT.ToString(), |
100 | | - table = table, |
101 | | - id = id, |
102 | | - data = data |
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 | | - Console.WriteLine(await response.Content.ReadAsStringAsync()); |
114 | | - throw new Exception( |
115 | | - $"Failed to create todo. Status: {response.StatusCode}, " + |
116 | | - $"Response: {await response.Content.ReadAsStringAsync()}" |
117 | | - ); |
118 | | - } |
119 | | - |
120 | | - return await response.Content.ReadAsStringAsync(); |
121 | | - } |
122 | | - |
123 | | - public Task<string> DeleteTodo(string id) |
124 | | - { |
125 | | - return DeleteItem("todos", id); |
126 | | - } |
127 | | - |
128 | | - async Task<string> DeleteItem(string table, string id) |
129 | | - { |
130 | | - var batch = new[] |
131 | | - { |
132 | | - new |
133 | | - { |
134 | | - op = UpdateType.DELETE.ToString(), |
135 | | - table = table, |
136 | | - id = id |
137 | | - } |
138 | | - }; |
139 | | - |
140 | | - var payload = JsonSerializer.Serialize(new { batch }); |
141 | | - var content = new StringContent(payload, Encoding.UTF8, "application/json"); |
142 | | - |
143 | | - HttpResponseMessage response = await _httpClient.PostAsync($"{_backendUrl}/api/data", content); |
144 | | - |
145 | | - if (!response.IsSuccessStatusCode) |
146 | | - { |
147 | | - Console.WriteLine(await response.Content.ReadAsStringAsync()); |
148 | | - throw new Exception( |
149 | | - $"Failed to delete item. Status: {response.StatusCode}, " + |
150 | | - $"Response: {await response.Content.ReadAsStringAsync()}" |
151 | | - ); |
152 | | - } |
153 | | - |
154 | | - return await response.Content.ReadAsStringAsync(); |
155 | | - } |
156 | | - |
157 | | - public void Dispose() |
158 | | - { |
159 | | - _httpClient?.Dispose(); |
160 | | - } |
| 1 | +namespace PowerSync.Common.IntegrationTests; |
| 2 | + |
| 3 | +using System; |
| 4 | +using System.Collections.Generic; |
| 5 | +using System.Net.Http; |
| 6 | +using System.Text; |
| 7 | +using System.Text.Json; |
| 8 | +using System.Threading.Tasks; |
| 9 | + |
| 10 | +using PowerSync.Common.DB.Crud; |
| 11 | + |
| 12 | +public class NodeClient |
| 13 | +{ |
| 14 | + private readonly HttpClient _httpClient; |
| 15 | + private readonly string _backendUrl; |
| 16 | + private readonly string _userId; |
| 17 | + |
| 18 | + public NodeClient(string userId) |
| 19 | + { |
| 20 | + _httpClient = new HttpClient(); |
| 21 | + _backendUrl = "http://localhost:6060"; |
| 22 | + _userId = userId; |
| 23 | + } |
| 24 | + |
| 25 | + public NodeClient(string backendUrl, string userId) |
| 26 | + { |
| 27 | + _httpClient = new HttpClient(); |
| 28 | + _backendUrl = backendUrl; |
| 29 | + _userId = userId; |
| 30 | + } |
| 31 | + |
| 32 | + public Task<string> CreateList(string id, string name) |
| 33 | + { |
| 34 | + return CreateItem("lists", id, name); |
| 35 | + } |
| 36 | + |
| 37 | + async Task<string> CreateItem(string table, string id, string name) |
| 38 | + { |
| 39 | + var data = new Dictionary<string, object> |
| 40 | + { |
| 41 | + { "created_at", DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss") }, |
| 42 | + { "name", name }, |
| 43 | + { "owner_id", _userId } |
| 44 | + }; |
| 45 | + |
| 46 | + var batch = new[] |
| 47 | + { |
| 48 | + new |
| 49 | + { |
| 50 | + op = UpdateType.PUT.ToString(), |
| 51 | + table = table, |
| 52 | + id = id, |
| 53 | + data = data |
| 54 | + } |
| 55 | + }; |
| 56 | + |
| 57 | + var payload = JsonSerializer.Serialize(new { batch }); |
| 58 | + var content = new StringContent(payload, Encoding.UTF8, "application/json"); |
| 59 | + |
| 60 | + HttpResponseMessage response = await _httpClient.PostAsync($"{_backendUrl}/api/data", content); |
| 61 | + |
| 62 | + if (!response.IsSuccessStatusCode) |
| 63 | + { |
| 64 | + Console.WriteLine(await response.Content.ReadAsStringAsync()); |
| 65 | + throw new Exception( |
| 66 | + $"Failed to create item. Status: {response.StatusCode}, " + |
| 67 | + $"Response: {await response.Content.ReadAsStringAsync()}" |
| 68 | + ); |
| 69 | + } |
| 70 | + |
| 71 | + return await response.Content.ReadAsStringAsync(); |
| 72 | + } |
| 73 | + |
| 74 | + public Task<string> DeleteList(string id) |
| 75 | + { |
| 76 | + return DeleteItem("lists", id); |
| 77 | + } |
| 78 | + |
| 79 | + public Task<string> CreateTodo(string id, string listId, string description) |
| 80 | + { |
| 81 | + return CreateTodoItem("todos", id, listId, description); |
| 82 | + } |
| 83 | + |
| 84 | + async Task<string> CreateTodoItem(string table, string id, string listId, string description) |
| 85 | + { |
| 86 | + var data = new Dictionary<string, object> |
| 87 | + { |
| 88 | + { "created_at", DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss") }, |
| 89 | + { "description", description }, |
| 90 | + { "list_id", listId }, |
| 91 | + { "created_by", _userId }, |
| 92 | + { "completed", 0 }, |
| 93 | + }; |
| 94 | + |
| 95 | + var batch = new[] |
| 96 | + { |
| 97 | + new |
| 98 | + { |
| 99 | + op = UpdateType.PUT.ToString(), |
| 100 | + table = table, |
| 101 | + id = id, |
| 102 | + data = data |
| 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 | + Console.WriteLine(await response.Content.ReadAsStringAsync()); |
| 114 | + throw new Exception( |
| 115 | + $"Failed to create todo. Status: {response.StatusCode}, " + |
| 116 | + $"Response: {await response.Content.ReadAsStringAsync()}" |
| 117 | + ); |
| 118 | + } |
| 119 | + |
| 120 | + return await response.Content.ReadAsStringAsync(); |
| 121 | + } |
| 122 | + |
| 123 | + public Task<string> DeleteTodo(string id) |
| 124 | + { |
| 125 | + return DeleteItem("todos", id); |
| 126 | + } |
| 127 | + |
| 128 | + async Task<string> DeleteItem(string table, string id) |
| 129 | + { |
| 130 | + var batch = new[] |
| 131 | + { |
| 132 | + new |
| 133 | + { |
| 134 | + op = UpdateType.DELETE.ToString(), |
| 135 | + table = table, |
| 136 | + id = id |
| 137 | + } |
| 138 | + }; |
| 139 | + |
| 140 | + var payload = JsonSerializer.Serialize(new { batch }); |
| 141 | + var content = new StringContent(payload, Encoding.UTF8, "application/json"); |
| 142 | + |
| 143 | + HttpResponseMessage response = await _httpClient.PostAsync($"{_backendUrl}/api/data", content); |
| 144 | + |
| 145 | + if (!response.IsSuccessStatusCode) |
| 146 | + { |
| 147 | + Console.WriteLine(await response.Content.ReadAsStringAsync()); |
| 148 | + throw new Exception( |
| 149 | + $"Failed to delete item. Status: {response.StatusCode}, " + |
| 150 | + $"Response: {await response.Content.ReadAsStringAsync()}" |
| 151 | + ); |
| 152 | + } |
| 153 | + |
| 154 | + return await response.Content.ReadAsStringAsync(); |
| 155 | + } |
| 156 | + |
| 157 | + public void Dispose() |
| 158 | + { |
| 159 | + _httpClient?.Dispose(); |
| 160 | + } |
161 | 161 | } |
0 commit comments