Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ open class AdMobBannerFragment : AdFragment() {
}

override fun loadAd() {
adUnit?.fetchDemand { result ->
Log.d("Prebid", "Fetch demand result: $result")
adUnit?.fetchDemandBidInfo { bidInfo ->
Log.d("Prebid", "Fetch demand result: ${bidInfo.resultCode}")
bannerView?.loadAd(adRequest!!)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ open class AdMobInterstitialFragment : AdFragment() {
.addNetworkExtrasBundle(PrebidInterstitialAdapter::class.java, extras!!)
.build()

adUnit?.fetchDemand { result ->
Log.d("Prebid", "Fetch demand result: $result")
adUnit?.fetchDemandBidInfo { bidInfo ->
Log.d("Prebid", "Fetch demand result: ${bidInfo.resultCode}")

InterstitialAd.load(requireContext(), adUnitId, request, object : InterstitialAdLoadCallback() {
override fun onAdLoaded(ad: InterstitialAd) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ class AdMobNativeFragment : AdFragment() {
.addNetworkExtrasBundle(PrebidNativeAdapter::class.java, extras!!)
.build()

adUnit?.fetchDemand { resultCode ->
Log.d(TAG, "Fetch demand result: $resultCode")
adUnit?.fetchDemandBidInfo { bidInfo ->
Log.d(TAG, "Fetch demand result: ${bidInfo.resultCode}")

/** For mediation use loadAd() not loadAds() */
adLoader?.loadAd(adRequest)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ open class AdMobRewardedFragment : AdFragment() {
.addNetworkExtrasBundle(PrebidRewardedAdapter::class.java, extras!!)
.build()

adUnit?.fetchDemand { result ->
Log.d("Prebid", "Fetch demand result: $result")
adUnit?.fetchDemandBidInfo { bidInfo ->
Log.d("Prebid", "Fetch demand result: ${bidInfo.resultCode}")

RewardedAd.load(requireContext(), adUnitId, request, object : RewardedAdLoadCallback() {
override fun onAdLoaded(ad: RewardedAd) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ open class MaxBannerFragment : AdFragment() {
}

override fun loadAd() {
adUnit?.fetchDemand {
adUnit?.fetchDemandBidInfo {
adView?.loadAd()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ open class MaxInterstitialFragment : AdFragment() {
}

override fun loadAd() {
adUnit?.fetchDemand {
adUnit?.fetchDemandBidInfo {
maxInterstitialAd?.loadAd()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ open class MaxNativeFragment : AdFragment() {
}

override fun loadAd() {
nativeAdUnit.fetchDemand(nativeAdLoader) {
nativeAdUnit.fetchDemandBidInfo(nativeAdLoader) {
nativeAdLoader.loadAd(createNativeAdView(requireActivity()))
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ open class MaxRewardedFragment : AdFragment() {
}

override fun loadAd() {
adUnit?.fetchDemand {
adUnit?.fetchDemandBidInfo {
maxRewardedAd?.loadAd()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import android.os.Looper
import android.view.View
import org.prebid.mobile.PrebidNativeAd
import org.prebid.mobile.PrebidNativeAdEventListener
import org.prebid.mobile.api.data.FetchDemandResult
import org.prebid.mobile.ResultCode
import org.prebid.mobile.api.mediation.MediationNativeAdUnit
import org.prebid.mobile.rendering.utils.ntv.NativeAdProvider
import org.prebid.mobile.renderingtestapp.AdFragment
Expand Down Expand Up @@ -66,17 +66,17 @@ open class PpmNativeFragment : AdFragment() {
}

override fun loadAd() {
nativeAdUnit?.fetchDemand {
if (it != FetchDemandResult.SUCCESS) {
nativeAdUnit?.fetchDemandBidInfo {
if (it.resultCode != ResultCode.SUCCESS) {
events.fetchDemandFailure(true)
return@fetchDemand
return@fetchDemandBidInfo
}
events.fetchDemandSuccess(true)

val nativeAd = NativeAdProvider.getNativeAd(extras)
if (nativeAd == null) {
events.getNativeAdResultFailure(true)
return@fetchDemand
return@fetchDemandBidInfo
}

events.getNativeAdResultSuccess(true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import androidx.constraintlayout.widget.ConstraintLayout
import org.prebid.mobile.LogUtil
import org.prebid.mobile.PrebidNativeAd
import org.prebid.mobile.PrebidNativeAdEventListener
import org.prebid.mobile.api.data.FetchDemandResult
import org.prebid.mobile.ResultCode
import org.prebid.mobile.api.mediation.MediationNativeAdUnit
import org.prebid.mobile.rendering.utils.ntv.NativeAdProvider
import org.prebid.mobile.renderingtestapp.R
Expand All @@ -48,15 +48,15 @@ class NativeFeedAdapter(
if (nativeAdLayout == null) {
nativeAdLayout = layoutInflater.inflate(R.layout.lyt_native_ad, parent, false) as ConstraintLayout
}
nativeAdUnit.fetchDemand {
if (it != FetchDemandResult.SUCCESS) {
return@fetchDemand
nativeAdUnit.fetchDemandBidInfo {
if (it.resultCode != ResultCode.SUCCESS) {
return@fetchDemandBidInfo
}

val nativeAd = NativeAdProvider.getNativeAd(extras)
if (nativeAd == null) {
LogUtil.error(TAG, "Native ad is null")
return@fetchDemand
return@fetchDemandBidInfo
}

inflateViewContent(nativeAd)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,9 +211,7 @@ public void fetchDemand(OnFetchDemandResult listener) {
allowNullableAdObject = true;

fetchDemand(null, resultCode -> {
BidInfo bidInfo = BidInfo.create(resultCode, bidResponse, configuration);
Util.saveCacheId(bidInfo.getNativeCacheId(), adObject);
listener.onComplete(bidInfo);
listener.onComplete(createBidInfo(resultCode));
});
}

Expand All @@ -237,9 +235,7 @@ public void fetchDemandBidInfo(Object adObject, OnFetchDemandResult listener) {
}

fetchDemand(adObject, resultCode -> {
BidInfo bidInfo = BidInfo.create(resultCode, bidResponse, configuration);
Util.saveCacheId(bidInfo.getNativeCacheId(), this.adObject);
listener.onComplete(bidInfo);
listener.onComplete(createBidInfo(resultCode));
});
}

Expand Down Expand Up @@ -297,6 +293,14 @@ public void setGlobalOrtbConfig(@Nullable String ortbConfig) {
configuration.setGlobalOrtbConfig(ortbConfig);
}

/**
* Builds the {@link BidInfo} reported for the last fetch.
*/
@NonNull
protected BidInfo createBidInfo(@NonNull ResultCode resultCode) {
return BidInfo.create(resultCode, bidResponse, configuration);
}

protected BidRequesterListener createBidListener(OnCompleteListener originalListener) {
return new BidRequesterListener() {
@Override
Expand Down Expand Up @@ -326,30 +330,7 @@ public void onError(AdException exception) {
protected ResultCode convertToResultCode(AdException renderingException) {
FetchDemandResult fetchDemandResult = FetchDemandResult.parseErrorMessage(renderingException.getMessage());
LogUtil.error("Prebid", "Can't download bids: " + fetchDemandResult);
switch (fetchDemandResult) {
case INVALID_ACCOUNT_ID:
return ResultCode.INVALID_ACCOUNT_ID;
case INVALID_CONFIG_ID:
return ResultCode.INVALID_CONFIG_ID;
case INVALID_SIZE:
return ResultCode.INVALID_SIZE;
case INVALID_CONTEXT:
return ResultCode.INVALID_CONTEXT;
case INVALID_AD_OBJECT:
return ResultCode.INVALID_AD_OBJECT;
case INVALID_HOST_URL:
return ResultCode.INVALID_HOST_URL;
case NETWORK_ERROR:
return ResultCode.NETWORK_ERROR;
case TIMEOUT:
return ResultCode.TIMEOUT;
case NO_BIDS:
return ResultCode.NO_BIDS;
case NO_CACHED_BIDS:
return ResultCode.NO_CACHED_BIDS;
default:
return ResultCode.PREBID_SERVER_ERROR;
}
return fetchDemandResult.toResultCode();
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import org.json.JSONObject;
import org.prebid.mobile.api.data.AdFormat;
import org.prebid.mobile.api.data.BidInfo;
import org.prebid.mobile.api.exceptions.AdException;
import org.prebid.mobile.configuration.NativeAdUnitConfiguration;
import org.prebid.mobile.rendering.bidding.data.bid.Bid;
Expand All @@ -30,6 +31,12 @@ public class NativeAdUnit extends AdUnit {

private final NativeAdUnitConfiguration nativeConfiguration;

/**
* Id the bid listener saved the last winning bid under in {@link CacheManager}.
*/
@Nullable
private String nativeCacheId;

/**
* Default constructor.
*/
Expand All @@ -38,6 +45,14 @@ public NativeAdUnit(@NonNull String configId) {
nativeConfiguration = configuration.getNativeConfiguration();
}

@NonNull
@Override
protected BidInfo createBidInfo(@NonNull ResultCode resultCode) {
// The bid listener already saved the winning bid, so reuse its id instead of caching
// the bid a second time.
return BidInfo.create(resultCode, bidResponse, configuration, nativeCacheId);
}

@Override
protected BidRequesterListener createBidListener(OnCompleteListener originalListener) {
return new BidRequesterListener() {
Expand All @@ -53,6 +68,7 @@ public void onFetchCompleted(BidResponse response) {
response.getWinningBidJson(),
expirationTimeSeconds != null ? expirationTimeSeconds.longValue() : null
);
nativeCacheId = cacheId;
Util.saveCacheId(cacheId, adObject);

notifyWinEvent(response);
Expand All @@ -62,6 +78,7 @@ public void onFetchCompleted(BidResponse response) {
@Override
public void onError(AdException exception) {
bidResponse = null;
nativeCacheId = null;

Util.apply(null, adObject);
originalListener.onComplete(convertToResultCode(exception));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,21 @@ public static BidInfo create(
@NonNull ResultCode resultCode,
@Nullable BidResponse bidResponse,
@Nullable AdUnitConfiguration configuration
) {
return create(resultCode, bidResponse, configuration, null);
}

/**
* Like {@link #create(ResultCode, BidResponse, AdUnitConfiguration)}, but for a native winning
* bid the caller already saved to {@link CacheManager}: reuses {@code nativeCacheId} instead of
* saving the bid again. With a {@code null} id, the winning bid is saved as usual.
*/
@NonNull
public static BidInfo create(
@NonNull ResultCode resultCode,
@Nullable BidResponse bidResponse,
@Nullable AdUnitConfiguration configuration,
@Nullable String nativeCacheId
) {
BidInfo bidInfo = new BidInfo(resultCode);
if (bidResponse == null) {
Expand Down Expand Up @@ -174,7 +189,7 @@ public static BidInfo create(

boolean isNative = configuration != null && configuration.getNativeConfiguration() != null;
if (isNative && bidInfo.resultCode == ResultCode.SUCCESS) {
final @Nullable String cacheId = CacheManager.save(
final @Nullable String cacheId = nativeCacheId != null ? nativeCacheId : CacheManager.save(
bidResponse.getWinningBidJson(),
bidInfo.exp != null ? bidInfo.exp.longValue() : null
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@

package org.prebid.mobile.api.data;

import org.prebid.mobile.LogUtil;
import org.prebid.mobile.ResultCode;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand Down Expand Up @@ -74,9 +77,83 @@ public enum FetchDemandResult {
*/
NO_CACHED_BIDS;

private static final String TAG = "FetchDemandResult";

public static final String NO_BIDS_MESSAGE = "Failed to parse bids. No winning bids were found.";
public static final String NO_CACHED_BIDS_MESSAGE = "No bids with successful Prebid Cache entries were found.";

/**
* Returns the {@link ResultCode} the original API reports for the same outcome.
*/
public ResultCode toResultCode() {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for pulling this out. The inverse mapping (ResultCode to FetchDemandResult) is still a private switch in MediationNativeAdUnit.convertResultCode, so the two directions now live in different files. A FetchDemandResult.fromResultCode(ResultCode) next to this method, covered by a round-trip test, would keep them in sync. Fine as a follow-up.

switch (this) {
case SUCCESS:
return ResultCode.SUCCESS;
case INVALID_ACCOUNT_ID:
return ResultCode.INVALID_ACCOUNT_ID;
case INVALID_CONFIG_ID:
return ResultCode.INVALID_CONFIG_ID;
case INVALID_SIZE:
return ResultCode.INVALID_SIZE;
case INVALID_CONTEXT:
return ResultCode.INVALID_CONTEXT;
case INVALID_AD_OBJECT:
return ResultCode.INVALID_AD_OBJECT;
case INVALID_HOST_URL:
return ResultCode.INVALID_HOST_URL;
case NETWORK_ERROR:
return ResultCode.NETWORK_ERROR;
case TIMEOUT:
return ResultCode.TIMEOUT;
case NO_BIDS:
return ResultCode.NO_BIDS;
case NO_CACHED_BIDS:
return ResultCode.NO_CACHED_BIDS;
case SERVER_ERROR:
default:
return ResultCode.PREBID_SERVER_ERROR;
}
}

/**
* Returns the {@link FetchDemandResult} for a {@link ResultCode} the original API reported, the
* inverse of {@link #toResultCode()}. Codes without a counterpart map to {@link #NETWORK_ERROR}.
*/
public static FetchDemandResult fromResultCode(ResultCode resultCode) {
switch (resultCode) {
case SUCCESS:
return SUCCESS;
case INVALID_ACCOUNT_ID:
return INVALID_ACCOUNT_ID;
case INVALID_CONFIG_ID:
return INVALID_CONFIG_ID;
case INVALID_CONTEXT:
return INVALID_CONTEXT;
case INVALID_HOST_URL:
return INVALID_HOST_URL;
case INVALID_SIZE:
return INVALID_SIZE;
case INVALID_AD_OBJECT:
return INVALID_AD_OBJECT;
case NO_BIDS:
return NO_BIDS;
case NO_CACHED_BIDS:
return NO_CACHED_BIDS;
case PREBID_SERVER_ERROR:
return SERVER_ERROR;
case TIMEOUT:
return TIMEOUT;
case NETWORK_ERROR:
return NETWORK_ERROR;
case INVALID_NATIVE_REQUEST:
LogUtil.error(TAG, "Invalid native request!");
return NETWORK_ERROR;
default:
LogUtil.error(TAG, "Something went wrong!");
return NETWORK_ERROR;
}
}

public static FetchDemandResult parseErrorMessage(String msg) {
Pattern storedRequestNotFound = Pattern.compile("^Invalid request: Stored Request with ID=\".*\" not found.");
Pattern storedImpNotFound = Pattern.compile("^Invalid request: Stored Imp with ID=\".*\" not found.");
Expand Down
Loading
Loading