Skip to content

Commit

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

fetchDemand(null, resultCode -> {
BidInfo bidInfo = BidInfo.create(resultCode, bidResponse, configuration, adObject);
BidInfo bidInfo = BidInfo.create(resultCode, bidResponse);

boolean isNativeAndSuccessful = configuration.getNativeConfiguration() != null && resultCode == ResultCode.SUCCESS && bidResponse != null;
if (isNativeAndSuccessful) {
String cacheId = CacheManager.save(bidResponse.getWinningBidJson());
Util.saveCacheId(cacheId, adObject);
bidInfo.setNativeCacheId(cacheId);
}

listener.onComplete(bidInfo);
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

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 Down Expand Up @@ -54,6 +51,10 @@ public String getNativeCacheId() {
return nativeCacheId;
}

public void setNativeCacheId(@Nullable String nativeCacheId) {
this.nativeCacheId = nativeCacheId;
}

@Nullable
public Integer getExp() {
return exp;
Expand All @@ -68,9 +69,7 @@ public Map<String, String> getEvents() {
@NonNull
public static BidInfo create(
@NonNull ResultCode resultCode,
@Nullable BidResponse bidResponse,
@Nullable AdUnitConfiguration configuration,
@Nullable Object adObject
@Nullable BidResponse bidResponse
) {
BidInfo bidInfo = new BidInfo(resultCode);
if (bidResponse == null) {
Expand All @@ -86,13 +85,6 @@ public static BidInfo create(
bidInfo.events = winningBid.getEvents();
}

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;
}

return bidInfo;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import org.prebid.mobile.CacheManager;
import org.prebid.mobile.OnCompleteListener;
import org.prebid.mobile.OnCompleteListener2;
import org.prebid.mobile.ResultCode;
import org.prebid.mobile.Util;
import org.prebid.mobile.api.data.BidInfo;

import java.util.Map;
Expand Down Expand Up @@ -44,7 +46,15 @@ public void onComplete(ResultCode resultCode, @Nullable Map<String, String> unmo


private void notifyListener(ResultCode resultCode) {
BidInfo bidInfo = BidInfo.create(resultCode, adUnit.getBidResponse(), adUnit.getConfiguration(), adObject);
BidInfo bidInfo = BidInfo.create(resultCode, adUnit.getBidResponse());

boolean isNativeAndSuccessful = adUnit.getConfiguration().getNativeConfiguration() != null && resultCode == ResultCode.SUCCESS && adUnit.getBidResponse() != null;
if (isNativeAndSuccessful) {
String cacheId = CacheManager.save(adUnit.getBidResponse().getWinningBidJson());
Util.saveCacheId(cacheId, adObject);
bidInfo.setNativeCacheId(cacheId);
}

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, null, null));
userListener.onComplete(BidInfo.create(ResultCode.INVALID_PREBID_REQUEST_OBJECT, null));
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
import org.prebid.mobile.NativeParameters;
import org.prebid.mobile.ResultCode;
import org.prebid.mobile.api.data.BidInfo;
import org.prebid.mobile.configuration.AdUnitConfiguration;
import org.prebid.mobile.configuration.NativeAdUnitConfiguration;
import org.prebid.mobile.rendering.bidding.data.bid.Bid;
import org.prebid.mobile.rendering.bidding.data.bid.BidResponse;

Expand Down Expand Up @@ -54,6 +56,7 @@ public void emptyBidResponse() {
);

when(mockAdUnit.getBidResponse()).thenReturn(null);
when(mockAdUnit.getConfiguration()).thenReturn(mock(AdUnitConfiguration.class));

subject.onComplete(ResultCode.NO_BIDS);

Expand All @@ -76,6 +79,7 @@ public void fullBidResponse() {
null,
mockListener
);
when(mockAdUnit.getConfiguration()).thenReturn(mock(AdUnitConfiguration.class));

HashMap<String, String> keywords = new HashMap<>();
keywords.put("key1", "value1");
Expand Down Expand Up @@ -115,6 +119,9 @@ public void fullBidResponseWithNative() {
Bid mockBid = mock(Bid.class);
when(mockBid.getEvents()).thenReturn(events);

AdUnitConfiguration mockConfiguration = mock(AdUnitConfiguration.class);
when(mockConfiguration.getNativeConfiguration()).thenReturn(mock(NativeAdUnitConfiguration.class));
when(mockAdUnit.getConfiguration()).thenReturn(mockConfiguration);
when(mockPrebidRequest.getNativeParameters()).thenReturn(mock(NativeParameters.class));
when(mockBidResponse.getTargeting()).thenReturn(keywords);
when(mockAdUnit.getBidResponse()).thenReturn(mockBidResponse);
Expand Down

0 comments on commit 010d24a

Please sign in to comment.