patch<T> method
Perform a PATCH HTTP request
-
pathis the endpoint of the request, it is added to the baseUrl of ApiBase -
conversionFuncis a function used to transform the json (stored as a Map) into another object.Example of a
conversionFuncfor a example class:class Person { Person({required this.firstName, required this.lastName}); String firstName; String lastName; } Person fromJsonToPerson(Map<String, dynamic> json) { return Person(firstName: json["firstName"], lastName: json["lastName"]); } -
headersare a map of headers to add to the defaultHeaders, if a keys ofheadersis present in the defaultHeaders, its value is overridden by the one inheaders. If the token of ApiBase has been set, anAuthorizationheader will be added, it will override if aAuthorizationheader has been set in the defaultHeaders but will be overriden if aAuthorizationheader is present in theheaders -
requestBodyis a Map<String, dynamic> containing the request body to send -
queryParametersis a map containing the query parameters of the request -
timeoutDurationis the Duration until a TimeoutException is thrown for the request. If not provided, the timeout duration is set to 10 seconds.
If the request is successful (status code 200), the Future returned by get will contain a right Either containing the data
transformed into an object of type T by conversionFunc. Else, it will contain a left Either containing the a ApiError
containing the status code and the body of the response as its message.
Implementation
Future<Either<ApiError, T>> patch<T>(
String path, T Function(dynamic json) conversionFunc,
{Map<String, String>? headers,
Map<String, dynamic>? requestBody,
Map<String, String>? queryParameters,
Duration? timeoutDuration}) async {
String? body = requestBody == null ? null : json.encode(requestBody);
final promise = _client
.patch(
Uri.parse((_baseUrl ?? "") +
path +
(queryParameters?.entries
.map((entry) => "${entry.key}=${entry.value}")
.fold("?", (previous, current) {
if (previous == "?") {
return "?$current";
}
return "$previous&$current";
}) ??
"")),
headers: <String, String>{
...?_defaultHeaders,
if (_accessToken != null) "Authorization": "Bearer $_accessToken",
...?headers
},
body: body,
)
.timeout(timeoutDuration ?? const Duration(seconds: 60));
return _handleApiResult(promise, path).mapRight(conversionFunc);
}