fetchJson method

Future<Map<String, dynamic>> fetchJson({
  1. required String url,
  2. Map<String, String>? headers,
  3. int? timeout,
  4. int? retries,
  5. int priority = 0,
})

Fetches JSON content from the given URL

url is the URL to fetch headers are additional headers to send with the request timeout is the timeout for the request in milliseconds retries is the number of retry attempts priority is the priority of the request (higher values = higher priority)

Implementation

Future<Map<String, dynamic>> fetchJson({
  required String url,
  Map<String, String>? headers,
  int? timeout,
  int? retries,
  int priority = 0,
}) async {
  final effectiveHeaders = {'Accept': 'application/json', ...?headers};

  final response = await fetchHtml(
    url: url,
    headers: effectiveHeaders,
    timeout: timeout,
    retries: retries,
    priority: priority,
  );

  try {
    return json.decode(response) as Map<String, dynamic>;
  } catch (e) {
    throw ScrapingException.parsing(
      'Failed to parse JSON response',
      originalException: e,
      url: url,
      isRetryable: false,
    );
  }
}