fetchJson method
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
Implementation
Future<Map<String, dynamic>> fetchJson({
required String url,
Map<String, String>? headers,
int? timeout,
int? retries,
}) async {
final effectiveHeaders = {
'User-Agent': _defaultUserAgent,
'Accept': 'application/json',
..._defaultHeaders,
...?headers,
};
final effectiveTimeout = timeout ?? _defaultTimeout;
final effectiveRetries = retries ?? _maxRetries;
final response = await _fetchWithRetry(
url: url,
headers: effectiveHeaders,
timeout: effectiveTimeout,
retries: effectiveRetries,
);
try {
return json.decode(response) as Map<String, dynamic>;
} catch (e) {
throw ScrapingException('Failed to parse JSON response: $e');
}
}