execute method
Sends the request and returns a Future. catch any error and returns with status 500
head
to trigger a HEAD request
count
if you want to returns the count value. Support exact, planned and estimated count options.
For more details about switching schemas: https://postgrest.org/en/stable/api.html#switching-schemas Returns {Future} Resolves when the request has completed.
Implementation
Future<PostgrestResponse<T>> execute({
bool head = false,
CountOption? count,
}) async {
if (head) {
method = 'HEAD';
}
if (count != null) {
if (headers['Prefer'] == null) {
headers['Prefer'] = 'count=${count.name()}';
} else {
headers['Prefer'] = '${headers['Prefer']!},count=${count.name()}';
}
}
try {
if (method == null) {
throw "Missing table operation: select, insert, update or delete";
}
final uppercaseMethod = method!.toUpperCase();
late http.Response response;
if (schema == null) {
// skip
} else if (['GET', 'HEAD'].contains(method)) {
headers['Accept-Profile'] = schema!;
} else {
headers['Content-Profile'] = schema!;
}
if (method != 'GET' && method != 'HEAD') {
headers['Content-Type'] = 'application/json';
}
final bodyStr = json.encode(body);
if (uppercaseMethod == 'GET') {
response = await (httpClient?.get ?? http.get)(
url,
headers: headers,
);
} else if (uppercaseMethod == 'POST') {
response = await (httpClient?.post ?? http.post)(
url,
headers: headers,
body: bodyStr,
);
} else if (uppercaseMethod == 'PUT') {
response = await (httpClient?.put ?? http.put)(
url,
headers: headers,
body: bodyStr,
);
} else if (uppercaseMethod == 'PATCH') {
response = await (httpClient?.patch ?? http.patch)(
url,
headers: headers,
body: bodyStr,
);
} else if (uppercaseMethod == 'DELETE') {
response = await (httpClient?.delete ?? http.delete)(
url,
headers: headers,
);
} else if (uppercaseMethod == 'HEAD') {
response = await (httpClient?.head ?? http.head)(
url,
headers: headers,
);
}
return _parseResponse(response);
} catch (e) {
final error =
PostgrestError(code: e.runtimeType.toString(), message: e.toString());
return PostgrestResponse(
status: 500,
error: error,
);
}
}