@@ -94,6 +94,110 @@ public Result<List<HttpRequest<BidRequest>>> makeHttpRequests(BidRequest bidRequ
9494 return Result .withValue (BidderUtil .defaultRequest (outgoingRequest , endpointUrl , mapper ));
9595 }
9696
97+ private ExtImpRtbhouse parseImpExt (Imp imp ) {
98+ try {
99+ return mapper .mapper ().convertValue (imp .getExt (), RTBHOUSE_EXT_TYPE_REFERENCE ).getBidder ();
100+ } catch (IllegalArgumentException e ) {
101+ throw new PreBidException (e .getMessage ());
102+ }
103+ }
104+
105+ private Price resolveBidFloor (Imp imp , ExtImpRtbhouse impExt , BidRequest bidRequest ) {
106+ final List <String > brCur = bidRequest .getCur ();
107+ final Price initialBidFloorPrice = Price .of (imp .getBidfloorcur (), imp .getBidfloor ());
108+
109+ final BigDecimal impExtBidFloor = impExt .getBidFloor ();
110+ final String impExtCurrency = impExtBidFloor != null && brCur != null && !brCur .isEmpty ()
111+ ? brCur .getFirst () : null ;
112+ final Price impExtBidFloorPrice = Price .of (impExtCurrency , impExtBidFloor );
113+ final Price resolvedPrice = initialBidFloorPrice .getValue () == null
114+ ? impExtBidFloorPrice : initialBidFloorPrice ;
115+
116+ return BidderUtil .isValidPrice (resolvedPrice )
117+ && !StringUtils .equalsIgnoreCase (resolvedPrice .getCurrency (), BIDDER_CURRENCY )
118+ ? convertBidFloor (resolvedPrice , imp .getId (), bidRequest )
119+ : resolvedPrice ;
120+ }
121+
122+ private Price convertBidFloor (Price bidFloorPrice , String impId , BidRequest bidRequest ) {
123+ final String bidFloorCur = bidFloorPrice .getCurrency ();
124+ try {
125+ final BigDecimal convertedPrice = currencyConversionService
126+ .convertCurrency (bidFloorPrice .getValue (), bidRequest , bidFloorCur , BIDDER_CURRENCY );
127+
128+ return Price .of (BIDDER_CURRENCY , convertedPrice );
129+ } catch (PreBidException e ) {
130+ throw new PreBidException (String .format (
131+ "Unable to convert provided bid floor currency from %s to %s for imp `%s`" ,
132+ bidFloorCur , BIDDER_CURRENCY , impId ));
133+ }
134+ }
135+
136+ private static Imp modifyImp (Imp imp , Price bidFloorPrice ) {
137+ return imp .toBuilder ()
138+ .tagid (extractTagId (imp ))
139+ .bidfloorcur (ObjectUtil .getIfNotNull (bidFloorPrice , Price ::getCurrency ))
140+ .bidfloor (ObjectUtil .getIfNotNull (bidFloorPrice , Price ::getValue ))
141+ .pmp (null )
142+ .build ();
143+ }
144+
145+ private static String extractTagId (Imp imp ) {
146+ return Optional .ofNullable (imp .getTagid ())
147+ .filter (StringUtils ::isNotBlank )
148+ .or (() -> extractGpid (imp ))
149+ .or (() -> extractAdslot (imp ))
150+ .or (() -> extractPbadslot (imp ))
151+ .or (() -> Optional .ofNullable (imp .getId ())
152+ .filter (StringUtils ::isNotBlank ))
153+ .orElse (null );
154+ }
155+
156+ private static Optional <String > extractGpid (Imp imp ) {
157+ return Optional .ofNullable (imp .getExt ())
158+ .map (ext -> ext .get ("gpid" ))
159+ .map (JsonNode ::textValue )
160+ .filter (StringUtils ::isNotBlank );
161+ }
162+
163+ private static Optional <String > extractAdslot (Imp imp ) {
164+ return Optional .ofNullable (imp .getExt ())
165+ .map (ext -> ext .get ("data" ))
166+ .map (data -> data .get ("adserver" ))
167+ .map (adserver -> adserver .get ("adslot" ))
168+ .map (JsonNode ::textValue )
169+ .filter (StringUtils ::isNotBlank );
170+ }
171+
172+ private static Optional <String > extractPbadslot (Imp imp ) {
173+ return Optional .ofNullable (imp .getExt ())
174+ .map (ext -> ext .get ("data" ))
175+ .map (data -> data .get ("pbadslot" ))
176+ .map (JsonNode ::textValue )
177+ .filter (StringUtils ::isNotBlank );
178+ }
179+
180+ private Site modifySite (Site site , String publisherId ) {
181+ final ObjectNode prebidNode = mapper .mapper ().createObjectNode ();
182+ prebidNode .put ("publisherId" , publisherId );
183+
184+ final ExtPublisher extPublisher = ExtPublisher .empty ();
185+ extPublisher .addProperty ("prebid" , prebidNode );
186+
187+ final Publisher publisher = Optional .ofNullable (site )
188+ .map (Site ::getPublisher )
189+ .map (Publisher ::toBuilder )
190+ .orElseGet (Publisher ::builder )
191+ .ext (extPublisher )
192+ .build ();
193+
194+ return Optional .ofNullable (site )
195+ .map (Site ::toBuilder )
196+ .orElseGet (Site ::builder )
197+ .publisher (publisher )
198+ .build ();
199+ }
200+
97201 @ Override
98202 public Result <List <BidderBid >> makeBids (BidderCall <BidRequest > httpCall , BidRequest bidRequest ) {
99203 try {
@@ -144,23 +248,6 @@ private BidderBid resolveBidderBid(Bid bid,
144248 .build ();
145249 }
146250
147- private String resolveNativeAdm (String adm , List <BidderError > bidderErrors ) {
148- final JsonNode admNode ;
149- try {
150- admNode = mapper .mapper ().readTree (adm );
151- } catch (JsonProcessingException e ) {
152- bidderErrors .add (BidderError .badServerResponse ("Unable to parse native adm: %s" .formatted (adm )));
153- return null ;
154- }
155-
156- final JsonNode nativeNode = admNode .get ("native" );
157- if (nativeNode != null ) {
158- return nativeNode .toString ();
159- }
160-
161- return adm ;
162- }
163-
164251 private static BidType getBidType (String impId , List <Imp > imps ) {
165252 for (Imp imp : imps ) {
166253 if (imp .getId ().equals (impId )) {
@@ -176,51 +263,21 @@ private static BidType getBidType(String impId, List<Imp> imps) {
176263 return BidType .banner ;
177264 }
178265
179- private ExtImpRtbhouse parseImpExt (Imp imp ) {
266+ private String resolveNativeAdm (String adm , List <BidderError > bidderErrors ) {
267+ final JsonNode admNode ;
180268 try {
181- return mapper .mapper ().convertValue (imp .getExt (), RTBHOUSE_EXT_TYPE_REFERENCE ).getBidder ();
182- } catch (IllegalArgumentException e ) {
183- throw new PreBidException (e .getMessage ());
269+ admNode = mapper .mapper ().readTree (adm );
270+ } catch (JsonProcessingException e ) {
271+ bidderErrors .add (BidderError .badServerResponse ("Unable to parse native adm: %s" .formatted (adm )));
272+ return null ;
184273 }
185- }
186274
187- private static Imp modifyImp (Imp imp , Price bidFloorPrice ) {
188- return imp .toBuilder ()
189- .bidfloorcur (ObjectUtil .getIfNotNull (bidFloorPrice , Price ::getCurrency ))
190- .bidfloor (ObjectUtil .getIfNotNull (bidFloorPrice , Price ::getValue ))
191- .pmp (null )
192- .build ();
193- }
194-
195- private Price resolveBidFloor (Imp imp , ExtImpRtbhouse impExt , BidRequest bidRequest ) {
196- final List <String > brCur = bidRequest .getCur ();
197- final Price initialBidFloorPrice = Price .of (imp .getBidfloorcur (), imp .getBidfloor ());
198-
199- final BigDecimal impExtBidFloor = impExt .getBidFloor ();
200- final String impExtCurrency = impExtBidFloor != null && brCur != null && !brCur .isEmpty ()
201- ? brCur .getFirst () : null ;
202- final Price impExtBidFloorPrice = Price .of (impExtCurrency , impExtBidFloor );
203- final Price resolvedPrice = initialBidFloorPrice .getValue () == null
204- ? impExtBidFloorPrice : initialBidFloorPrice ;
205-
206- return BidderUtil .isValidPrice (resolvedPrice )
207- && !StringUtils .equalsIgnoreCase (resolvedPrice .getCurrency (), BIDDER_CURRENCY )
208- ? convertBidFloor (resolvedPrice , imp .getId (), bidRequest )
209- : resolvedPrice ;
210- }
211-
212- private Price convertBidFloor (Price bidFloorPrice , String impId , BidRequest bidRequest ) {
213- final String bidFloorCur = bidFloorPrice .getCurrency ();
214- try {
215- final BigDecimal convertedPrice = currencyConversionService
216- .convertCurrency (bidFloorPrice .getValue (), bidRequest , bidFloorCur , BIDDER_CURRENCY );
217-
218- return Price .of (BIDDER_CURRENCY , convertedPrice );
219- } catch (PreBidException e ) {
220- throw new PreBidException (String .format (
221- "Unable to convert provided bid floor currency from %s to %s for imp `%s`" ,
222- bidFloorCur , BIDDER_CURRENCY , impId ));
275+ final JsonNode nativeNode = admNode .get ("native" );
276+ if (nativeNode != null ) {
277+ return nativeNode .toString ();
223278 }
279+
280+ return adm ;
224281 }
225282
226283 private static Bid resolveMacros (Bid bid ) {
@@ -232,25 +289,4 @@ private static Bid resolveMacros(Bid bid) {
232289 .adm (StringUtils .replace (bid .getAdm (), PRICE_MACRO , priceAsString ))
233290 .build ();
234291 }
235-
236- private Site modifySite (Site site , String publisherId ) {
237- final ObjectNode prebidNode = mapper .mapper ().createObjectNode ();
238- prebidNode .put ("publisherId" , publisherId );
239-
240- final ExtPublisher extPublisher = ExtPublisher .empty ();
241- extPublisher .addProperty ("prebid" , prebidNode );
242-
243- final Publisher publisher = Optional .ofNullable (site )
244- .map (Site ::getPublisher )
245- .map (Publisher ::toBuilder )
246- .orElseGet (Publisher ::builder )
247- .ext (extPublisher )
248- .build ();
249-
250- return Optional .ofNullable (site )
251- .map (Site ::toBuilder )
252- .orElseGet (Site ::builder )
253- .publisher (publisher )
254- .build ();
255- }
256292}
0 commit comments