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

     1  package dxkulture
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"net/http"
     7  	"net/url"
     8  
     9  	"github.com/prebid/prebid-server/v2/adapters"
    10  	"github.com/prebid/prebid-server/v2/config"
    11  	"github.com/prebid/prebid-server/v2/errortypes"
    12  	"github.com/prebid/prebid-server/v2/openrtb_ext"
    13  
    14  	"github.com/prebid/openrtb/v20/openrtb2"
    15  )
    16  
    17  var markupTypeToBidType = map[openrtb2.MarkupType]openrtb_ext.BidType{
    18  	openrtb2.MarkupBanner: openrtb_ext.BidTypeBanner,
    19  	openrtb2.MarkupVideo:  openrtb_ext.BidTypeVideo,
    20  }
    21  
    22  type adapter struct {
    23  	endpoint string
    24  }
    25  
    26  // Builder builds a new instance of the DXKulture adapter for the given bidder with the given config.
    27  func Builder(bidderName openrtb_ext.BidderName, config config.Adapter, server config.Server) (adapters.Bidder, error) {
    28  	bidder := &adapter{
    29  		endpoint: config.Endpoint,
    30  	}
    31  	return bidder, nil
    32  }
    33  
    34  func (a *adapter) MakeRequests(request *openrtb2.BidRequest, reqInfo *adapters.ExtraRequestInfo) ([]*adapters.RequestData, []error) {
    35  	impressions := request.Imp
    36  
    37  	adapterRequests := make([]*adapters.RequestData, 0, len(impressions))
    38  	var errs []error
    39  
    40  	for _, impression := range impressions {
    41  		impExt, err := parseExt(&impression)
    42  		if err != nil {
    43  			errs = append(errs, err)
    44  			continue
    45  		}
    46  
    47  		request.Imp = []openrtb2.Imp{impression}
    48  		body, err := json.Marshal(request)
    49  		if err != nil {
    50  			errs = append(errs, err)
    51  			continue
    52  		}
    53  
    54  		if request.Test == 1 {
    55  			impExt.PublisherId = "test"
    56  		}
    57  
    58  		params := url.Values{}
    59  		params.Add("publisher_id", impExt.PublisherId)
    60  		params.Add("placement_id", impExt.PlacementId)
    61  
    62  		adapterRequests = append(adapterRequests, &adapters.RequestData{
    63  			Method:  http.MethodPost,
    64  			Uri:     a.endpoint + "?" + params.Encode(),
    65  			Body:    body,
    66  			Headers: getHeaders(request),
    67  			ImpIDs:  openrtb_ext.GetImpIDs(request.Imp),
    68  		})
    69  	}
    70  
    71  	request.Imp = impressions
    72  	return adapterRequests, errs
    73  }
    74  
    75  func (a *adapter) MakeBids(internalRequest *openrtb2.BidRequest, externalRequest *adapters.RequestData, response *adapters.ResponseData) (*adapters.BidderResponse, []error) {
    76  	if adapters.IsResponseStatusCodeNoContent(response) {
    77  		return nil, nil
    78  	}
    79  	if err := adapters.CheckResponseStatusCodeForErrors(response); err != nil {
    80  		return nil, []error{err}
    81  	}
    82  
    83  	var ortbResponse openrtb2.BidResponse
    84  	err := json.Unmarshal(response.Body, &ortbResponse)
    85  	if err != nil {
    86  		return nil, []error{&errortypes.BadServerResponse{
    87  			Message: "Bad Server Response",
    88  		}}
    89  	}
    90  
    91  	var bidErrors []error
    92  
    93  	bidderResponse := adapters.NewBidderResponseWithBidsCapacity(1)
    94  	for _, seatBid := range ortbResponse.SeatBid {
    95  		for i := range seatBid.Bid {
    96  			bid := seatBid.Bid[i]
    97  			bidType, err := getBidType(&bid)
    98  			if err != nil {
    99  				bidErrors = append(bidErrors, err)
   100  				continue
   101  			}
   102  
   103  			bidderResponse.Bids = append(bidderResponse.Bids, &adapters.TypedBid{
   104  				Bid:     &bid,
   105  				BidType: bidType,
   106  			})
   107  		}
   108  	}
   109  
   110  	return bidderResponse, bidErrors
   111  }
   112  
   113  func getBidType(bid *openrtb2.Bid) (openrtb_ext.BidType, error) {
   114  	if bidType, ok := markupTypeToBidType[bid.MType]; ok {
   115  		return bidType, nil
   116  	}
   117  	return "", &errortypes.BadServerResponse{
   118  		Message: fmt.Sprintf("Unsupported MType %d", bid.MType),
   119  	}
   120  }
   121  
   122  func parseExt(imp *openrtb2.Imp) (*openrtb_ext.ExtImpDXKulture, error) {
   123  	var bidderExt adapters.ExtImpBidder
   124  
   125  	if err := json.Unmarshal(imp.Ext, &bidderExt); err != nil {
   126  		return nil, &errortypes.BadInput{
   127  			Message: fmt.Sprintf("Ignoring imp id=%s, error while decoding extImpBidder, err: %s", imp.ID, err),
   128  		}
   129  	}
   130  
   131  	impExt := openrtb_ext.ExtImpDXKulture{}
   132  	err := json.Unmarshal(bidderExt.Bidder, &impExt)
   133  	if err != nil {
   134  		return nil, &errortypes.BadInput{
   135  			Message: fmt.Sprintf("Ignoring imp id=%s, error while decoding impExt, err: %s", imp.ID, err),
   136  		}
   137  	}
   138  
   139  	return &impExt, nil
   140  }
   141  
   142  func getHeaders(request *openrtb2.BidRequest) http.Header {
   143  	headers := http.Header{}
   144  	headers.Add("Content-Type", "application/json;charset=utf-8")
   145  	headers.Add("Accept", "application/json")
   146  	headers.Add("X-Openrtb-Version", "2.5")
   147  
   148  	if request.Site != nil {
   149  		if request.Site.Ref != "" {
   150  			headers.Set("Referer", request.Site.Ref)
   151  		}
   152  		if request.Site.Domain != "" {
   153  			headers.Add("Origin", request.Site.Domain)
   154  		}
   155  	}
   156  
   157  	if request.Device != nil {
   158  		if len(request.Device.UA) > 0 {
   159  			headers.Add("User-Agent", request.Device.UA)
   160  		}
   161  
   162  		if len(request.Device.IPv6) > 0 {
   163  			headers.Add("X-Forwarded-For", request.Device.IPv6)
   164  		}
   165  
   166  		if len(request.Device.IP) > 0 {
   167  			headers.Add("X-Forwarded-For", request.Device.IP)
   168  		}
   169  	}
   170  	return headers
   171  }