github.com/prebid/prebid-server/v2@v2.18.0/adapters/globalsun/globalsun.go (about) 1 package globalsun 2 3 import ( 4 "encoding/json" 5 "fmt" 6 "net/http" 7 8 "github.com/buger/jsonparser" 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/errortypes" 13 "github.com/prebid/prebid-server/v2/openrtb_ext" 14 ) 15 16 type adapter struct { 17 endpoint string 18 } 19 20 type reqBodyExt struct { 21 GlobalsunBidderExt reqBodyExtBidder `json:"bidder"` 22 } 23 24 type reqBodyExtBidder struct { 25 Type string `json:"type"` 26 PlacementID string `json:"placementId,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 adapterRequests := make([]*adapters.RequestData, 0, len(request.Imp)) 38 39 reqCopy := *request 40 for _, imp := range request.Imp { 41 reqCopy.Imp = []openrtb2.Imp{imp} 42 43 placementID, err := jsonparser.GetString(imp.Ext, "bidder", "placementId") 44 if err != nil { 45 return nil, []error{err} 46 } 47 48 extJson, err := json.Marshal(reqBodyExt{ 49 GlobalsunBidderExt: reqBodyExtBidder{ 50 PlacementID: placementID, 51 Type: "publisher", 52 }, 53 }) 54 if err != nil { 55 return nil, []error{err} 56 } 57 58 reqCopy.Imp[0].Ext = extJson 59 60 adapterReq, err := a.makeRequest(&reqCopy) 61 if err != nil { 62 return nil, []error{err} 63 } 64 65 if adapterReq != nil { 66 adapterRequests = append(adapterRequests, adapterReq) 67 } 68 } 69 return adapterRequests, nil 70 } 71 72 func (a *adapter) makeRequest(request *openrtb2.BidRequest) (*adapters.RequestData, error) { 73 reqJSON, err := json.Marshal(request) 74 if err != nil { 75 return nil, err 76 } 77 78 headers := http.Header{} 79 headers.Add("Content-Type", "application/json;charset=utf-8") 80 headers.Add("Accept", "application/json") 81 return &adapters.RequestData{ 82 Method: "POST", 83 Uri: a.endpoint, 84 Body: reqJSON, 85 Headers: headers, 86 ImpIDs: openrtb_ext.GetImpIDs(request.Imp), 87 }, err 88 } 89 90 func (a *adapter) MakeBids(request *openrtb2.BidRequest, requestData *adapters.RequestData, responseData *adapters.ResponseData) (*adapters.BidderResponse, []error) { 91 if responseData.StatusCode == http.StatusNoContent { 92 return nil, nil 93 } 94 95 if responseData.StatusCode != http.StatusOK { 96 err := &errortypes.BadServerResponse{ 97 Message: fmt.Sprintf("Unexpected status code: %d. Run with request.debug = 1 for more info.", responseData.StatusCode), 98 } 99 return nil, []error{err} 100 } 101 102 var response openrtb2.BidResponse 103 if err := json.Unmarshal(responseData.Body, &response); err != nil { 104 return nil, []error{err} 105 } 106 107 bidResponse := adapters.NewBidderResponseWithBidsCapacity(len(request.Imp)) 108 bidResponse.Currency = response.Cur 109 for _, seatBid := range response.SeatBid { 110 for i := range seatBid.Bid { 111 bidType, err := getMediaTypeForImp(seatBid.Bid[i].ImpID, request.Imp) 112 if err != nil { 113 return nil, []error{err} 114 } 115 116 b := &adapters.TypedBid{ 117 Bid: &seatBid.Bid[i], 118 BidType: bidType, 119 } 120 bidResponse.Bids = append(bidResponse.Bids, b) 121 } 122 } 123 return bidResponse, nil 124 } 125 126 func getMediaTypeForImp(impID string, imps []openrtb2.Imp) (openrtb_ext.BidType, error) { 127 for _, imp := range imps { 128 if imp.ID == impID { 129 if imp.Banner != nil { 130 return openrtb_ext.BidTypeBanner, nil 131 } 132 if imp.Video != nil { 133 return openrtb_ext.BidTypeVideo, nil 134 } 135 if imp.Native != nil { 136 return openrtb_ext.BidTypeNative, nil 137 } 138 } 139 } 140 141 return "", &errortypes.BadInput{ 142 Message: fmt.Sprintf("Failed to find impression \"%s\"", impID), 143 } 144 }