getPackageScore method

Future<Map<String, dynamic>?> getPackageScore(
  1. String packageName
)

Get the package score and metrics from pub.dev.

GET /api/packages/{package}/score

Returns {grantedPoints, maxPoints, likeCount, ...}. Returns null on failure.

Implementation

Future<Map<String, dynamic>?> getPackageScore(String packageName) async {
  final cacheKey = 'pub_score_$packageName';
  final cached = await _cache.get(cacheKey);
  if (cached != null) return cached;

  try {
    Logger.debug('Fetching package score for $packageName');
    final response = await _http.get(
      '$_baseUrl/packages/$packageName/score',
    );
    if (!response.isSuccess) {
      Logger.warn(
        'Failed to fetch score for $packageName: '
        'HTTP ${response.statusCode}',
      );
      return null;
    }

    final data = response.json;
    // Scores change slowly; cache for a moderate duration.
    await _cache.set(cacheKey, data, CacheManager.packageMetadataTtl);
    return data;
  } catch (e, stack) {
    Logger.error('Error fetching score for $packageName', e, stack);
    return null;
  }
}