github.com/prebid/prebid-server/v2@v2.18.0/adapters/mgidX/mgidX.go (about) 1 package mgidX 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 MgidXBidderExt 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 err 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 mgidXExt openrtb_ext.ImpExtMgidX 46 47 if err = json.Unmarshal(reqCopy.Imp[0].Ext, &bidderExt); err != nil { 48 return nil, []error{err} 49 } 50 if err = json.Unmarshal(bidderExt.Bidder, &mgidXExt); err != nil { 51 return nil, []error{err} 52 } 53 54 impExt := reqBodyExt{MgidXBidderExt: reqBodyExtBidder{}} 55 56 if mgidXExt.PlacementID != "" { 57 impExt.MgidXBidderExt.PlacementID = mgidXExt.PlacementID 58 impExt.MgidXBidderExt.Type = "publisher" 59 } else if mgidXExt.EndpointID != "" { 60 impExt.MgidXBidderExt.EndpointID = mgidXExt.EndpointID 61 impExt.MgidXBidderExt.Type = "network" 62 } else { 63 continue 64 } 65 66 finalyImpExt, err := json.Marshal(impExt) 67 if err != nil { 68 return nil, []error{err} 69 } 70 71 reqCopy.Imp[0].Ext = finalyImpExt 72 73 adapterReq, err := a.makeRequest(&reqCopy) 74 if err != nil { 75 return nil, []error{err} 76 } 77 78 if adapterReq != nil { 79 adapterRequests = append(adapterRequests, adapterReq) 80 } 81 } 82 83 if len(adapterRequests) == 0 { 84 return nil, []error{errors.New("found no valid impressions")} 85 } 86 87 return adapterRequests, nil 88 } 89 90 func (a *adapter) makeRequest(request *openrtb2.BidRequest) (*adapters.RequestData, error) { 91 reqJSON, err := json.Marshal(request) 92 if err != nil { 93 return nil, err 94 } 95 96 headers := http.Header{} 97 headers.Add("Content-Type", "application/json;charset=utf-8") 98 headers.Add("Accept", "application/json") 99 return &adapters.RequestData{ 100 Method: "POST", 101 Uri: a.endpoint, 102 Body: reqJSON, 103 Headers: headers, 104 ImpIDs: openrtb_ext.GetImpIDs(request.Imp), 105 }, nil 106 } 107 108 func (a *adapter) MakeBids(request *openrtb2.BidRequest, requestData *adapters.RequestData, responseData *adapters.ResponseData) (*adapters.BidderResponse, []error) { 109 if adapters.IsResponseStatusCodeNoContent(responseData) { 110 return nil, nil 111 } 112 113 if err := adapters.CheckResponseStatusCodeForErrors(responseData); err != nil { 114 return nil, []error{err} 115 } 116 117 var response openrtb2.BidResponse 118 if err := json.Unmarshal(responseData.Body, &response); err != nil { 119 return nil, []error{err} 120 } 121 122 bidResponse := adapters.NewBidderResponseWithBidsCapacity(len(request.Imp)) 123 bidResponse.Currency = response.Cur 124 125 impsMappedByID := make(map[string]openrtb2.Imp, len(request.Imp)) 126 for i, imp := range request.Imp { 127 impsMappedByID[request.Imp[i].ID] = imp 128 } 129 130 for _, seatBid := range response.SeatBid { 131 for i := range seatBid.Bid { 132 bidType, err := getBidMediaType(&seatBid.Bid[i]) 133 if err != nil { 134 return nil, []error{err} 135 } 136 137 b := &adapters.TypedBid{ 138 Bid: &seatBid.Bid[i], 139 BidType: bidType, 140 } 141 bidResponse.Bids = append(bidResponse.Bids, b) 142 } 143 } 144 return bidResponse, nil 145 } 146 147 func getBidMediaType(bid *openrtb2.Bid) (openrtb_ext.BidType, error) { 148 var extBid openrtb_ext.ExtBid 149 err := json.Unmarshal(bid.Ext, &extBid) 150 if err != nil { 151 return "", fmt.Errorf("unable to deserialize imp %v bid.ext", bid.ImpID) 152 } 153 154 if extBid.Prebid == nil { 155 return "", fmt.Errorf("imp %v with unknown media type", bid.ImpID) 156 } 157 158 return extBid.Prebid.Type, nil 159 }