66import com .iab .openrtb .request .Imp ;
77import com .iab .openrtb .response .Bid ;
88import org .apache .commons .collections4 .CollectionUtils ;
9- import org .apache .commons .lang3 .StringUtils ;
9+ import org .apache .commons .lang3 .tuple . Pair ;
1010import org .prebid .server .bidder .Bidder ;
1111import org .prebid .server .bidder .model .BidderBid ;
1212import org .prebid .server .bidder .model .BidderCall ;
1313import org .prebid .server .bidder .model .BidderError ;
1414import org .prebid .server .bidder .model .HttpRequest ;
15- import org .prebid .server .bidder .model .Price ;
1615import org .prebid .server .bidder .model .Result ;
1716import org .prebid .server .bidder .stroeercore .model .StroeerCoreBid ;
1817import org .prebid .server .bidder .stroeercore .model .StroeerCoreBidResponse ;
19- import org .prebid .server .currency .CurrencyConversionService ;
2018import org .prebid .server .exception .PreBidException ;
2119import org .prebid .server .json .DecodeException ;
2220import org .prebid .server .json .JacksonMapper ;
2624import org .prebid .server .util .BidderUtil ;
2725import org .prebid .server .util .HttpUtil ;
2826
29- import java .math .BigDecimal ;
3027import java .util .ArrayList ;
3128import java .util .Collections ;
3229import java .util .List ;
@@ -41,14 +38,10 @@ public class StroeerCoreBidder implements Bidder<BidRequest> {
4138
4239 private final String endpointUrl ;
4340 private final JacksonMapper mapper ;
44- private final CurrencyConversionService currencyConversionService ;
4541
46- public StroeerCoreBidder (String endpointUrl ,
47- JacksonMapper mapper ,
48- CurrencyConversionService currencyConversionService ) {
42+ public StroeerCoreBidder (String endpointUrl , JacksonMapper mapper ) {
4943 this .endpointUrl = HttpUtil .validateUrl (endpointUrl );
5044 this .mapper = Objects .requireNonNull (mapper );
51- this .currencyConversionService = Objects .requireNonNull (currencyConversionService );
5245 }
5346
5447 @ Override
@@ -57,37 +50,20 @@ public Result<List<HttpRequest<BidRequest>>> makeHttpRequests(BidRequest bidRequ
5750 final List <BidderError > errors = new ArrayList <>();
5851
5952 for (Imp imp : bidRequest .getImp ()) {
60- final ExtImpStroeerCore impExt ;
61- final Price price ;
62-
6353 try {
64- validateImp (imp );
65-
66- impExt = parseImpExt (imp );
67- validateImpExt (impExt );
68-
69- price = convertBidFloor (bidRequest , imp );
54+ final ExtImpStroeerCore impExt = parseImpExt (imp );
55+ modifiedImps .add (imp .toBuilder ().tagid (impExt .getSlotId ()).build ());
7056 } catch (PreBidException e ) {
7157 errors .add (BidderError .badInput ("%s. Ignore imp id = %s." .formatted (e .getMessage (), imp .getId ())));
72- continue ;
7358 }
74-
75- modifiedImps .add (modifyImp (imp , impExt , price ));
7659 }
7760
7861 if (modifiedImps .isEmpty ()) {
7962 return Result .withErrors (errors );
8063 }
8164
8265 final BidRequest outgoingRequest = bidRequest .toBuilder ().imp (modifiedImps ).build ();
83-
84- return createHttpRequests (errors , outgoingRequest );
85- }
86-
87- private static void validateImp (Imp imp ) {
88- if (imp .getBanner () == null && imp .getVideo () == null ) {
89- throw new PreBidException ("Expected banner or video impression" );
90- }
66+ return Result .withValue (BidderUtil .defaultRequest (outgoingRequest , endpointUrl , mapper ));
9167 }
9268
9369 private ExtImpStroeerCore parseImpExt (Imp imp ) {
@@ -98,95 +74,65 @@ private ExtImpStroeerCore parseImpExt(Imp imp) {
9874 }
9975 }
10076
101- private static void validateImpExt (ExtImpStroeerCore impExt ) {
102- if (StringUtils .isBlank (impExt .getSlotId ())) {
103- throw new PreBidException ("Custom param slot id (sid) is empty" );
104- }
105- }
106-
107- private Price convertBidFloor (BidRequest bidRequest , Imp imp ) {
108- final BigDecimal bidFloor = imp .getBidfloor ();
109- final String bidFloorCurrency = imp .getBidfloorcur ();
110-
111- if (!shouldConvertBidFloor (bidFloor , bidFloorCurrency )) {
112- return Price .of (bidFloorCurrency , bidFloor );
113- }
114-
115- final BigDecimal convertedBidFloor = currencyConversionService .convertCurrency (
116- bidFloor , bidRequest , bidFloorCurrency , BIDDER_CURRENCY );
117-
118- return Price .of (BIDDER_CURRENCY , convertedBidFloor );
119- }
120-
121- private Result <List <HttpRequest <BidRequest >>> createHttpRequests (List <BidderError > errors , BidRequest bidRequest ) {
122- return Result .of (Collections .singletonList (BidderUtil .defaultRequest (bidRequest , endpointUrl , mapper )), errors );
123- }
124-
125- private static boolean shouldConvertBidFloor (BigDecimal bidFloor , String bidFloorCurrency ) {
126- return BidderUtil .isValidPrice (bidFloor ) && !StringUtils .equalsIgnoreCase (bidFloorCurrency , BIDDER_CURRENCY );
127- }
128-
129- private static Imp modifyImp (Imp imp , ExtImpStroeerCore impExt , Price price ) {
130- return imp .toBuilder ()
131- .bidfloorcur (price .getCurrency ())
132- .bidfloor (price .getValue ())
133- .tagid (impExt .getSlotId ())
134- .build ();
135- }
136-
13777 @ Override
13878 public Result <List <BidderBid >> makeBids (BidderCall <BidRequest > httpCall , BidRequest bidRequest ) {
13979 try {
14080 final String body = httpCall .getResponse ().getBody ();
81+ final List <BidderError > errors = new ArrayList <>();
14182 final StroeerCoreBidResponse bidResponse = mapper .decodeValue (body , StroeerCoreBidResponse .class );
142- return Result .withValues (extractBids (httpCall . getRequest (). getPayload (), bidResponse ) );
83+ return Result .of (extractBids (bidResponse , errors ), errors );
14384 } catch (DecodeException e ) {
14485 return Result .withError (BidderError .badServerResponse (e .getMessage ()));
14586 }
14687 }
14788
148- private List <BidderBid > extractBids (BidRequest bidRequest , StroeerCoreBidResponse bidResponse ) {
89+ private static Pair <BidType , Integer > getBidType (String mtype ) {
90+ return switch (mtype ) {
91+ case "banner" -> Pair .of (BidType .banner , 1 );
92+ case "video" -> Pair .of (BidType .video , 2 );
93+ default -> null ;
94+ };
95+ }
96+
97+ private List <BidderBid > extractBids (StroeerCoreBidResponse bidResponse , List <BidderError > errors ) {
14998 if (bidResponse == null || CollectionUtils .isEmpty (bidResponse .getBids ())) {
15099 return Collections .emptyList ();
151100 }
152101
153102 return bidResponse .getBids ().stream ()
154103 .filter (Objects ::nonNull )
155- .map (stroeerCoreBid -> toBidderBid (bidRequest , stroeerCoreBid ))
104+ .map (stroeerCoreBid -> toBidderBid (stroeerCoreBid , errors ))
105+ .filter (Objects ::nonNull )
156106 .toList ();
157107 }
158108
159- private BidderBid toBidderBid (BidRequest bidRequest , StroeerCoreBid stroeercoreBid ) {
109+ private BidderBid toBidderBid (StroeerCoreBid stroeercoreBid , List <BidderError > errors ) {
110+ final Pair <BidType , Integer > bidType = getBidType (stroeercoreBid .getMtype ());
111+ if (bidType == null ) {
112+ errors .add (BidderError .badServerResponse (
113+ "Bid media type error: unable to determine media type for bid with id \" %s\" "
114+ .formatted (stroeercoreBid .getBidId ())));
115+ return null ;
116+ }
117+
160118 final ObjectNode bidExt = stroeercoreBid .getDsa () != null
161119 ? mapper .mapper ().createObjectNode ().set ("dsa" , stroeercoreBid .getDsa ())
162120 : null ;
163121
164122 return BidderBid .of (
165123 Bid .builder ()
166124 .id (stroeercoreBid .getId ())
167- .impid (stroeercoreBid .getImpId ())
125+ .impid (stroeercoreBid .getBidId ())
168126 .w (stroeercoreBid .getWidth ())
169127 .h (stroeercoreBid .getHeight ())
170128 .price (stroeercoreBid .getCpm ())
171129 .adm (stroeercoreBid .getAdMarkup ())
172130 .crid (stroeercoreBid .getCreativeId ())
131+ .adomain (stroeercoreBid .getAdomain ())
132+ .mtype (bidType .getRight ())
173133 .ext (bidExt )
174134 .build (),
175- getBidType ( stroeercoreBid . getImpId (), bidRequest . getImp () ),
135+ bidType . getLeft ( ),
176136 BIDDER_CURRENCY );
177137 }
178-
179- private static BidType getBidType (String impId , List <Imp > imps ) {
180- for (Imp imp : imps ) {
181- if (imp .getId ().equals (impId )) {
182- if (imp .getBanner () != null ) {
183- return BidType .banner ;
184- } else if (imp .getVideo () != null ) {
185- return BidType .video ;
186- }
187- }
188- }
189-
190- return BidType .banner ;
191- }
192138}
0 commit comments