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

     1  package nobid
     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  // NoBidAdapter - NoBid Adapter definition
    16  type NoBidAdapter struct {
    17  	endpoint string
    18  }
    19  
    20  // Builder builds a new instance of the NoBid adapter for the given bidder with the given config.
    21  func Builder(bidderName openrtb_ext.BidderName, config config.Adapter, server config.Server) (adapters.Bidder, error) {
    22  	bidder := &NoBidAdapter{
    23  		endpoint: config.Endpoint,
    24  	}
    25  	return bidder, nil
    26  }
    27  
    28  // MakeRequests Makes the OpenRTB request payload
    29  func (a *NoBidAdapter) MakeRequests(request *openrtb2.BidRequest, reqInfo *adapters.ExtraRequestInfo) ([]*adapters.RequestData, []error) {
    30  
    31  	if len(request.Imp) == 0 {
    32  		return nil, []error{&errortypes.BadInput{
    33  			Message: fmt.Sprintf("No Imps in Bid Request"),
    34  		}}
    35  	}
    36  
    37  	data, err := json.Marshal(request)
    38  	if err != nil {
    39  		return nil, []error{err}
    40  	}
    41  
    42  	headers := http.Header{}
    43  	headers.Add("Content-Type", "application/json;charset=utf-8")
    44  	headers.Add("Accept", "application/json")
    45  
    46  	return []*adapters.RequestData{{
    47  		Method:  "POST",
    48  		Uri:     a.endpoint,
    49  		Body:    data,
    50  		Headers: headers,
    51  		ImpIDs:  openrtb_ext.GetImpIDs(request.Imp),
    52  	}}, []error{}
    53  }
    54  
    55  // MakeBids makes the bids
    56  func (a *NoBidAdapter) MakeBids(internalRequest *openrtb2.BidRequest, externalRequest *adapters.RequestData, response *adapters.ResponseData) (*adapters.BidderResponse, []error) {
    57  
    58  	if response.StatusCode == http.StatusNoContent {
    59  		return nil, nil
    60  	}
    61  
    62  	if response.StatusCode == http.StatusBadRequest {
    63  		return nil, []error{&errortypes.BadInput{
    64  			Message: fmt.Sprintf("Unexpected status code: %d. Run with request.debug = 1 for more info", response.StatusCode),
    65  		}}
    66  	}
    67  
    68  	if response.StatusCode != http.StatusOK {
    69  		return nil, []error{&errortypes.BadServerResponse{
    70  			Message: fmt.Sprintf("Unexpected status code: %d. Run with request.debug = 1 for more info", response.StatusCode),
    71  		}}
    72  	}
    73  
    74  	var bidResp openrtb2.BidResponse
    75  
    76  	if err := json.Unmarshal(response.Body, &bidResp); err != nil {
    77  		return nil, []error{err}
    78  	}
    79  
    80  	count := getBidCount(bidResp)
    81  	bidResponse := adapters.NewBidderResponseWithBidsCapacity(count)
    82  
    83  	var errs []error
    84  
    85  	for _, sb := range bidResp.SeatBid {
    86  		for i := range sb.Bid {
    87  			bidType, err := getMediaTypeForImp(sb.Bid[i].ImpID, internalRequest.Imp)
    88  			if err != nil {
    89  				errs = append(errs, err)
    90  			} else {
    91  				b := &adapters.TypedBid{
    92  					Bid:     &sb.Bid[i],
    93  					BidType: bidType,
    94  				}
    95  				bidResponse.Bids = append(bidResponse.Bids, b)
    96  			}
    97  		}
    98  	}
    99  	return bidResponse, errs
   100  }
   101  
   102  func getBidCount(bidResponse openrtb2.BidResponse) int {
   103  	c := 0
   104  	for _, sb := range bidResponse.SeatBid {
   105  		c = c + len(sb.Bid)
   106  	}
   107  	return c
   108  }
   109  
   110  func getMediaTypeForImp(impID string, imps []openrtb2.Imp) (openrtb_ext.BidType, error) {
   111  	mediaType := openrtb_ext.BidTypeBanner
   112  	for _, imp := range imps {
   113  		if imp.ID == impID {
   114  			if imp.Banner == nil && imp.Video != nil {
   115  				mediaType = openrtb_ext.BidTypeVideo
   116  			}
   117  			return mediaType, nil
   118  		}
   119  	}
   120  
   121  	// This shouldnt happen. Lets handle it just incase by returning an error.
   122  	return "", &errortypes.BadInput{
   123  		Message: fmt.Sprintf("Failed to find impression \"%s\"", impID),
   124  	}
   125  }