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

     1  package roulax
     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/macros"
    13  	"github.com/prebid/prebid-server/v2/openrtb_ext"
    14  )
    15  
    16  type adapter struct {
    17  	endpoint *template.Template
    18  }
    19  
    20  // Builder builds a new instance of the Roulax 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  	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  // getImpAdotExt parses and return first imp ext or nil
    34  func getImpRoulaxExt(imp *openrtb2.Imp) (openrtb_ext.ExtImpRoulax, error) {
    35  	var extBidder adapters.ExtImpBidder
    36  	if err := json.Unmarshal(imp.Ext, &extBidder); err != nil {
    37  		return openrtb_ext.ExtImpRoulax{}, err
    38  	}
    39  	var extImpRoulax openrtb_ext.ExtImpRoulax
    40  	if err := json.Unmarshal(extBidder.Bidder, &extImpRoulax); err != nil {
    41  		return openrtb_ext.ExtImpRoulax{}, err
    42  	}
    43  	return extImpRoulax, nil
    44  }
    45  
    46  // MakeRequests makes the HTTP requests which should be made to fetch bids.
    47  func (a *adapter) MakeRequests(request *openrtb2.BidRequest, reqInfo *adapters.ExtraRequestInfo) (res []*adapters.RequestData, errs []error) {
    48  	reqJson, err := json.Marshal(request)
    49  	if err != nil {
    50  		return nil, append(errs, err)
    51  	}
    52  	headers := http.Header{}
    53  	headers.Add("Content-Type", "application/json;charset=utf-8")
    54  	roulaxExt, err := getImpRoulaxExt(&request.Imp[0])
    55  	if err != nil {
    56  		return nil, append(errs, err)
    57  	}
    58  
    59  	endpointParams := macros.EndpointTemplateParams{AccountID: roulaxExt.Pid, PublisherID: roulaxExt.PublisherPath}
    60  	url, err := macros.ResolveMacros(a.endpoint, endpointParams)
    61  	if err != nil {
    62  		return nil, append(errs, err)
    63  	}
    64  
    65  	return []*adapters.RequestData{{
    66  		Method:  "POST",
    67  		Uri:     url,
    68  		Body:    reqJson,
    69  		Headers: headers,
    70  		ImpIDs:  openrtb_ext.GetImpIDs(request.Imp),
    71  	}}, errs
    72  }
    73  
    74  // MakeBids unpacks the server's response into Bids.
    75  func (a *adapter) MakeBids(request *openrtb2.BidRequest, _ *adapters.RequestData, responseData *adapters.ResponseData) (*adapters.BidderResponse, []error) {
    76  	if adapters.IsResponseStatusCodeNoContent(responseData) {
    77  		return nil, nil
    78  	}
    79  	if err := adapters.CheckResponseStatusCodeForErrors(responseData); err != nil {
    80  		return nil, []error{err}
    81  	}
    82  	var response openrtb2.BidResponse
    83  	if err := json.Unmarshal(responseData.Body, &response); err != nil {
    84  		return nil, []error{err}
    85  	}
    86  	bidResponse := adapters.NewBidderResponseWithBidsCapacity(len(request.Imp))
    87  	bidResponse.Currency = response.Cur
    88  	var errs []error
    89  	for _, seatBid := range response.SeatBid {
    90  		for i, bid := range seatBid.Bid {
    91  			bidType, err := getMediaTypeForBid(bid)
    92  			if err != nil {
    93  				errs = append(errs, err)
    94  				continue
    95  			}
    96  
    97  			bidResponse.Bids = append(bidResponse.Bids, &adapters.TypedBid{
    98  				Bid:     &seatBid.Bid[i],
    99  				BidType: bidType,
   100  			})
   101  		}
   102  	}
   103  	return bidResponse, errs
   104  }
   105  
   106  func getMediaTypeForBid(bid openrtb2.Bid) (openrtb_ext.BidType, error) {
   107  	switch bid.MType {
   108  	case openrtb2.MarkupBanner:
   109  		return openrtb_ext.BidTypeBanner, nil
   110  	case openrtb2.MarkupVideo:
   111  		return openrtb_ext.BidTypeVideo, nil
   112  	case openrtb2.MarkupNative:
   113  		return openrtb_ext.BidTypeNative, nil
   114  	default:
   115  		return "", fmt.Errorf("Unable to fetch mediaType in impID: %s, mType: %d", bid.ImpID, bid.MType)
   116  	}
   117  }