from static method

Future<ShelfApiRequest> from(
  1. Request request,
  2. String method,
  3. String endpoint
)

Implementation

static Future<ShelfApiRequest> from(
  Request request,
  String method,
  String endpoint,
) async {
  Map<String, dynamic> params, context;

  if (method == 'POST') {
    var body = jsonDecode(await request.readAsString());

    _validate(body);

    params = body['params'] as Map<String, dynamic>;
    context = body['context'] as Map<String, dynamic>? ?? {};
  } else {
    params = request.requestedUri.queryParameters
        .map((k, v) => MapEntry(k, jsonDecode(v)));
    context = jsonDecode(request.headers['x-api-context'] ?? '{}')
        as Map<String, dynamic>;
  }

  return ShelfApiRequest._(
    url: request.requestedUri,
    method: method,
    headers: request.headers,
    parameters: params,
    context: context,
  );
}