@@ -66,14 +66,14 @@ public Result<List<HttpRequest<BidRequest>>> makeHttpRequests(BidRequest request
6666 }
6767
6868 private Imp modifyImp (Imp imp ) {
69- final ExtImpTrustx impExt = tryParseImpExt (imp );
69+ final ExtImpTrustx impExt = parseImpExt (imp );
7070
71- return impExt == null ? imp : imp . toBuilder ()
72- . ext (mapper .mapper ().valueToTree (modifyImpExt (impExt )))
73- . build () ;
71+ return impExt != null
72+ ? imp . toBuilder (). ext (mapper .mapper ().valueToTree (modifyImpExt (impExt ))). build ( )
73+ : imp ;
7474 }
7575
76- private ExtImpTrustx tryParseImpExt (Imp imp ) {
76+ private ExtImpTrustx parseImpExt (Imp imp ) {
7777 try {
7878 return mapper .mapper ().convertValue (imp .getExt (), ExtImpTrustx .class );
7979 } catch (IllegalArgumentException e ) {
@@ -88,9 +88,7 @@ private ExtImpTrustx modifyImpExt(ExtImpTrustx impExt) {
8888 .filter (StringUtils ::isNotEmpty )
8989 .orElse (null );
9090
91- return impExt .toBuilder ()
92- .gpid (adSlot != null ? adSlot : impExt .getGpid ())
93- .build ();
91+ return adSlot != null ? impExt .toBuilder ().gpid (adSlot ).build () : impExt ;
9492 }
9593
9694 private MultiMap makeHeaders (BidRequest request ) {
@@ -100,9 +98,9 @@ private MultiMap makeHeaders(BidRequest request) {
10098
10199 final Device device = request .getDevice ();
102100 final String ip = StringUtils .firstNonEmpty (
103- ObjectUtil . getIfNotNull ( device , Device :: getIpv6 ) ,
104- ObjectUtil . getIfNotNull ( device , Device :: getIp ) );
105- final String userAgent = ObjectUtil . getIfNotNull ( device , Device :: getUa ) ;
101+ device != null ? device . getIpv6 () : null ,
102+ device != null ? device . getIp () : null );
103+ final String userAgent = device != null ? device . getUa () : null ;
106104
107105 final MultiMap headers = HttpUtil .headers ();
108106
@@ -144,81 +142,82 @@ private Result<List<BidderBid>> bidsFromResponse(BidResponse bidResponse) {
144142 .filter (Objects ::nonNull )
145143 .flatMap (Collection ::stream )
146144 .filter (Objects ::nonNull )
147- .map (bid -> makeBid (bid , errors ))
145+ .map (bid -> makeBidderBid (bid , errors ))
148146 .filter (Objects ::nonNull )
149147 .toList ();
150148
151149 return Result .of (bidderBids , errors );
152150 }
153151
154- private BidderBid makeBid (Bid bid , List <BidderError > errors ) {
152+ private BidderBid makeBidderBid (Bid bid , List <BidderError > errors ) {
153+ final BidType bidType ;
155154 try {
156- final BidType bidType = getBidType (bid );
157-
158- final ExtBidPrebidVideo videoInfo = (bidType == BidType .video ) ? ExtBidPrebidVideo .of (
159- bid .getDur () != null && bid .getDur () > 0 ? bid .getDur () : null ,
160- CollectionUtils .isNotEmpty (bid .getCat ()) ? bid .getCat ().getFirst () : null
161- ) : null ;
162-
163- final Bid modifiedBid = Optional .ofNullable (tryParseBidExt (bid .getExt ()))
164- .map (TrustxBidder ::modifyBidExt )
165- .map (mapper .mapper ()::<ObjectNode >valueToTree )
166- .map (extBid -> bid .toBuilder ().ext (extBid ).build ())
167- .orElse (bid );
168-
169- return BidderBid .builder ()
170- .bid (modifiedBid )
171- .type (bidType )
172- .videoInfo (videoInfo )
173- .build ();
155+ bidType = getBidType (bid );
174156 } catch (PreBidException e ) {
175157 errors .add (BidderError .badInput (e .getMessage ()));
176158 return null ;
177159 }
160+
161+ return BidderBid .builder ()
162+ .bid (modifyBid (bid ))
163+ .type (bidType )
164+ .videoInfo (bidType == BidType .video ? makeExtBidPrebidVideo (bid ) : null )
165+ .build ();
178166 }
179167
180- private static BidType getBidType (Bid bid ) {
181- final Integer markupType = bid .getMtype ( );
182- if (markupType == null ) {
183- throw new PreBidException ( "Missing MType for bid: " + bid . getId ()) ;
168+ private Bid modifyBid (Bid bid ) {
169+ final ExtPrebid < ExtBidPrebid , ExtBidBidderTrustx > ext = parseBidExt ( bid .getExt () );
170+ if (ext == null ) {
171+ return bid ;
184172 }
185173
186- return switch (markupType ) {
187- case 1 -> BidType .banner ;
188- case 2 -> BidType .video ;
189- default -> throw new PreBidException ("Unsupported MType: %d" .formatted (markupType ));
190- };
174+ final ExtBidBidderTrustx extBidder = ext .getBidder ();
175+ final String networkName = Optional .ofNullable (extBidder )
176+ .map (ExtBidBidderTrustx ::getTrustx )
177+ .map (ExtBidTrustx ::getNetworkName )
178+ .filter (StringUtils ::isNotEmpty )
179+ .orElse (null );
180+ if (networkName == null ) {
181+ return bid ;
182+ }
183+
184+ final ExtBidPrebid modifiedExtPrebid = Optional .ofNullable (ext .getPrebid ())
185+ .map (ExtBidPrebid ::toBuilder )
186+ .orElseGet (ExtBidPrebid ::builder )
187+ .meta (ExtBidPrebidMeta .builder ().networkName (networkName ).build ())
188+ .build ();
189+ final ObjectNode modifiedExt = mapper .mapper ().valueToTree (ExtPrebid .of (modifiedExtPrebid , extBidder ));
190+
191+ return bid .toBuilder ().ext (modifiedExt ).build ();
191192 }
192193
193- private ExtPrebid <ExtBidPrebid , ExtBidBidderTrustx > tryParseBidExt (ObjectNode bidExt ) {
194+ private ExtPrebid <ExtBidPrebid , ExtBidBidderTrustx > parseBidExt (ObjectNode bidExt ) {
194195 try {
195196 return mapper .mapper ().convertValue (bidExt , TRUSTX_BID_EXT_TYPE_REFERENCE );
196197 } catch (IllegalArgumentException e ) {
197198 return null ;
198199 }
199200 }
200201
201- private static ExtPrebid <ExtBidPrebid , ExtBidBidderTrustx > modifyBidExt (
202- ExtPrebid <ExtBidPrebid , ExtBidBidderTrustx > extBid ) {
202+ private static BidType getBidType (Bid bid ) {
203+ final Integer markupType = bid .getMtype ();
204+ if (markupType == null ) {
205+ throw new PreBidException ("Missing MType for bid: " + bid .getId ());
206+ }
203207
204- return Optional .ofNullable (extBid .getBidder ())
205- .map (ExtBidBidderTrustx ::getTrustx )
206- .map (ExtBidTrustx ::getNetworkName )
207- .filter (StringUtils ::isNotEmpty )
208- .map (networkName -> ExtBidPrebidMeta .builder ().networkName (networkName ).build ())
209- .map (extBidPrebidMeta -> modifyBidExtMeta (extBid , extBidPrebidMeta ))
210- .orElse (extBid );
208+ return switch (markupType ) {
209+ case 1 -> BidType .banner ;
210+ case 2 -> BidType .video ;
211+ default -> throw new PreBidException ("Unsupported MType: %d" .formatted (markupType ));
212+ };
211213 }
212214
213- private static ExtPrebid <ExtBidPrebid , ExtBidBidderTrustx > modifyBidExtMeta (
214- ExtPrebid <ExtBidPrebid , ExtBidBidderTrustx > extBid , ExtBidPrebidMeta extBidPrebidMeta ) {
215-
216- final ExtBidPrebid updatedExtBidPrebid = Optional .ofNullable (extBid .getPrebid ())
217- .map (ExtBidPrebid ::toBuilder )
218- .orElseGet (ExtBidPrebid ::builder )
219- .meta (extBidPrebidMeta )
220- .build ();
215+ private static ExtBidPrebidVideo makeExtBidPrebidVideo (Bid bid ) {
216+ final Integer dur = bid .getDur ();
217+ final List <String > cat = bid .getCat ();
221218
222- return ExtPrebid .of (updatedExtBidPrebid , extBid .getBidder ());
219+ return ExtBidPrebidVideo .of (
220+ dur != null && dur > 0 ? dur : null ,
221+ CollectionUtils .isNotEmpty (cat ) ? cat .getFirst () : null );
223222 }
224223}
0 commit comments