build static method

HttpRequestSpec build({
  1. required HttpRule rule,
  2. required Map<String, dynamic> message,
})

Implementation

static HttpRequestSpec build({
  required HttpRule rule,
  required Map<String, dynamic> message,
}) {
  // Work on a shallow copy of top-level keys we can remove from as we
  // consume fields for the path/body, without mutating the caller's map.
  final remaining = _deepCopy(message);

  final pathString = _buildPath(rule, remaining);

  Map<String, dynamic>? jsonBody;
  final query = <String, dynamic>{};
  bool hasBody = !rule.method.isGet;
  final String bodyField = rule.body ?? "*";
  if (hasBody && bodyField == "*") {
    jsonBody = remaining;
  } else if (hasBody) {
    final bodyFieldValue = remaining.remove(bodyField);
    if (bodyFieldValue is Map<String, dynamic>) {
      jsonBody = bodyFieldValue;
    } else if (bodyFieldValue != null) {
      // Non-message body field (unusual, but technically legal proto3
      // JSON if the field is itself a message type at the wire level;
      // for scalar/bad input we just wrap it).
      jsonBody = {bodyField: bodyFieldValue};
    }
    query.addAll(_flattenForQuery(remaining));
  } else {
    query.addAll(_flattenForQuery(remaining));
  }

  // final base = Uri.parse(baseUrl);
  final fullPath = _joinPaths(pathString);

  return HttpRequestSpec(
    method: rule.method,
    path: fullPath,
    jsonBody: jsonBody,
    queryParameters: query.isEmpty ? {} : _stringifyQuery(query),
  );
}