convertDataModelToPostRequest<T extends Object> function

PostRequest convertDataModelToPostRequest<T extends Object>({
  1. required T dataModel,
  2. required Map<String, dynamic> toJson(
    1. T e
    ),
})

The data model is required have a top level property string called path, which will become the PostRequest's path.

If model has a top level property called body, it must be of type Map<String, dynamic>, which will become the PostRequest's body.

Other properties become query parameters.

Implementation

PostRequest convertDataModelToPostRequest<T extends Object>({
  required T dataModel,
  required Map<String, dynamic> Function(T e) toJson,
}) {
  try {
    final json = toJson(dataModel);
    final body = json.remove('body');
    final uri = json.toUri();
    return PostRequest(
      path: uri.path,
      queryParameters: uri.queryParameters,
      body: body,
    );
  } catch (e) {
    throw FormatException(e.toString());
  }
}