github.com/prebid/prebid-server/v2@v2.18.0/adapters/playdigo/playdigo.go (about) 1 package playdigo 2 3 import ( 4 "encoding/json" 5 "errors" 6 "fmt" 7 "net/http" 8 9 "github.com/prebid/openrtb/v20/openrtb2" 10 "github.com/prebid/prebid-server/v2/adapters" 11 "github.com/prebid/prebid-server/v2/config" 12 "github.com/prebid/prebid-server/v2/openrtb_ext" 13 ) 14 15 type adapter struct { 16 endpoint string 17 } 18 19 type reqBodyExt struct { 20 PlaydigoBidderExt reqBodyExtBidder `json:"bidder"` 21 } 22 23 type reqBodyExtBidder struct { 24 Type string `json:"type"` 25 PlacementID string `json:"placementId,omitempty"` 26 EndpointID string `json:"endpointId,omitempty"` 27 } 28 29 func Builder(bidderName openrtb_ext.BidderName, config config.Adapter, server config.Server) (adapters.Bidder, error) { 30 bidder := &adapter{ 31 endpoint: config.Endpoint, 32 } 33 return bidder, nil 34 } 35 36 func (a *adapter) MakeRequests(request *openrtb2.BidRequest, reqInfo *adapters.ExtraRequestInfo) ([]*adapters.RequestData, []error) { 37 var errs []error 38 var adapterRequests []*adapters.RequestData 39 40 reqCopy := *request 41 for _, imp := range request.Imp { 42 reqCopy.Imp = []openrtb2.Imp{imp} 43 44 var bidderExt adapters.ExtImpBidder 45 var playdigoExt openrtb_ext.ImpExtPlaydigo 46 47 if err := json.Unmarshal(imp.Ext, &bidderExt); err != nil { 48 errs = append(errs, err) 49 continue 50 } 51 if err := json.Unmarshal(bidderExt.Bidder, &playdigoExt); err != nil { 52 errs = append(errs, err) 53 continue 54 } 55 56 impExt := reqBodyExt{PlaydigoBidderExt: reqBodyExtBidder{}} 57 58 if playdigoExt.PlacementID != "" { 59 impExt.PlaydigoBidderExt.PlacementID = playdigoExt.PlacementID 60 impExt.PlaydigoBidderExt.Type = "publisher" 61 } else if playdigoExt.EndpointID != "" { 62 impExt.PlaydigoBidderExt.EndpointID = playdigoExt.EndpointID 63 impExt.PlaydigoBidderExt.Type = "network" 64 } 65 66 finalyImpExt, err := json.Marshal(impExt) 67 if err != nil { 68 errs = append(errs, err) 69 continue 70 } 71 72 reqCopy.Imp[0].Ext = finalyImpExt 73 74 adapterReq, err := a.makeRequest(&reqCopy) 75 if err != nil { 76 errs = append(errs, err) 77 continue 78 } 79 80 adapterRequests = append(adapterRequests, adapterReq) 81 } 82 83 if len(adapterRequests) == 0 { 84 errs = append(errs, errors.New("found no valid impressions")) 85 return nil, errs 86 } 87 88 return adapterRequests, nil 89 } 90 91 func (a *adapter) makeRequest(request *openrtb2.BidRequest) (*adapters.RequestData, error) { 92 reqJSON, err := json.Marshal(request) 93 if err != nil { 94 return nil, err 95 } 96 97 headers := http.Header{} 98 headers.Add("Content-Type", "application/json;charset=utf-8") 99 headers.Add("Accept", "application/json") 100 return &adapters.RequestData{ 101 Method: "POST", 102 Uri: a.endpoint, 103 Body: reqJSON, 104 Headers: headers, 105 ImpIDs: openrtb_ext.GetImpIDs(request.Imp), 106 }, nil 107 } 108 109 func (a *adapter) MakeBids(request *openrtb2.BidRequest, requestData *adapters.RequestData, responseData *adapters.ResponseData) (*adapters.BidderResponse, []error) { 110 if adapters.IsResponseStatusCodeNoContent(responseData) { 111 return nil, nil 112 } 113 114 if err := adapters.CheckResponseStatusCodeForErrors(responseData); err != nil { 115 return nil, []error{err} 116 } 117 118 var response openrtb2.BidResponse 119 if err := json.Unmarshal(responseData.Body, &response); err != nil { 120 return nil, []error{err} 121 } 122 123 bidResponse := adapters.NewBidderResponseWithBidsCapacity(len(request.Imp)) 124 if len(response.Cur) != 0 { 125 bidResponse.Currency = response.Cur 126 } 127 128 for _, seatBid := range response.SeatBid { 129 for i := range seatBid.Bid { 130 bid := seatBid.Bid[i] 131 bidType, err := getBidType(bid) 132 if err != nil { 133 return nil, []error{err} 134 } 135 136 b := &adapters.TypedBid{ 137 Bid: &seatBid.Bid[i], 138 BidType: bidType, 139 } 140 bidResponse.Bids = append(bidResponse.Bids, b) 141 } 142 } 143 return bidResponse, nil 144 } 145 146 func getBidType(bid openrtb2.Bid) (openrtb_ext.BidType, error) { 147 // determinate media type by bid response field mtype 148 switch bid.MType { 149 case openrtb2.MarkupBanner: 150 return openrtb_ext.BidTypeBanner, nil 151 case openrtb2.MarkupVideo: 152 return openrtb_ext.BidTypeVideo, nil 153 case openrtb2.MarkupAudio: 154 return openrtb_ext.BidTypeAudio, nil 155 case openrtb2.MarkupNative: 156 return openrtb_ext.BidTypeNative, nil 157 } 158 159 return "", fmt.Errorf("could not define media type for impression: %s", bid.ImpID) 160 }