Skip to content

Commit d190186

Browse files
committed
Add getList method to PocketbaseArduino class
1 parent d32f681 commit d190186

2 files changed

Lines changed: 168 additions & 1 deletion

File tree

PocketbaseArduino.cpp

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ String PocketbaseArduino::httpsGETRequest(const char *endpoint)
9595
{
9696
Serial.printf("[HTTPS] Unable to connect\n");
9797
}
98+
// TODO: improve return value in case failure happens
9899
return ""; // Return an empty string on failure
99100
}
100101

@@ -167,3 +168,126 @@ String PocketbaseArduino::getOne(const char *recordId, const char *expand /* = n
167168
return httpGETRequest(fullEndpoint.c_str());
168169
}
169170
}
171+
172+
String PocketbaseArduino::getList(
173+
const char *page /* = nullptr */,
174+
const char *perPage /* = nullptr */,
175+
const char *sort /* = nullptr */,
176+
const char *filter /* = nullptr */,
177+
const char *expand /* = nullptr */,
178+
const char *fields /* = nullptr */,
179+
const char *skipTotal /* = nullptr */)
180+
{
181+
String fullEndpoint;
182+
183+
if (base_url.startsWith("https://"))
184+
{
185+
// Use HTTPS if base URL starts with "https://"
186+
fullEndpoint = base_url + String(current_endpoint) + "records/";
187+
}
188+
else
189+
{
190+
// Use HTTP for other cases
191+
fullEndpoint = base_url + String(current_endpoint) + "records/";
192+
}
193+
194+
if (page != nullptr && strlen(page) > 0)
195+
{
196+
197+
if (fullEndpoint.indexOf('?') == -1)
198+
{
199+
fullEndpoint += "?";
200+
}
201+
else
202+
{
203+
fullEndpoint += "&";
204+
}
205+
206+
fullEndpoint += "page=" + String(page);
207+
}
208+
209+
if (perPage != nullptr && strlen(perPage) > 0)
210+
{
211+
if (fullEndpoint.indexOf('?') == -1)
212+
{
213+
fullEndpoint += "?";
214+
}
215+
else
216+
{
217+
fullEndpoint += "&";
218+
}
219+
220+
fullEndpoint += "perPage=" + String(perPage);
221+
}
222+
223+
if (sort != nullptr && strlen(sort) > 0)
224+
{
225+
if (fullEndpoint.indexOf('?') == -1)
226+
{
227+
fullEndpoint += "?";
228+
}
229+
else
230+
{
231+
fullEndpoint += "&";
232+
}
233+
234+
fullEndpoint += "perPage=" + String(sort);
235+
}
236+
237+
if (filter != nullptr && strlen(filter) > 0)
238+
{
239+
if (fullEndpoint.indexOf('?') == -1)
240+
{
241+
fullEndpoint += "?";
242+
}
243+
else
244+
{
245+
fullEndpoint += "&";
246+
}
247+
248+
fullEndpoint += "perPage=" + String(filter);
249+
}
250+
251+
if (skipTotal != nullptr && strlen(skipTotal) > 0)
252+
{
253+
if (fullEndpoint.indexOf('?') == -1)
254+
{
255+
fullEndpoint += "?";
256+
}
257+
else
258+
{
259+
fullEndpoint += "&";
260+
}
261+
262+
fullEndpoint += "perPage=" + String(skipTotal);
263+
}
264+
265+
if (expand != nullptr && strlen(expand) > 0)
266+
{
267+
fullEndpoint += "?expand=" + String(expand);
268+
}
269+
270+
if (fields != nullptr && strlen(fields) > 0)
271+
{
272+
273+
if (fullEndpoint.indexOf('?') == -1)
274+
{
275+
fullEndpoint += "?";
276+
}
277+
else
278+
{
279+
fullEndpoint += "&";
280+
}
281+
282+
fullEndpoint += "fields=" + String(fields);
283+
}
284+
285+
if (base_url.startsWith("https://"))
286+
{
287+
return httpsGETRequest(fullEndpoint.c_str());
288+
}
289+
else
290+
{
291+
return httpGETRequest(fullEndpoint.c_str());
292+
}
293+
}

PocketbaseArduino.h

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,50 @@ class PocketbaseArduino
3636
*
3737
* For more information, see: https://pocketbase.io/docs
3838
*/
39-
String getOne(const char *recordId, const char *expand /* = nullptr */, const char *fields /* = nullptr */);
39+
String getOne(
40+
const char *recordId,
41+
const char *expand /* = nullptr */,
42+
const char *fields /* = nullptr */);
43+
44+
/**
45+
* @brief Fetches a multiple records from a Pocketbase collection. Supports sorting and filtering.
46+
*
47+
* @param page The page (aka. offset) of the paginated list (default to 1).
48+
*
49+
* @param perPage Specify the max returned records per page (default to 30).
50+
*
51+
* @param sort Specify the records order attribute(s).
52+
* Add - / + (default) in front of the attribute for DESC / ASC order. Ex.:
53+
* // DESC by created and ASC by id
54+
* ?sort=-created,id
55+
*
56+
* @param filter Filter the returned records.
57+
*
58+
* @param expand (Optional) Auto expand record relations. Ex.:?expand=relField1,relField2.subRelField Supports up to 6-levels depth nested relations expansion.
59+
* The expanded relations will be appended to the record under the expand property (eg. "expand": {"relField1": {...}, ...}).
60+
* Only the relations to which the request user has permissions to view will be expanded.
61+
*
62+
* @param fields (Optional) Comma separated string of the fields to return in the JSON response (by default returns all fields). Ex.: ?fields=*,expand.relField.name
63+
* * targets all keys from the specific depth level.
64+
* In addition, the following field modifiers are also supported:
65+
* :excerpt(maxLength, withEllipsis?)
66+
* Returns a short plain text version of the field string value.
67+
* Ex.: ?fields=*,description:excerpt(200,true)
68+
*
69+
* @param skipTotal If it is set the total counts query will be skipped and the response fields totalItems and totalPages will have -1 value.
70+
* This could drastically speed up the search queries when the total counters are not needed or cursor based pagination is used.
71+
* For optimization purposes, it is set by default for the getFirstListItem() and getFullList() SDKs methods.
72+
*
73+
* For more information, see: https://pocketbase.io/docs
74+
*/
75+
String getList(
76+
const char *page /* = nullptr */,
77+
const char *perPage /* = nullptr */,
78+
const char *sort /* = nullptr */,
79+
const char *filter /* = nullptr */,
80+
const char *expand /* = nullptr */,
81+
const char *fields /* = nullptr */,
82+
const char *skipTotal /* = nullptr */);
4083

4184
private:
4285
String httpGETRequest(const char *endpoint);

0 commit comments

Comments
 (0)