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