github.com/prebid/prebid-server@v0.275.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/v19/openrtb2"
     9  	"github.com/prebid/prebid-server/adapters"
    10  	"github.com/prebid/prebid-server/config"
    11  	"github.com/prebid/prebid-server/errortypes"
    12  	"github.com/prebid/prebid-server/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  	}}, []error{}
    52  }
    53  
    54  // MakeBids makes the bids
    55  func (a *NoBidAdapter) MakeBids(internalRequest *openrtb2.BidRequest, externalRequest *adapters.RequestData, response *adapters.ResponseData) (*adapters.BidderResponse, []error) {
    56  
    57  	if response.StatusCode == http.StatusNoContent {
    58  		return nil, nil
    59  	}
    60  
    61  	if response.StatusCode == http.StatusBadRequest {
    62  		return nil, []error{&errortypes.BadInput{
    63  			Message: fmt.Sprintf("Unexpected status code: %d. Run with request.debug = 1 for more info", response.StatusCode),
    64  		}}
    65  	}
    66  
    67  	if response.StatusCode != http.StatusOK {
    68  		return nil, []error{&errortypes.BadServerResponse{
    69  			Message: fmt.Sprintf("Unexpected status code: %d. Run with request.debug = 1 for more info", response.StatusCode),
    70  		}}
    71  	}
    72  
    73  	var bidResp openrtb2.BidResponse
    74  
    75  	if err := json.Unmarshal(response.Body, &bidResp); err != nil {
    76  		return nil, []error{err}
    77  	}
    78  
    79  	count := getBidCount(bidResp)
    80  	bidResponse := adapters.NewBidderResponseWithBidsCapacity(count)
    81  
    82  	var errs []error
    83  
    84  	for _, sb := range bidResp.SeatBid {
    85  		for i := range sb.Bid {
    86  			bidType, err := getMediaTypeForImp(sb.Bid[i].ImpID, internalRequest.Imp)
    87  			if err != nil {
    88  				errs = append(errs, err)
    89  			} else {
    90  				b := &adapters.TypedBid{
    91  					Bid:     &sb.Bid[i],
    92  					BidType: bidType,
    93  				}
    94  				bidResponse.Bids = append(bidResponse.Bids, b)
    95  			}
    96  		}
    97  	}
    98  	return bidResponse, errs
    99  }
   100  
   101  func getBidCount(bidResponse openrtb2.BidResponse) int {
   102  	c := 0
   103  	for _, sb := range bidResponse.SeatBid {
   104  		c = c + len(sb.Bid)
   105  	}
   106  	return c
   107  }
   108  
   109  func getMediaTypeForImp(impID string, imps []openrtb2.Imp) (openrtb_ext.BidType, error) {
   110  	mediaType := openrtb_ext.BidTypeBanner
   111  	for _, imp := range imps {
   112  		if imp.ID == impID {
   113  			if imp.Banner == nil && imp.Video != nil {
   114  				mediaType = openrtb_ext.BidTypeVideo
   115  			}
   116  			return mediaType, nil
   117  		}
   118  	}
   119  
   120  	// This shouldnt happen. Lets handle it just incase by returning an error.
   121  	return "", &errortypes.BadInput{
   122  		Message: fmt.Sprintf("Failed to find impression \"%s\"", impID),
   123  	}
   124  }