github.com/prebid/prebid-server/v2@v2.18.0/adapters/dianomi/dianomi.go (about) 1 package dianomi 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/errortypes" 12 "github.com/prebid/prebid-server/v2/openrtb_ext" 13 ) 14 15 type adapter struct { 16 endpoint string 17 } 18 19 type dianomiRequestExt struct { 20 openrtb_ext.ExtRequest 21 PriceType string `json:"pt"` 22 } 23 24 // Builder builds a new instance of the Dianomi adapter for the given bidder with the given config. 25 func Builder(bidderName openrtb_ext.BidderName, config config.Adapter, server config.Server) (adapters.Bidder, error) { 26 bidder := &adapter{ 27 endpoint: config.Endpoint, 28 } 29 return bidder, nil 30 } 31 32 func (a *adapter) MakeRequests(request *openrtb2.BidRequest, requestInfo *adapters.ExtraRequestInfo) ([]*adapters.RequestData, []error) { 33 var errors []error 34 var validImps = make([]openrtb2.Imp, 0, len(request.Imp)) 35 priceType := "" 36 37 for _, imp := range request.Imp { 38 var bidderExt adapters.ExtImpBidder 39 if err := json.Unmarshal(imp.Ext, &bidderExt); err != nil { 40 errors = append(errors, &errortypes.BadInput{ 41 Message: err.Error(), 42 }) 43 continue 44 } 45 46 var dianomiImpExt openrtb_ext.ExtImpDianomi 47 if err := json.Unmarshal(bidderExt.Bidder, &dianomiImpExt); err != nil { 48 errors = append(errors, &errortypes.BadInput{ 49 Message: err.Error(), 50 }) 51 continue 52 } 53 54 imp.TagID = dianomiImpExt.SmartadId.String() 55 validImps = append(validImps, imp) 56 57 // If imps specify priceType they should all be the same. If they differ, only the first one will be used 58 if dianomiImpExt.PriceType != "" && priceType == "" { 59 priceType = dianomiImpExt.PriceType 60 } 61 } 62 63 if priceType != "" { 64 requestExt := dianomiRequestExt{} 65 var err error 66 67 if len(request.Ext) > 0 { 68 if err = json.Unmarshal(request.Ext, &requestExt); err != nil { 69 errors = append(errors, err) 70 } 71 } 72 73 if err == nil { 74 requestExt.PriceType = priceType 75 76 if request.Ext, err = json.Marshal(&requestExt); err != nil { 77 errors = append(errors, err) 78 } 79 } 80 } 81 82 request.Imp = validImps 83 84 requestJSON, err := json.Marshal(request) 85 if err != nil { 86 errors = append(errors, err) 87 return nil, errors 88 } 89 90 headers := http.Header{} 91 headers.Add("Content-Type", "application/json;charset=utf-8") 92 headers.Add("Accept", "application/json") 93 requestData := &adapters.RequestData{ 94 Method: "POST", 95 Uri: a.endpoint, 96 Body: requestJSON, 97 Headers: headers, 98 ImpIDs: openrtb_ext.GetImpIDs(request.Imp), 99 } 100 101 return []*adapters.RequestData{requestData}, errors 102 } 103 104 func (a *adapter) MakeBids(request *openrtb2.BidRequest, requestData *adapters.RequestData, responseData *adapters.ResponseData) (*adapters.BidderResponse, []error) { 105 if responseData.StatusCode == http.StatusNoContent { 106 return nil, nil 107 } 108 109 if responseData.StatusCode == http.StatusBadRequest { 110 err := &errortypes.BadInput{ 111 Message: "Unexpected status code: 400. Bad request from publisher.", 112 } 113 return nil, []error{err} 114 } 115 116 if responseData.StatusCode != http.StatusOK { 117 err := &errortypes.BadServerResponse{ 118 Message: fmt.Sprintf("Unexpected status code: %d.", responseData.StatusCode), 119 } 120 return nil, []error{err} 121 } 122 123 var response openrtb2.BidResponse 124 if err := json.Unmarshal(responseData.Body, &response); err != nil { 125 return nil, []error{err} 126 } 127 128 bidResponse := adapters.NewBidderResponseWithBidsCapacity(len(request.Imp)) 129 bidResponse.Currency = response.Cur 130 var errors []error 131 for _, seatBid := range response.SeatBid { 132 for i, bid := range seatBid.Bid { 133 bidType, err := getMediaTypeForBid(bid) 134 if err != nil { 135 errors = append(errors, err) 136 continue 137 } 138 bidResponse.Bids = append(bidResponse.Bids, &adapters.TypedBid{ 139 Bid: &seatBid.Bid[i], 140 BidType: bidType, 141 }) 142 } 143 } 144 145 return bidResponse, errors 146 } 147 148 func getMediaTypeForBid(bid openrtb2.Bid) (openrtb_ext.BidType, error) { 149 if bid.Ext != nil { 150 var bidExt openrtb_ext.ExtBid 151 err := json.Unmarshal(bid.Ext, &bidExt) 152 if err == nil && bidExt.Prebid != nil { 153 return openrtb_ext.ParseBidType(string(bidExt.Prebid.Type)) 154 } 155 } 156 157 return "", &errortypes.BadServerResponse{ 158 Message: fmt.Sprintf("Failed to parse impression \"%s\" mediatype", bid.ImpID), 159 } 160 }