github.com/prebid/prebid-server/v2@v2.18.0/adapters/pwbid/pwbid.go (about) 1 package pwbid 2 3 import ( 4 "encoding/json" 5 "fmt" 6 7 "github.com/prebid/openrtb/v20/openrtb2" 8 "github.com/prebid/prebid-server/v2/adapters" 9 "github.com/prebid/prebid-server/v2/config" 10 "github.com/prebid/prebid-server/v2/errortypes" 11 "github.com/prebid/prebid-server/v2/openrtb_ext" 12 ) 13 14 type adapter struct { 15 endpoint string 16 } 17 18 func Builder(bidderName openrtb_ext.BidderName, config config.Adapter, server config.Server) (adapters.Bidder, error) { 19 bidder := &adapter{ 20 endpoint: config.Endpoint, 21 } 22 23 return bidder, nil 24 } 25 26 func (a *adapter) MakeRequests(request *openrtb2.BidRequest, requestInfo *adapters.ExtraRequestInfo) ([]*adapters.RequestData, []error) { 27 requestJSON, err := json.Marshal(request) 28 if err != nil { 29 return nil, []error{err} 30 } 31 32 requestData := &adapters.RequestData{ 33 Method: "POST", 34 Uri: a.endpoint, 35 Body: requestJSON, 36 ImpIDs: openrtb_ext.GetImpIDs(request.Imp), 37 } 38 39 return []*adapters.RequestData{requestData}, nil 40 } 41 42 func (a *adapter) MakeBids(request *openrtb2.BidRequest, requestData *adapters.RequestData, responseData *adapters.ResponseData) (*adapters.BidderResponse, []error) { 43 var errors []error 44 45 if adapters.IsResponseStatusCodeNoContent(responseData) { 46 return nil, nil 47 } 48 49 if err := adapters.CheckResponseStatusCodeForErrors(responseData); err != nil { 50 return nil, []error{err} 51 } 52 53 var response openrtb2.BidResponse 54 if err := json.Unmarshal(responseData.Body, &response); err != nil { 55 return nil, []error{err} 56 } 57 58 bidResponse := adapters.NewBidderResponseWithBidsCapacity(len(request.Imp)) 59 60 for _, seatBid := range response.SeatBid { 61 for i, bid := range seatBid.Bid { 62 bidType, typeErr := getMediaTypeForBid(request.Imp, bid) 63 if typeErr != nil { 64 errors = append(errors, typeErr) 65 continue 66 } 67 68 b := &adapters.TypedBid{ 69 Bid: &seatBid.Bid[i], 70 BidType: bidType, 71 } 72 bidResponse.Bids = append(bidResponse.Bids, b) 73 } 74 } 75 76 return bidResponse, errors 77 } 78 79 func getMediaTypeForBid(impressions []openrtb2.Imp, bid openrtb2.Bid) (openrtb_ext.BidType, error) { 80 for _, impression := range impressions { 81 if impression.ID == bid.ImpID { 82 if impression.Banner != nil { 83 return openrtb_ext.BidTypeBanner, nil 84 } 85 if impression.Native != nil { 86 return openrtb_ext.BidTypeNative, nil 87 } 88 if impression.Video != nil { 89 return openrtb_ext.BidTypeVideo, nil 90 } 91 } 92 } 93 94 return "", &errortypes.BadServerResponse{ 95 Message: fmt.Sprintf("The impression with ID %s is not present into the request", bid.ImpID), 96 } 97 }