github.com/prebid/prebid-server/v2@v2.18.0/adapters/iqzone/iqzone.go (about) 1 package iqzone 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 func Builder(bidderName openrtb_ext.BidderName, config config.Adapter, server config.Server) (adapters.Bidder, error) { 20 bidder := &adapter{ 21 endpoint: config.Endpoint, 22 } 23 return bidder, nil 24 } 25 26 func (a *adapter) MakeRequests(request *openrtb2.BidRequest, reqInfo *adapters.ExtraRequestInfo) ([]*adapters.RequestData, []error) { 27 var errs []error 28 var err error 29 var adapterRequests []*adapters.RequestData 30 31 reqCopy := *request 32 for _, imp := range request.Imp { 33 reqCopy.Imp = []openrtb2.Imp{imp} 34 35 var bidderExt adapters.ExtImpBidder 36 var iqzoneExt openrtb_ext.ImpExtIQZone 37 38 if err = json.Unmarshal(reqCopy.Imp[0].Ext, &bidderExt); err != nil { 39 return nil, append(errs, err) 40 } 41 if err = json.Unmarshal(bidderExt.Bidder, &iqzoneExt); err != nil { 42 return nil, append(errs, err) 43 } 44 45 finalyImpExt := reqCopy.Imp[0].Ext 46 if iqzoneExt.PlacementID != "" { 47 finalyImpExt, _ = json.Marshal(map[string]interface{}{ 48 "bidder": map[string]interface{}{ 49 "placementId": iqzoneExt.PlacementID, 50 "type": "publisher", 51 }, 52 }) 53 } else if iqzoneExt.EndpointID != "" { 54 finalyImpExt, _ = json.Marshal(map[string]interface{}{ 55 "bidder": map[string]interface{}{ 56 "endpointId": iqzoneExt.EndpointID, 57 "type": "network", 58 }, 59 }) 60 } 61 62 reqCopy.Imp[0].Ext = finalyImpExt 63 64 adapterReq, errors := a.makeRequest(&reqCopy) 65 if adapterReq != nil { 66 adapterRequests = append(adapterRequests, adapterReq) 67 } 68 errs = append(errs, errors...) 69 } 70 return adapterRequests, errs 71 } 72 73 func (a *adapter) makeRequest(request *openrtb2.BidRequest) (*adapters.RequestData, []error) { 74 var errs []error 75 76 reqJSON, err := json.Marshal(request) 77 if err != nil { 78 errs = append(errs, err) 79 return nil, errs 80 } 81 82 headers := http.Header{} 83 headers.Add("Content-Type", "application/json;charset=utf-8") 84 headers.Add("Accept", "application/json") 85 return &adapters.RequestData{ 86 Method: "POST", 87 Uri: a.endpoint, 88 Body: reqJSON, 89 Headers: headers, 90 ImpIDs: openrtb_ext.GetImpIDs(request.Imp), 91 }, errs 92 } 93 94 func (a *adapter) MakeBids(request *openrtb2.BidRequest, requestData *adapters.RequestData, responseData *adapters.ResponseData) (*adapters.BidderResponse, []error) { 95 if responseData.StatusCode == http.StatusNoContent { 96 return nil, nil 97 } 98 99 if responseData.StatusCode != http.StatusOK { 100 err := &errortypes.BadServerResponse{ 101 Message: fmt.Sprintf("Unexpected status code: %d. Run with request.debug = 1 for more info.", responseData.StatusCode), 102 } 103 return nil, []error{err} 104 } 105 106 var response openrtb2.BidResponse 107 if err := json.Unmarshal(responseData.Body, &response); err != nil { 108 return nil, []error{err} 109 } 110 111 bidResponse := adapters.NewBidderResponseWithBidsCapacity(len(request.Imp)) 112 bidResponse.Currency = response.Cur 113 for _, seatBid := range response.SeatBid { 114 for i := range seatBid.Bid { 115 bidType, err := getBidMediaType(&seatBid.Bid[i]) 116 if err != nil { 117 return nil, []error{err} 118 } 119 120 b := &adapters.TypedBid{ 121 Bid: &seatBid.Bid[i], 122 BidType: bidType, 123 } 124 bidResponse.Bids = append(bidResponse.Bids, b) 125 } 126 } 127 return bidResponse, nil 128 } 129 130 func getBidMediaType(bid *openrtb2.Bid) (openrtb_ext.BidType, error) { 131 switch bid.MType { 132 case openrtb2.MarkupBanner: 133 return openrtb_ext.BidTypeBanner, nil 134 case openrtb2.MarkupVideo: 135 return openrtb_ext.BidTypeVideo, nil 136 case openrtb2.MarkupNative: 137 return openrtb_ext.BidTypeNative, nil 138 default: 139 return "", fmt.Errorf("Unable to fetch mediaType in multi-format: %s", bid.ImpID) 140 } 141 }