fetchFileContent method
Fetch the raw content of a file from a GitHub repository.
Uses the raw.githubusercontent.com endpoint for efficiency (no JSON
overhead, no base64 decoding required).
An optional ref (branch/tag/SHA) can be specified; defaults to main.
Returns null if the file does not exist or on failure.
Implementation
Future<String?> fetchFileContent(
String owner,
String repo,
String path, {
String? ref,
}) async {
final branch = ref ?? 'main';
final cacheKey = 'github_file_${owner}_${repo}_${branch}_$path';
final cached = await _cache.get(cacheKey);
if (cached != null) {
return cached['content'] as String?;
}
try {
final url = '$_rawBase/$owner/$repo/$branch/$path';
Logger.debug('Fetching file: $url');
final response = await _http.get(url);
if (!response.isSuccess) {
// 404 is expected when probing for filenames; do not warn.
if (response.statusCode != 404) {
Logger.warn(
'Failed to fetch $path from $owner/$repo@$branch: '
'HTTP ${response.statusCode}',
);
}
return null;
}
final content = response.body;
await _cache.set(
cacheKey,
{'content': content},
CacheManager.changelogTtl,
);
return content;
} catch (e, stack) {
Logger.error(
'Error fetching $path from $owner/$repo@$branch',
e,
stack,
);
return null;
}
}