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

     1  package bliink
     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  	endpoint string
    18  }
    19  
    20  // Builder builds a new instance of the Bliink adapter for the given bidder with the given config.
    21  func Builder(_ openrtb_ext.BidderName, config config.Adapter, server config.Server) (adapters.Bidder, error) {
    22  	bidder := &adapter{
    23  		endpoint: config.Endpoint,
    24  	}
    25  
    26  	return bidder, nil
    27  }
    28  
    29  func (a *adapter) MakeRequests(request *openrtb2.BidRequest, _ *adapters.ExtraRequestInfo) ([]*adapters.RequestData, []error) {
    30  	requestJSON, err := json.Marshal(request)
    31  	if err != nil {
    32  		return nil, []error{err}
    33  	}
    34  
    35  	headers := http.Header{}
    36  	headers.Add("Content-Type", "application/json;charset=utf-8")
    37  	headers.Add("Accept", "application/json")
    38  	headers.Add("x-openrtb-version", "2.5")
    39  
    40  	requestData := &adapters.RequestData{
    41  		Method:  "POST",
    42  		Uri:     a.endpoint,
    43  		Body:    requestJSON,
    44  		Headers: headers,
    45  		ImpIDs:  openrtb_ext.GetImpIDs(request.Imp),
    46  	}
    47  
    48  	return []*adapters.RequestData{requestData}, nil
    49  }
    50  
    51  func (a *adapter) MakeBids(request *openrtb2.BidRequest, _ *adapters.RequestData, responseData *adapters.ResponseData) (*adapters.BidderResponse, []error) {
    52  	if responseData.StatusCode == http.StatusNoContent {
    53  		return nil, nil
    54  	}
    55  
    56  	if responseData.StatusCode == http.StatusBadRequest {
    57  		err := &errortypes.BadInput{
    58  			Message: "Unexpected status code: 400. Bad request from publisher. Run with request.debug = 1 for more info.",
    59  		}
    60  		return nil, []error{err}
    61  	}
    62  
    63  	if responseData.StatusCode != http.StatusOK {
    64  		err := &errortypes.BadServerResponse{
    65  			Message: fmt.Sprintf("Unexpected status code: %d. Run with request.debug = 1 for more info.", responseData.StatusCode),
    66  		}
    67  		return nil, []error{err}
    68  	}
    69  
    70  	var response openrtb2.BidResponse
    71  	if err := json.Unmarshal(responseData.Body, &response); err != nil {
    72  		return nil, []error{err}
    73  	}
    74  
    75  	if len(response.SeatBid) == 0 {
    76  		return nil, []error{&errortypes.BadServerResponse{
    77  			Message: "Empty SeatBid array",
    78  		}}
    79  	}
    80  
    81  	bidResponse := adapters.NewBidderResponseWithBidsCapacity(len(request.Imp))
    82  	bidResponse.Currency = response.Cur
    83  	var errs []error
    84  	for _, seatBid := range response.SeatBid {
    85  		for i, bid := range seatBid.Bid {
    86  			mediaType, err := getMediaTypeForImp(bid.ImpID, request.Imp)
    87  			if err != nil {
    88  				errs = append(errs, err)
    89  				continue
    90  			}
    91  			bidResponse.Bids = append(bidResponse.Bids, &adapters.TypedBid{
    92  				Bid:     &seatBid.Bid[i],
    93  				BidType: mediaType,
    94  			})
    95  		}
    96  	}
    97  
    98  	return bidResponse, errs
    99  }
   100  
   101  func getMediaTypeForImp(impId string, imps []openrtb2.Imp) (openrtb_ext.BidType, error) {
   102  	var mediaType openrtb_ext.BidType
   103  	var typeCnt = 0
   104  	for _, imp := range imps {
   105  		if imp.ID == impId {
   106  			if imp.Banner != nil {
   107  				typeCnt += 1
   108  				mediaType = openrtb_ext.BidTypeBanner
   109  			}
   110  			if imp.Native != nil {
   111  				typeCnt += 1
   112  				mediaType = openrtb_ext.BidTypeNative
   113  			}
   114  			if imp.Video != nil {
   115  				typeCnt += 1
   116  				mediaType = openrtb_ext.BidTypeVideo
   117  			}
   118  		}
   119  	}
   120  	if typeCnt == 1 {
   121  		return mediaType, nil
   122  	}
   123  	return mediaType, fmt.Errorf("unable to fetch mediaType in multi-format: %s", impId)
   124  }