diff --git a/README.md b/README.md index 3a9ad0a..c8479a5 100644 --- a/README.md +++ b/README.md @@ -173,6 +173,11 @@ pb.deleteRecord("dummy_auth", p1); ## Development +### Dependencies + +This requires that you have the `libcurl` libraries available for +linking against. + ### Unit tests To run tests you will want to enable the `pragma`s and `writeln`s. therefore pass the `dbg` flag in as such: diff --git a/dub.json b/dub.json index d7d538d..184af18 100644 --- a/dub.json +++ b/dub.json @@ -6,5 +6,6 @@ "description": "PocketBase wrapper with serializer/deserializer support", "license": "LGPL v3.0", "name": "libpb", - "targetType" : "library" + "targetType" : "library", + "libs":["curl"] } diff --git a/source/libpb.d b/source/libpb.d index 5801d82..330d9a1 100644 --- a/source/libpb.d +++ b/source/libpb.d @@ -91,7 +91,31 @@ public class PocketBase // Compute the query string string queryStr = "page="~to!(string)(page)~"&perPage="~to!(string)(perPage); + + // If there is a filter then perform the needed escaping + if(cmp(filter, "") != 0) + { + // For the filter, make sure to add URL escaping to the `filter` parameter + import etc.c.curl : curl_escape; + import std.string : toStringz, fromStringz; + char* escapedParameter = curl_escape(toStringz(filter), cast(int)filter.length); + if(escapedParameter is null) + { + debug(dbg) + { + writeln("Invalid return from curl_easy_escape"); + } + throw new PBException(); + } + + // Convert back to D-string (the filter) + filter = cast(string)fromStringz(escapedParameter); + } + + // Append the filter queryStr ~= cmp(filter, "") == 0 ? "" : "&filter="~filter; + + writeln(queryStr); try { @@ -107,6 +131,12 @@ public class PocketBase } catch(CurlException e) { + debug(dbg) + { + writeln("curl"); + writeln(e); + } + throw new NetworkException(); } catch(JSONException e) @@ -746,7 +776,7 @@ unittest p1.age = 23; Person p2 = Person(); - p2.name = p1.name; + p2.name = p1.name~"2"; p2.age = p1.age; p1 = pb.createRecord("dummy", p1); @@ -755,4 +785,16 @@ unittest Person[] people = pb.listRecords!(Person)("dummy", 1, 30, "(id='"~p1.id~"')"); assert(people.length == 1); assert(cmp(people[0].id, p1.id) == 0); + + pb.deleteRecord("dummy", p1); + people = pb.listRecords!(Person)("dummy", 1, 30, "(id='"~p1.id~"')"); + assert(people.length == 0); + + people = pb.listRecords!(Person)("dummy", 1, 30, "(id='"~p2.id~"' && age=24)"); + assert(people.length == 0); + + people = pb.listRecords!(Person)("dummy", 1, 30, "(id='"~p2.id~"' && age=23)"); + assert(people.length == 1 && cmp(people[0].id, p2.id) == 0); + + pb.deleteRecord("dummy", p2); }