diff --git a/adapters/sovrn/sovrn.go b/adapters/sovrn/sovrn.go index 70323b834b2..5c98f19a176 100644 --- a/adapters/sovrn/sovrn.go +++ b/adapters/sovrn/sovrn.go @@ -39,11 +39,11 @@ func (s *SovrnAdapter) MakeRequests(request *openrtb2.BidRequest, reqInfo *adapt } } - errs := make([]error, 0, 1) + errs := make([]error, 0, len(request.Imp)) var err error - var validImps int = 0 + validImps := make([]openrtb2.Imp, 0, len(request.Imp)) - for index, imp := range request.Imp { + for _, imp := range request.Imp { var bidderExt adapters.ExtImpBidder if err := json.Unmarshal(imp.Ext, &bidderExt); err != nil { errs = append(errs, &errortypes.BadInput{ @@ -60,7 +60,7 @@ func (s *SovrnAdapter) MakeRequests(request *openrtb2.BidRequest, reqInfo *adapt continue } - tagId := getTagid(sovrnExt) + tagId := getTagId(sovrnExt) if tagId == "" { errs = append(errs, &errortypes.BadInput{ Message: "Missing required parameter 'tagid'", @@ -68,7 +68,7 @@ func (s *SovrnAdapter) MakeRequests(request *openrtb2.BidRequest, reqInfo *adapt continue } - imp.TagID = getTagid(sovrnExt) + imp.TagID = tagId if imp.BidFloor == 0 && sovrnExt.BidFloor > 0 { imp.BidFloor = sovrnExt.BidFloor @@ -88,14 +88,15 @@ func (s *SovrnAdapter) MakeRequests(request *openrtb2.BidRequest, reqInfo *adapt } } - validImps++ - request.Imp[index] = imp + validImps = append(validImps, imp) } - if validImps == 0 { + if len(validImps) == 0 { return nil, errs } + request.Imp = validImps + reqJSON, err := json.Marshal(request) if err != nil { errs = append(errs, err) @@ -116,31 +117,32 @@ func addHeaderIfNonEmpty(headers http.Header, headerName string, headerValue str } } -func (s *SovrnAdapter) MakeBids(request *openrtb2.BidRequest, requestData *adapters.RequestData, responseData *adapters.ResponseData) (*adapters.BidderResponse, []error) { - if responseData.StatusCode == http.StatusNoContent { +func (s *SovrnAdapter) MakeBids(request *openrtb2.BidRequest, bidderRequest *adapters.RequestData, bidderResponse *adapters.ResponseData) (*adapters.BidderResponse, []error) { + if bidderResponse.StatusCode == http.StatusNoContent { return nil, nil } - if responseData.StatusCode == http.StatusBadRequest { + if bidderResponse.StatusCode == http.StatusBadRequest { return nil, []error{&errortypes.BadInput{ - Message: fmt.Sprintf("Unexpected status code: %d. Run with request.debug = 1 for more info", responseData.StatusCode), + Message: fmt.Sprintf("Unexpected status code: %d. Run with request.debug = 1 for more info", bidderResponse.StatusCode), }} } - if responseData.StatusCode != http.StatusOK { + if bidderResponse.StatusCode != http.StatusOK { return nil, []error{&errortypes.BadServerResponse{ - Message: fmt.Sprintf("Unexpected status code: %d. Run with request.debug = 1 for more info", responseData.StatusCode), + Message: fmt.Sprintf("Unexpected status code: %d. Run with request.debug = 1 for more info", bidderResponse.StatusCode), }} } var bidResponse openrtb2.BidResponse - if err := json.Unmarshal(responseData.Body, &bidResponse); err != nil { + if err := json.Unmarshal(bidderResponse.Body, &bidResponse); err != nil { return nil, []error{&errortypes.BadServerResponse{ Message: err.Error(), }} } response := adapters.NewBidderResponseWithBidsCapacity(5) + errs := make([]error, 0) for _, sb := range bidResponse.SeatBid { for _, bid := range sb.Bid { @@ -150,8 +152,11 @@ func (s *SovrnAdapter) MakeBids(request *openrtb2.BidRequest, requestData *adapt bidType := openrtb_ext.BidTypeBanner - impIdx := getImpIdx(bid.ImpID, request) - if impIdx != -1 && request.Imp[impIdx].Video != nil { + impIdx, impIdErr := getImpIdx(bid.ImpID, request) + if impIdErr != nil { + errs = append(errs, impIdErr) + continue + } else if request.Imp[impIdx].Video != nil { bidType = openrtb_ext.BidTypeVideo } @@ -163,10 +168,10 @@ func (s *SovrnAdapter) MakeBids(request *openrtb2.BidRequest, requestData *adapt } } - return response, nil + return response, errs } -func getTagid(sovrnExt openrtb_ext.ExtImpSovrn) string { +func getTagId(sovrnExt openrtb_ext.ExtImpSovrn) string { if len(sovrnExt.Tagid) > 0 { return sovrnExt.Tagid } else { @@ -174,14 +179,16 @@ func getTagid(sovrnExt openrtb_ext.ExtImpSovrn) string { } } -func getImpIdx(impId string, request *openrtb2.BidRequest) int { +func getImpIdx(impId string, request *openrtb2.BidRequest) (int, error) { for idx, imp := range request.Imp { if imp.ID == impId { - return idx + return idx, nil } } - return -1 // hopefully this never happens + return -1, &errortypes.BadInput{ + Message: fmt.Sprintf("Could not find imp ID '%s' in bid, dropping bid", impId), + } } // Builder builds a new instance of the Sovrn adapter for the given bidder with the given config.