Skip to content

Commit

Permalink
More PR fixes- test still pass
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Pabst committed May 9, 2022
1 parent 27fde45 commit ad110c0
Showing 1 changed file with 29 additions and 22 deletions.
51 changes: 29 additions & 22 deletions adapters/sovrn/sovrn.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -60,15 +60,15 @@ 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'",
})
continue
}

imp.TagID = getTagid(sovrnExt)
imp.TagID = tagId

if imp.BidFloor == 0 && sovrnExt.BidFloor > 0 {
imp.BidFloor = sovrnExt.BidFloor
Expand All @@ -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)
Expand All @@ -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 {
Expand All @@ -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
}

Expand All @@ -163,25 +168,27 @@ 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 {
return sovrnExt.TagId
}
}

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.
Expand Down

0 comments on commit ad110c0

Please sign in to comment.