toAPIRequest static method

FutureOr<APIRequest> toAPIRequest(
  1. Request request, {
  2. required bool cookieless,
  3. required bool useSessionID,
})

Converts a request to an APIRequest.

Implementation

static FutureOr<APIRequest> toAPIRequest(Request request,
    {required bool cookieless, required bool useSessionID}) {
  var requestTime = DateTime.now();

  var method = parseAPIRequestMethod(request.method) ?? APIRequestMethod.GET;

  var headers = Map.fromEntries(request.headersAll.entries.map((e) {
    var values = e.value;
    return MapEntry(e.key, values.length == 1 ? values[0] : values);
  }));

  if (cookieless) {
    headers.remove('cookie');
  }

  var requestedUri = request.requestedUri;

  var path = requestedUri.path;
  var parameters =
      Map.fromEntries(requestedUri.queryParametersAll.entries.map((e) {
    var values = e.value;
    return MapEntry(e.key, values.length == 1 ? values[0] : values);
  }));

  var scheme = request.requestedUri.scheme;

  var connectionInfo = _getConnectionInfo(request);
  var requesterAddress = connectionInfo?.remoteAddress.address;

  var requesterSource = requesterAddress == null
      ? APIRequesterSource.unknown
      : (_isLocalAddress(requesterAddress)
          ? APIRequesterSource.local
          : APIRequesterSource.remote);

  String? sessionID;
  bool newSession = false;

  if (useSessionID && !cookieless) {
    var cookies = _parseCookies(request);
    if (cookies != null && cookies.isNotEmpty) {
      sessionID = cookies['SESSIONID'] ?? cookies['SESSION_ID'];
    }

    if (sessionID == null) {
      sessionID = APISession.generateSessionID();
      newSession = true;
    }
  }

  var keepAlive = false;

  if (request.protocolVersion == '1.1') {
    var headerConnection = headers[HttpHeaders.connectionHeader];
    if (headerConnection != null) {
      keepAlive = equalsIgnoreAsciiCase('$headerConnection', 'Keep-Alive');
    }
  }

  var credential = _resolveCredential(request);

  var parsingDuration = DateTime.now().difference(requestTime);

  return _resolvePayload(request).resolveMapped((payloadResolved) {
    var mimeType = payloadResolved?.$1;
    var payload = payloadResolved?.$2;

    Map<String, dynamic> parametersResolved;

    if (mimeType != null && payload != null && mimeType.isFormURLEncoded) {
      var payloadMap = payload as Map<String, dynamic>;

      parametersResolved = parameters.isEmpty
          ? payloadMap
          : <String, dynamic>{...parameters, ...payloadMap};

      payload = null;
      mimeType = null;
    } else {
      parametersResolved = Map<String, dynamic>.from(parameters);
    }

    var req = APIRequest(method, path,
        protocol: 'HTTP/${request.protocolVersion}',
        keepAlive: keepAlive,
        parameters: parametersResolved,
        requesterSource: requesterSource,
        requesterAddress: requesterAddress,
        headers: headers,
        sessionID: sessionID,
        newSession: newSession,
        credential: credential,
        scheme: scheme,
        requestedUri: request.requestedUri,
        originalRequest: request,
        payload: payload,
        payloadMimeType: mimeType,
        time: requestTime,
        parsingDuration: parsingDuration);

    return req;
  });
}