Skip to content

Commit

Permalink
refactor: remove saveNativeResult #699
Browse files Browse the repository at this point in the history
  • Loading branch information
ValentinPostindustria committed Oct 11, 2023
1 parent 94e3af3 commit 67adf8b
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -200,11 +200,7 @@ public void fetchDemand(OnFetchDemandResult listener) {
allowNullableAdObject = true;

fetchDemand(null, resultCode -> {
BidInfo bidInfo = BidInfo.create(resultCode, bidResponse);
boolean isNative = configuration.getNativeConfiguration() != null;
if (isNative) {
BidInfo.saveNativeResult(bidInfo, bidResponse, adObject);
}
BidInfo bidInfo = BidInfo.create(resultCode, bidResponse, configuration, adObject);
listener.onComplete(bidInfo);
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.prebid.mobile.CacheManager;
import org.prebid.mobile.ResultCode;
import org.prebid.mobile.Util;
import org.prebid.mobile.configuration.AdUnitConfiguration;
import org.prebid.mobile.rendering.bidding.data.bid.Bid;
import org.prebid.mobile.rendering.bidding.data.bid.BidResponse;

Expand All @@ -16,9 +17,9 @@ public class BidInfo {
@NonNull
private final ResultCode resultCode;
@Nullable
private final Map<String, String> targetingKeywords;
private Map<String, String> targetingKeywords;
@Nullable
private final Map<String, String> events;
private Map<String, String> events;
@Nullable
private String nativeCacheId;
@Nullable
Expand All @@ -34,14 +35,8 @@ public class BidInfo {
public static final String EVENT_IMP = "ext.prebid.events.imp";


private BidInfo(
@NonNull ResultCode resultCode,
@Nullable Map<String, String> targetingKeywords,
@Nullable Map<String, String> events
) {
private BidInfo(@NonNull ResultCode resultCode) {
this.resultCode = resultCode;
this.targetingKeywords = targetingKeywords;
this.events = events;
}

@NonNull
Expand Down Expand Up @@ -71,27 +66,34 @@ public Map<String, String> getEvents() {


@NonNull
public static BidInfo create(@NonNull ResultCode resultCode, @Nullable BidResponse bidResponse) {
public static BidInfo create(
@NonNull ResultCode resultCode,
@Nullable BidResponse bidResponse,
@Nullable AdUnitConfiguration configuration,
@Nullable Object adObject
) {
BidInfo bidInfo = new BidInfo(resultCode);
if (bidResponse == null) {
return new BidInfo(resultCode, null, null);
return bidInfo;
}

bidInfo.targetingKeywords = bidResponse.getTargeting();

bidInfo.exp = bidResponse.getExpirationTimeSeconds();

Bid winningBid = bidResponse.getWinningBid();
Map<String, String> events = null;
if (winningBid != null) {
events = winningBid.getEvents();
bidInfo.events = winningBid.getEvents();
}

return new BidInfo(resultCode, bidResponse.getTargeting(), events);
}

public static void saveNativeResult(@NonNull BidInfo bidInfo, @Nullable BidResponse bidResponse, @Nullable Object adObject) {
if (bidInfo.resultCode == ResultCode.SUCCESS && bidResponse != null) {
boolean isNative = configuration != null && configuration.getNativeConfiguration() != null;
if (isNative && bidInfo.resultCode == ResultCode.SUCCESS) {
String cacheId = CacheManager.save(bidResponse.getWinningBidJson());
Util.saveCacheId(cacheId, adObject);
bidInfo.nativeCacheId = cacheId;
bidInfo.exp = bidResponse.getExpirationTimeSeconds();
}

return bidInfo;
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.prebid.mobile.api.original;

import android.annotation.SuppressLint;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

Expand All @@ -16,6 +18,7 @@
import org.prebid.mobile.VideoParameters;
import org.prebid.mobile.api.data.AdFormat;
import org.prebid.mobile.api.exceptions.AdException;
import org.prebid.mobile.configuration.AdUnitConfiguration;
import org.prebid.mobile.configuration.NativeAdUnitConfiguration;
import org.prebid.mobile.rendering.bidding.data.bid.BidResponse;
import org.prebid.mobile.rendering.bidding.listeners.BidRequesterListener;
Expand Down Expand Up @@ -135,4 +138,10 @@ public BidResponse getBidResponse() {
return bidResponse;
}

@SuppressLint("VisibleForTests")
@Override
public AdUnitConfiguration getConfiguration() {
return super.getConfiguration();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import org.prebid.mobile.OnCompleteListener2;
import org.prebid.mobile.ResultCode;
import org.prebid.mobile.api.data.BidInfo;
import org.prebid.mobile.rendering.bidding.data.bid.BidResponse;

import java.util.Map;

Expand All @@ -17,23 +16,19 @@
class OnCompleteListenerImpl implements OnCompleteListener, OnCompleteListener2 {

@Nullable
private Object adObject;
private final Object adObject;
@NonNull
private PrebidRequest request;
private final MultiformatAdUnitFacade adUnit;
@NonNull
private MultiformatAdUnitFacade adUnit;
@NonNull
private OnFetchDemandResult listener;
private final OnFetchDemandResult listener;

OnCompleteListenerImpl(
@NonNull MultiformatAdUnitFacade adUnit,
@NonNull PrebidRequest request,
@Nullable Object adObject,
@NonNull OnFetchDemandResult listener
) {
this.adObject = adObject;
this.adUnit = adUnit;
this.request = request;
this.listener = listener;
}

Expand All @@ -49,12 +44,7 @@ public void onComplete(ResultCode resultCode, @Nullable Map<String, String> unmo


private void notifyListener(ResultCode resultCode) {
BidResponse bidResponse = adUnit.getBidResponse();
BidInfo bidInfo = BidInfo.create(resultCode, bidResponse);
boolean isNative = request.getNativeParameters() != null;
if (isNative) {
BidInfo.saveNativeResult(bidInfo, bidResponse, adObject);
}
BidInfo bidInfo = BidInfo.create(resultCode, adUnit.getBidResponse(), adUnit.getConfiguration(), adObject);
listener.onComplete(bidInfo);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ private void baseFetchDemand(
}

if (request == null || requestDoesNotHaveAnyConfiguration(request)) {
userListener.onComplete(BidInfo.create(ResultCode.INVALID_PREBID_REQUEST_OBJECT, null));
userListener.onComplete(BidInfo.create(ResultCode.INVALID_PREBID_REQUEST_OBJECT, null, null, null));
return;
}

Expand All @@ -85,7 +85,7 @@ private void baseFetchDemand(

adUnit = new MultiformatAdUnitFacade(configId, request);

OnCompleteListenerImpl innerListener = new OnCompleteListenerImpl(adUnit, request, adObject, userListener);
OnCompleteListenerImpl innerListener = new OnCompleteListenerImpl(adUnit, request, userListener);
if (adObject != null) {
adUnit.fetchDemand(adObject, innerListener);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

import android.util.Base64;

import androidx.annotation.Nullable;

import org.json.JSONArray;
import org.json.JSONObject;
import org.prebid.mobile.api.data.BidInfo;
Expand Down Expand Up @@ -128,6 +130,7 @@ public class Bid {
// wait between the auction and the actual impression
private int exp;

@Nullable
private Map<String, String> events;

private MobileSdkPassThrough mobileSdkPassThrough;
Expand Down Expand Up @@ -254,6 +257,7 @@ public MobileSdkPassThrough getMobileSdkPassThrough() {
return mobileSdkPassThrough;
}

@Nullable
public Map<String, String> getEvents() {
return events;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ public void destroy() throws Exception {
public void emptyBidResponse() {
OnCompleteListenerImpl subject = new OnCompleteListenerImpl(
mockAdUnit,
mockPrebidRequest,
null,
mockListener
);
Expand All @@ -74,7 +73,6 @@ public void emptyBidResponse() {
public void fullBidResponse() {
OnCompleteListenerImpl subject = new OnCompleteListenerImpl(
mockAdUnit,
mockPrebidRequest,
null,
mockListener
);
Expand All @@ -83,6 +81,7 @@ public void fullBidResponse() {
keywords.put("key1", "value1");
keywords.put("key2", "value2");
when(mockBidResponse.getTargeting()).thenReturn(keywords);
when(mockBidResponse.getExpirationTimeSeconds()).thenReturn(null);
when(mockAdUnit.getBidResponse()).thenReturn(mockBidResponse);

subject.onComplete(ResultCode.SUCCESS);
Expand All @@ -103,7 +102,6 @@ public void fullBidResponse() {
public void fullBidResponseWithNative() {
OnCompleteListenerImpl subject = new OnCompleteListenerImpl(
mockAdUnit,
mockPrebidRequest,
null,
mockListener
);
Expand Down

0 comments on commit 67adf8b

Please sign in to comment.