getAdById method

Future<FastResponseAd?> getAdById(
  1. String adId
)

Retrieves an advertisement by its ID from the API.

Returns a FastResponseAd object if the ad with the given adId exists, otherwise returns null.

Implementation

Future<FastResponseAd?> getAdById(String adId) async {
  debugLog('Fetching ad with ID: $adId', debugLabel: debugLabel);

  final cachedAd = await _getCachedAd(adId);

  if (cachedAd != null) return cachedAd;

  final path = '$_adsPath$adId';
  final uri = Uri.https(uriAuthority, path);

  try {
    final response = await _requestAd(uri);

    if (response.statusCode == 200) {
      final ad = await compute(tryDecodeAd, response.body);

      if (ad != null) {
        _cache.put(adId, response.body, ttl: ttl);

        return ad;
      }
    }
  } catch (e) {
    debugLog('Failed to fetch ads: $e', debugLabel: debugLabel);
  }

  return null;
}