Skip to content

Commit 99c17ef

Browse files
chrisbobbegnprice
authored andcommitted
api: Implement ApiConnection.delete
1 parent 6dba863 commit 99c17ef

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

lib/api/core.dart

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,16 @@ class ApiConnection {
147147
..files.add(http.MultipartFile('file', content, length, filename: filename));
148148
return send(routeName, fromJson, request);
149149
}
150+
151+
Future<T> delete<T>(String routeName, T Function(Map<String, dynamic>) fromJson,
152+
String path, Map<String, dynamic>? params) async {
153+
final url = realmUrl.replace(path: "/api/v1/$path");
154+
final request = http.Request('DELETE', url);
155+
if (params != null) {
156+
request.bodyFields = encodeParameters(params)!;
157+
}
158+
return send(routeName, fromJson, request);
159+
}
150160
}
151161

152162
ApiRequestException _makeApiException(String routeName, int httpStatus, Map<String, dynamic>? json) {

test/api/core_test.dart

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,36 @@ void main() {
104104
checkRequest(['asdf'.codeUnits], 100, null);
105105
});
106106

107+
test('ApiConnection.delete', () async {
108+
Future<void> checkRequest(Map<String, dynamic>? params, String expectedBody, {bool expectContentType = true}) {
109+
return FakeApiConnection.with_(account: eg.selfAccount, (connection) async {
110+
connection.prepare(json: {});
111+
await connection.delete(kExampleRouteName, (json) => json, 'example/route', params);
112+
check(connection.lastRequest!).isA<http.Request>()
113+
..method.equals('DELETE')
114+
..url.asString.equals('${eg.realmUrl.origin}/api/v1/example/route')
115+
..headers.deepEquals({
116+
...authHeader(email: eg.selfAccount.email, apiKey: eg.selfAccount.apiKey),
117+
if (expectContentType)
118+
'content-type': 'application/x-www-form-urlencoded; charset=utf-8',
119+
})
120+
..body.equals(expectedBody);
121+
});
122+
}
123+
124+
checkRequest(null, '', expectContentType: false);
125+
checkRequest({}, '');
126+
checkRequest({'x': 3}, 'x=3');
127+
checkRequest({'x': 3, 'y': 4}, 'x=3&y=4');
128+
checkRequest({'x': null}, 'x=null');
129+
checkRequest({'x': true}, 'x=true');
130+
checkRequest({'x': 'foo'}, 'x=%22foo%22');
131+
checkRequest({'x': [1, 2]}, 'x=%5B1%2C2%5D');
132+
checkRequest({'x': {'y': 1}}, 'x=%7B%22y%22%3A1%7D');
133+
checkRequest({'x': RawParameter('foo')}, 'x=foo');
134+
checkRequest({'x': RawParameter('foo'), 'y': 'bar'}, 'x=foo&y=%22bar%22');
135+
});
136+
107137
test('API success result', () async {
108138
await FakeApiConnection.with_(account: eg.selfAccount, (connection) async {
109139
connection.prepare(json: {'result': 'success', 'x': 3});

0 commit comments

Comments
 (0)