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

     1  package screencore
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"net/http"
     7  	"text/template"
     8  
     9  	"github.com/prebid/openrtb/v20/openrtb2"
    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/macros"
    14  	"github.com/prebid/prebid-server/v2/openrtb_ext"
    15  )
    16  
    17  type adapter struct {
    18  	endpoint *template.Template
    19  }
    20  
    21  func Builder(bidderName openrtb_ext.BidderName, config config.Adapter, server config.Server) (adapters.Bidder, error) {
    22  	template, err := template.New("endpointTemplate").Parse(config.Endpoint)
    23  	if err != nil {
    24  		return nil, fmt.Errorf("unable to parse endpoint url template: %v", err)
    25  	}
    26  
    27  	bidder := &adapter{
    28  		endpoint: template,
    29  	}
    30  	return bidder, nil
    31  }
    32  
    33  func getHeaders(request *openrtb2.BidRequest) http.Header {
    34  	headers := http.Header{}
    35  	headers.Add("Content-Type", "application/json;charset=utf-8")
    36  	headers.Add("Accept", "application/json")
    37  	headers.Add("X-Openrtb-Version", "2.5")
    38  
    39  	if request.Device != nil {
    40  		if len(request.Device.UA) > 0 {
    41  			headers.Add("User-Agent", request.Device.UA)
    42  		}
    43  
    44  		if len(request.Device.IPv6) > 0 {
    45  			headers.Add("X-Forwarded-For", request.Device.IPv6)
    46  		}
    47  
    48  		if len(request.Device.IP) > 0 {
    49  			headers.Add("X-Forwarded-For", request.Device.IP)
    50  		}
    51  	}
    52  
    53  	return headers
    54  }
    55  
    56  func (a *adapter) MakeRequests(openRTBRequest *openrtb2.BidRequest, reqInfo *adapters.ExtraRequestInfo) (requestsToBidder []*adapters.RequestData, errs []error) {
    57  	if len(openRTBRequest.Imp) == 0 {
    58  		return nil, []error{&errortypes.BadInput{
    59  			Message: "Empty Imp array in BidRequest",
    60  		}}
    61  	}
    62  
    63  	screencoreExt, err := getImpressionExt(&openRTBRequest.Imp[0])
    64  	if err != nil {
    65  		return nil, []error{err}
    66  	}
    67  
    68  	openRTBRequest.Imp[0].Ext = nil
    69  
    70  	url, err := a.buildEndpointURL(screencoreExt)
    71  	if err != nil {
    72  		return nil, []error{err}
    73  	}
    74  
    75  	reqJSON, err := json.Marshal(openRTBRequest)
    76  	if err != nil {
    77  		return nil, []error{err}
    78  	}
    79  
    80  	return []*adapters.RequestData{{
    81  		Method:  http.MethodPost,
    82  		Body:    reqJSON,
    83  		Uri:     url,
    84  		Headers: getHeaders(openRTBRequest),
    85  		ImpIDs:  openrtb_ext.GetImpIDs(openRTBRequest.Imp),
    86  	}}, nil
    87  }
    88  
    89  func getImpressionExt(imp *openrtb2.Imp) (*openrtb_ext.ExtScreencore, error) {
    90  	var bidderExt adapters.ExtImpBidder
    91  	if err := json.Unmarshal(imp.Ext, &bidderExt); err != nil {
    92  		return nil, &errortypes.BadInput{
    93  			Message: "Error parsing screencoreExt - " + err.Error(),
    94  		}
    95  	}
    96  	var screencoreExt openrtb_ext.ExtScreencore
    97  	if err := json.Unmarshal(bidderExt.Bidder, &screencoreExt); err != nil {
    98  		return nil, &errortypes.BadInput{
    99  			Message: "Error parsing bidderExt - " + err.Error(),
   100  		}
   101  	}
   102  
   103  	return &screencoreExt, nil
   104  }
   105  
   106  func (a *adapter) buildEndpointURL(params *openrtb_ext.ExtScreencore) (string, error) {
   107  	endpointParams := macros.EndpointTemplateParams{AccountID: params.AccountID, SourceId: params.PlacementID}
   108  	return macros.ResolveMacros(a.endpoint, endpointParams)
   109  }
   110  
   111  func checkResponseStatusCodes(response *adapters.ResponseData) error {
   112  	if response.StatusCode == http.StatusServiceUnavailable {
   113  		return &errortypes.BadServerResponse{
   114  			Message: fmt.Sprintf("Something went wrong Status Code: [ %d ] ", response.StatusCode),
   115  		}
   116  	}
   117  
   118  	return adapters.CheckResponseStatusCodeForErrors(response)
   119  }
   120  
   121  func (a *adapter) MakeBids(openRTBRequest *openrtb2.BidRequest, requestToBidder *adapters.RequestData, bidderRawResponse *adapters.ResponseData) (bidderResponse *adapters.BidderResponse, errs []error) {
   122  	if adapters.IsResponseStatusCodeNoContent(bidderRawResponse) {
   123  		return nil, nil
   124  	}
   125  
   126  	httpStatusError := checkResponseStatusCodes(bidderRawResponse)
   127  	if httpStatusError != nil {
   128  		return nil, []error{httpStatusError}
   129  	}
   130  
   131  	responseBody := bidderRawResponse.Body
   132  	var bidResp openrtb2.BidResponse
   133  	if err := json.Unmarshal(responseBody, &bidResp); err != nil {
   134  		return nil, []error{&errortypes.BadServerResponse{
   135  			Message: "Bad Server Response",
   136  		}}
   137  	}
   138  
   139  	if len(bidResp.SeatBid) == 0 {
   140  		return nil, []error{&errortypes.BadServerResponse{
   141  			Message: "Empty SeatBid array",
   142  		}}
   143  	}
   144  
   145  	bidResponse := adapters.NewBidderResponseWithBidsCapacity(5)
   146  	var bidsArray []*adapters.TypedBid
   147  
   148  	for _, sb := range bidResp.SeatBid {
   149  		for idx, bid := range sb.Bid {
   150  			bidType, err := getMediaTypeForImp(bid)
   151  			if err != nil {
   152  				return nil, []error{err}
   153  			}
   154  
   155  			bidsArray = append(bidsArray, &adapters.TypedBid{
   156  				Bid:     &sb.Bid[idx],
   157  				BidType: bidType,
   158  			})
   159  		}
   160  	}
   161  
   162  	bidResponse.Bids = bidsArray
   163  	return bidResponse, nil
   164  }
   165  
   166  func getMediaTypeForImp(bid openrtb2.Bid) (openrtb_ext.BidType, error) {
   167  	switch bid.MType {
   168  	case openrtb2.MarkupBanner:
   169  		return openrtb_ext.BidTypeBanner, nil
   170  	case openrtb2.MarkupVideo:
   171  		return openrtb_ext.BidTypeVideo, nil
   172  	case openrtb2.MarkupNative:
   173  		return openrtb_ext.BidTypeNative, nil
   174  	default:
   175  		return "", fmt.Errorf("unsupported MType %d", bid.MType)
   176  	}
   177  }