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

     1  package axonix
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"net/http"
     7  	"net/url"
     8  	"strconv"
     9  	"strings"
    10  	"text/template"
    11  
    12  	"github.com/prebid/openrtb/v20/openrtb2"
    13  	"github.com/prebid/prebid-server/v2/adapters"
    14  	"github.com/prebid/prebid-server/v2/config"
    15  	"github.com/prebid/prebid-server/v2/errortypes"
    16  	"github.com/prebid/prebid-server/v2/macros"
    17  	"github.com/prebid/prebid-server/v2/openrtb_ext"
    18  )
    19  
    20  type adapter struct {
    21  	EndpointTemplate *template.Template
    22  }
    23  
    24  func Builder(bidderName openrtb_ext.BidderName, config config.Adapter, server config.Server) (adapters.Bidder, error) {
    25  	endpoint, err := template.New("endpointTemplate").Parse(config.Endpoint)
    26  	if err != nil {
    27  		return nil, fmt.Errorf("unable to parse endpoint url template: %v", err)
    28  	}
    29  	bidder := &adapter{
    30  		EndpointTemplate: endpoint,
    31  	}
    32  	return bidder, nil
    33  }
    34  
    35  func (a *adapter) getEndpoint(ext *openrtb_ext.ExtImpAxonix) (string, error) {
    36  	endpointParams := macros.EndpointTemplateParams{
    37  		AccountID: url.PathEscape(ext.SupplyId),
    38  	}
    39  	return macros.ResolveMacros(a.EndpointTemplate, endpointParams)
    40  }
    41  
    42  func (a *adapter) MakeRequests(request *openrtb2.BidRequest, requestInfo *adapters.ExtraRequestInfo) ([]*adapters.RequestData, []error) {
    43  	var errors []error
    44  
    45  	var bidderExt adapters.ExtImpBidder
    46  	if err := json.Unmarshal(request.Imp[0].Ext, &bidderExt); err != nil {
    47  		errors = append(errors, &errortypes.BadInput{
    48  			Message: err.Error(),
    49  		})
    50  
    51  		return nil, errors
    52  	}
    53  
    54  	var axonixExt openrtb_ext.ExtImpAxonix
    55  	if err := json.Unmarshal(bidderExt.Bidder, &axonixExt); err != nil {
    56  		errors = append(errors, &errortypes.BadInput{
    57  			Message: err.Error(),
    58  		})
    59  
    60  		return nil, errors
    61  	}
    62  
    63  	endpoint, err := a.getEndpoint(&axonixExt)
    64  	if err != nil {
    65  		errors = append(errors, err)
    66  		return nil, errors
    67  	}
    68  
    69  	requestJSON, err := json.Marshal(request)
    70  	if err != nil {
    71  		errors = append(errors, err)
    72  		return nil, errors
    73  	}
    74  
    75  	headers := http.Header{}
    76  	headers.Add("Content-Type", "application/json")
    77  
    78  	requestData := &adapters.RequestData{
    79  		Method:  "POST",
    80  		Uri:     endpoint,
    81  		Body:    requestJSON,
    82  		Headers: headers,
    83  		ImpIDs:  openrtb_ext.GetImpIDs(request.Imp),
    84  	}
    85  
    86  	return []*adapters.RequestData{requestData}, nil
    87  }
    88  
    89  func (a *adapter) MakeBids(request *openrtb2.BidRequest, requestData *adapters.RequestData, responseData *adapters.ResponseData) (*adapters.BidderResponse, []error) {
    90  	if responseData.StatusCode == http.StatusNoContent {
    91  		return nil, nil
    92  	}
    93  
    94  	if responseData.StatusCode != http.StatusOK {
    95  		err := &errortypes.BadServerResponse{
    96  			Message: fmt.Sprintf("Unexpected status code: %d.", responseData.StatusCode),
    97  		}
    98  		return nil, []error{err}
    99  	}
   100  
   101  	var response openrtb2.BidResponse
   102  	if err := json.Unmarshal(responseData.Body, &response); err != nil {
   103  		return nil, []error{err}
   104  	}
   105  
   106  	bidResponse := adapters.NewBidderResponseWithBidsCapacity(len(request.Imp))
   107  	bidResponse.Currency = response.Cur
   108  	for _, seatBid := range response.SeatBid {
   109  		for _, bid := range seatBid.Bid {
   110  			bid := bid
   111  			resolveMacros(&bid)
   112  			b := &adapters.TypedBid{
   113  				Bid:     &bid,
   114  				BidType: getMediaType(bid.ImpID, request.Imp),
   115  			}
   116  			bidResponse.Bids = append(bidResponse.Bids, b)
   117  		}
   118  	}
   119  
   120  	return bidResponse, nil
   121  }
   122  
   123  func getMediaType(impId string, imps []openrtb2.Imp) openrtb_ext.BidType {
   124  	for _, imp := range imps {
   125  		if imp.ID == impId {
   126  			if imp.Native != nil {
   127  				return openrtb_ext.BidTypeNative
   128  			} else if imp.Video != nil {
   129  				return openrtb_ext.BidTypeVideo
   130  			}
   131  			return openrtb_ext.BidTypeBanner
   132  		}
   133  	}
   134  	return openrtb_ext.BidTypeBanner
   135  }
   136  
   137  func resolveMacros(bid *openrtb2.Bid) {
   138  	if bid == nil {
   139  		return
   140  	}
   141  	price := strconv.FormatFloat(bid.Price, 'f', -1, 64)
   142  	bid.NURL = strings.Replace(bid.NURL, "${AUCTION_PRICE}", price, -1)
   143  	bid.AdM = strings.Replace(bid.AdM, "${AUCTION_PRICE}", price, -1)
   144  }