joinCookies function

String? joinCookies(
  1. String? staticCookies,
  2. String? cookies
)

Joins two cookie strings.

Implementation

String? joinCookies(String? staticCookies, String? cookies) {
  if (cookies == null && staticCookies == null) {
    return null;
  }

  final trimmedCookies = cookies?.trim();
  final trimmedStaticCookies = staticCookies?.trim();

  if (trimmedCookies == null || trimmedCookies.isEmpty) {
    if (trimmedStaticCookies != "") {
      return trimmedStaticCookies;
    }
    return null;
  }

  if (trimmedStaticCookies == null || trimmedStaticCookies.isEmpty) {
    if (trimmedCookies != "") {
      return trimmedCookies;
    }
    return null;
  }

  Map<String, String> parsedCookies = parseCookies(trimmedCookies);
  Map<String, String> parsedStaticCookies = parseCookies(trimmedStaticCookies);

  Map<String, String> mergedCookies = {...parsedStaticCookies};

  parsedCookies.forEach((key, value) {
    if (!mergedCookies.containsKey(key)) {
      mergedCookies[key] = value;
    }
  });

  return mapToCookie(mergedCookies);
}