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

     1  package revcontent
     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  // Builder builds a new instance of the Revcontent adapter for the given bidder with the given config.
    20  func Builder(bidderName openrtb_ext.BidderName, config config.Adapter, server config.Server) (adapters.Bidder, error) {
    21  	bidder := &adapter{
    22  		endpoint: config.Endpoint,
    23  	}
    24  	return bidder, nil
    25  }
    26  
    27  func (a *adapter) MakeRequests(request *openrtb2.BidRequest, reqInfo *adapters.ExtraRequestInfo) ([]*adapters.RequestData, []error) {
    28  	reqBody, err := json.Marshal(request)
    29  
    30  	if err != nil {
    31  		return nil, []error{err}
    32  	}
    33  
    34  	if err := checkRequest(request); err != nil {
    35  		return nil, []error{err}
    36  	}
    37  
    38  	headers := http.Header{}
    39  	headers.Add("Content-Type", "application/json;charset=utf-8")
    40  
    41  	req := &adapters.RequestData{
    42  		Method:  "POST",
    43  		Uri:     a.endpoint,
    44  		Body:    reqBody,
    45  		Headers: headers,
    46  		ImpIDs:  openrtb_ext.GetImpIDs(request.Imp),
    47  	}
    48  	return []*adapters.RequestData{req}, nil
    49  }
    50  
    51  func checkRequest(request *openrtb2.BidRequest) error {
    52  	if (request.App == nil || len(request.App.Name) == 0) && (request.Site == nil || len(request.Site.Domain) == 0) {
    53  		return &errortypes.BadInput{
    54  			Message: "Impression is missing app name or site domain, and must contain one.",
    55  		}
    56  	}
    57  
    58  	return nil
    59  }
    60  
    61  // MakeBids make the bids for the bid response.
    62  func (a *adapter) MakeBids(internalRequest *openrtb2.BidRequest, externalRequest *adapters.RequestData, response *adapters.ResponseData) (*adapters.BidderResponse, []error) {
    63  	if response.StatusCode == http.StatusNoContent {
    64  		return nil, nil
    65  	}
    66  
    67  	if response.StatusCode == http.StatusBadRequest {
    68  		return nil, []error{&errortypes.BadInput{
    69  			Message: fmt.Sprintf("unexpected status code: %d.", response.StatusCode),
    70  		}}
    71  	}
    72  
    73  	if response.StatusCode != http.StatusOK {
    74  		return nil, []error{&errortypes.BadServerResponse{
    75  			Message: fmt.Sprintf("unexpected status code: %d.", response.StatusCode),
    76  		}}
    77  	}
    78  
    79  	var bidResp openrtb2.BidResponse
    80  
    81  	if err := json.Unmarshal(response.Body, &bidResp); err != nil {
    82  		return nil, []error{err}
    83  	}
    84  
    85  	bidResponse := adapters.NewBidderResponseWithBidsCapacity(5)
    86  
    87  	for _, sb := range bidResp.SeatBid {
    88  		for i := range sb.Bid {
    89  			var mediaType = getBidType(sb.Bid[i].AdM)
    90  			bidResponse.Bids = append(bidResponse.Bids, &adapters.TypedBid{
    91  				Bid:     &sb.Bid[i],
    92  				BidType: mediaType,
    93  			})
    94  		}
    95  	}
    96  	return bidResponse, nil
    97  
    98  }
    99  
   100  func getBidType(bidAdm string) openrtb_ext.BidType {
   101  	// native: {"ver":"1.1","assets":...
   102  	// banner: <div id='rtb-widget...
   103  	if bidAdm != "" && bidAdm[:1] == "<" {
   104  		return openrtb_ext.BidTypeBanner
   105  	}
   106  	return openrtb_ext.BidTypeNative
   107  }