github.com/prebid/prebid-server/v2@v2.18.0/adapters/kargo/kargo.go (about)

     1  package kargo
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"net/http"
     7  
     8  	"github.com/prebid/openrtb/v20/openrtb2"
     9  
    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  	URI string
    18  }
    19  type kargoExt struct {
    20  	MediaType string `json:"mediaType"`
    21  }
    22  
    23  // Builder builds a new instance of the Kargo adapter for the given bidder with the given config.
    24  func Builder(bidderName openrtb_ext.BidderName, config config.Adapter, server config.Server) (adapters.Bidder, error) {
    25  	bidder := &adapter{
    26  		URI: config.Endpoint, // base url of bidding server
    27  	}
    28  	return bidder, nil
    29  }
    30  
    31  // MakeRequests creates outgoing requests to the Kargo bidding server.
    32  func (a *adapter) MakeRequests(request *openrtb2.BidRequest, requestInfo *adapters.ExtraRequestInfo) ([]*adapters.RequestData, []error) {
    33  	requestJSON, err := json.Marshal(request)
    34  	if err != nil {
    35  		return nil, []error{err}
    36  	}
    37  
    38  	requestData := &adapters.RequestData{
    39  		Method: "POST",
    40  		Uri:    a.URI,
    41  		Body:   requestJSON,
    42  		ImpIDs: openrtb_ext.GetImpIDs(request.Imp),
    43  	}
    44  
    45  	return []*adapters.RequestData{requestData}, nil
    46  }
    47  
    48  // MakeBids receives a bid response from the Kargo bidding server and creates bids for the publishers auction.
    49  func (a *adapter) MakeBids(request *openrtb2.BidRequest, requestData *adapters.RequestData, responseData *adapters.ResponseData) (*adapters.BidderResponse, []error) {
    50  	if responseData.StatusCode == http.StatusNoContent {
    51  		return nil, nil
    52  	}
    53  
    54  	if responseData.StatusCode != http.StatusOK {
    55  		err := &errortypes.BadServerResponse{
    56  			Message: fmt.Sprintf("Unexpected status code: %d. Run with request.debug = 1 for more info.", responseData.StatusCode),
    57  		}
    58  		return nil, []error{err}
    59  	}
    60  
    61  	var response openrtb2.BidResponse
    62  	if err := json.Unmarshal(responseData.Body, &response); err != nil {
    63  		return nil, []error{err}
    64  	}
    65  
    66  	bidResponse := adapters.NewBidderResponseWithBidsCapacity(len(request.Imp))
    67  	bidResponse.Currency = response.Cur
    68  	for _, seatBid := range response.SeatBid {
    69  		for i, bid := range seatBid.Bid {
    70  			b := &adapters.TypedBid{
    71  				Bid:     &seatBid.Bid[i],
    72  				BidType: getMediaTypeForBid(bid.Ext),
    73  			}
    74  			bidResponse.Bids = append(bidResponse.Bids, b)
    75  		}
    76  	}
    77  	return bidResponse, nil
    78  }
    79  
    80  func getMediaTypeForBid(ext json.RawMessage) openrtb_ext.BidType {
    81  	var impExt kargoExt
    82  	if err := json.Unmarshal(ext, &impExt); err == nil {
    83  		switch impExt.MediaType {
    84  		case string(openrtb_ext.BidTypeVideo):
    85  			return openrtb_ext.BidTypeVideo
    86  		case string(openrtb_ext.BidTypeNative):
    87  			return openrtb_ext.BidTypeNative
    88  		}
    89  	}
    90  	return openrtb_ext.BidTypeBanner
    91  }