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

     1  package gothamads
     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  	impExt, err := getImpressionExt(&openRTBRequest.Imp[0])
    58  	if err != nil {
    59  		return nil, []error{err}
    60  	}
    61  
    62  	openRTBRequest.Imp[0].Ext = nil
    63  
    64  	url, err := a.buildEndpointURL(impExt)
    65  	if err != nil {
    66  		return nil, []error{err}
    67  	}
    68  
    69  	reqJSON, err := json.Marshal(openRTBRequest)
    70  	if err != nil {
    71  		return nil, []error{err}
    72  	}
    73  
    74  	return []*adapters.RequestData{{
    75  		Method:  http.MethodPost,
    76  		Body:    reqJSON,
    77  		Uri:     url,
    78  		Headers: getHeaders(openRTBRequest),
    79  		ImpIDs:  openrtb_ext.GetImpIDs(openRTBRequest.Imp),
    80  	}}, nil
    81  }
    82  
    83  func getImpressionExt(imp *openrtb2.Imp) (*openrtb_ext.ExtGothamAds, error) {
    84  	var bidderExt adapters.ExtImpBidder
    85  	if err := json.Unmarshal(imp.Ext, &bidderExt); err != nil {
    86  		return nil, &errortypes.BadInput{
    87  			Message: err.Error(),
    88  		}
    89  	}
    90  	var gothamadsExt openrtb_ext.ExtGothamAds
    91  	if err := json.Unmarshal(bidderExt.Bidder, &gothamadsExt); err != nil {
    92  		return nil, &errortypes.BadInput{
    93  			Message: err.Error(),
    94  		}
    95  	}
    96  
    97  	return &gothamadsExt, nil
    98  }
    99  
   100  func (a *adapter) buildEndpointURL(params *openrtb_ext.ExtGothamAds) (string, error) {
   101  	endpointParams := macros.EndpointTemplateParams{AccountID: params.AccountID}
   102  	return macros.ResolveMacros(a.endpoint, endpointParams)
   103  }
   104  
   105  func checkResponseStatusCodes(response *adapters.ResponseData) error {
   106  	if response.StatusCode == http.StatusServiceUnavailable {
   107  		return &errortypes.BadServerResponse{
   108  			Message: fmt.Sprintf("Something went wrong Status Code: [ %d ] ", response.StatusCode),
   109  		}
   110  	}
   111  
   112  	return adapters.CheckResponseStatusCodeForErrors(response)
   113  }
   114  
   115  func (a *adapter) MakeBids(openRTBRequest *openrtb2.BidRequest, requestToBidder *adapters.RequestData, bidderRawResponse *adapters.ResponseData) (bidderResponse *adapters.BidderResponse, errs []error) {
   116  	if adapters.IsResponseStatusCodeNoContent(bidderRawResponse) {
   117  		return nil, nil
   118  	}
   119  
   120  	httpStatusError := checkResponseStatusCodes(bidderRawResponse)
   121  	if httpStatusError != nil {
   122  		return nil, []error{httpStatusError}
   123  	}
   124  
   125  	responseBody := bidderRawResponse.Body
   126  	var bidResp openrtb2.BidResponse
   127  	if err := json.Unmarshal(responseBody, &bidResp); err != nil {
   128  		return nil, []error{&errortypes.BadServerResponse{
   129  			Message: "Bad Server Response",
   130  		}}
   131  	}
   132  
   133  	if len(bidResp.SeatBid) == 0 {
   134  		return nil, []error{&errortypes.BadServerResponse{
   135  			Message: "Empty SeatBid array",
   136  		}}
   137  	}
   138  
   139  	bidResponse := adapters.NewBidderResponseWithBidsCapacity(5)
   140  	var bidsArray []*adapters.TypedBid
   141  
   142  	for _, sb := range bidResp.SeatBid {
   143  		for idx, bid := range sb.Bid {
   144  			bidType, err := getMediaTypeForImp(bid)
   145  			if err != nil {
   146  				return nil, []error{err}
   147  			}
   148  
   149  			bidsArray = append(bidsArray, &adapters.TypedBid{
   150  				Bid:     &sb.Bid[idx],
   151  				BidType: bidType,
   152  			})
   153  		}
   154  	}
   155  
   156  	bidResponse.Bids = bidsArray
   157  	return bidResponse, nil
   158  }
   159  
   160  func getMediaTypeForImp(bid openrtb2.Bid) (openrtb_ext.BidType, error) {
   161  	switch bid.MType {
   162  	case openrtb2.MarkupBanner:
   163  		return openrtb_ext.BidTypeBanner, nil
   164  	case openrtb2.MarkupVideo:
   165  		return openrtb_ext.BidTypeVideo, nil
   166  	case openrtb2.MarkupNative:
   167  		return openrtb_ext.BidTypeNative, nil
   168  	default:
   169  		return "", fmt.Errorf("unsupported MType %d", bid.MType)
   170  	}
   171  }