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

     1  package lemmadigital
     2  
     3  import (
     4  	"encoding/json"
     5  	"errors"
     6  	"fmt"
     7  	"strconv"
     8  	"text/template"
     9  
    10  	"github.com/prebid/openrtb/v20/openrtb2"
    11  	"github.com/prebid/prebid-server/v2/adapters"
    12  	"github.com/prebid/prebid-server/v2/config"
    13  	"github.com/prebid/prebid-server/v2/errortypes"
    14  	"github.com/prebid/prebid-server/v2/macros"
    15  	"github.com/prebid/prebid-server/v2/openrtb_ext"
    16  )
    17  
    18  type adapter struct {
    19  	endpoint *template.Template
    20  }
    21  
    22  // Builder builds a new instance of the Lemmadigital adapter for the given bidder with the given config.
    23  func Builder(bidderName openrtb_ext.BidderName, config config.Adapter, server config.Server) (adapters.Bidder, error) {
    24  	template, err := template.New("endpointTemplate").Parse(config.Endpoint)
    25  	if err != nil {
    26  		return nil, fmt.Errorf("unable to parse endpoint url template: %v", err)
    27  	}
    28  
    29  	bidder := &adapter{
    30  		endpoint: template,
    31  	}
    32  	return bidder, nil
    33  }
    34  
    35  func (a *adapter) MakeRequests(request *openrtb2.BidRequest, requestInfo *adapters.ExtraRequestInfo) ([]*adapters.RequestData, []error) {
    36  	if len(request.Imp) == 0 {
    37  		return nil, []error{errors.New("Impression array should not be empty")}
    38  	}
    39  
    40  	var bidderExt adapters.ExtImpBidder
    41  	if err := json.Unmarshal(request.Imp[0].Ext, &bidderExt); err != nil {
    42  		return nil, []error{&errortypes.BadInput{
    43  			Message: fmt.Sprintf("Invalid imp.ext for impression index %d. Error Infomation: %s", 0, err.Error()),
    44  		}}
    45  	}
    46  
    47  	var impExt openrtb_ext.ImpExtLemmaDigital
    48  	if err := json.Unmarshal(bidderExt.Bidder, &impExt); err != nil {
    49  		return nil, []error{&errortypes.BadInput{
    50  			Message: fmt.Sprintf("Invalid imp.ext.bidder for impression index %d. Error Infomation: %s", 0, err.Error()),
    51  		}}
    52  	}
    53  
    54  	endpoint, err := a.buildEndpointURL(impExt)
    55  	if err != nil {
    56  		return nil, []error{err}
    57  	}
    58  
    59  	requestJSON, err := json.Marshal(request)
    60  	if err != nil {
    61  		return nil, []error{err}
    62  	}
    63  
    64  	requestData := &adapters.RequestData{
    65  		Method: "POST",
    66  		Uri:    endpoint,
    67  		Body:   requestJSON,
    68  		ImpIDs: openrtb_ext.GetImpIDs(request.Imp),
    69  	}
    70  
    71  	return []*adapters.RequestData{requestData}, nil
    72  }
    73  
    74  func (a *adapter) MakeBids(request *openrtb2.BidRequest, requestData *adapters.RequestData, responseData *adapters.ResponseData) (*adapters.BidderResponse, []error) {
    75  	if adapters.IsResponseStatusCodeNoContent(responseData) {
    76  		return nil, nil
    77  	}
    78  
    79  	if err := adapters.CheckResponseStatusCodeForErrors(responseData); err != nil {
    80  		return nil, []error{err}
    81  	}
    82  
    83  	var response openrtb2.BidResponse
    84  	if err := json.Unmarshal(responseData.Body, &response); err != nil {
    85  		return nil, []error{err}
    86  	}
    87  
    88  	bidType := openrtb_ext.BidTypeBanner
    89  	if nil != request.Imp[0].Video {
    90  		bidType = openrtb_ext.BidTypeVideo
    91  	}
    92  
    93  	bidResponse := adapters.NewBidderResponseWithBidsCapacity(len(request.Imp))
    94  	if len(response.Cur) > 0 {
    95  		bidResponse.Currency = response.Cur
    96  	}
    97  	if len(response.SeatBid) > 0 {
    98  		for i := range response.SeatBid[0].Bid {
    99  			b := &adapters.TypedBid{
   100  				Bid:     &response.SeatBid[0].Bid[i],
   101  				BidType: bidType,
   102  			}
   103  			bidResponse.Bids = append(bidResponse.Bids, b)
   104  		}
   105  	}
   106  
   107  	return bidResponse, nil
   108  }
   109  
   110  func (a *adapter) buildEndpointURL(params openrtb_ext.ImpExtLemmaDigital) (string, error) {
   111  	endpointParams := macros.EndpointTemplateParams{PublisherID: strconv.Itoa(params.PublisherId),
   112  		AdUnit: strconv.Itoa(params.AdId)}
   113  	return macros.ResolveMacros(a.endpoint, endpointParams)
   114  }