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

     1  package adprime
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"net/http"
     7  	"strings"
     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/openrtb_ext"
    14  )
    15  
    16  // AdprimeAdapter struct
    17  type AdprimeAdapter struct {
    18  	URI string
    19  }
    20  
    21  // Builder builds a new instance of the Adprime adapter for the given bidder with the given config.
    22  func Builder(bidderName openrtb_ext.BidderName, config config.Adapter, server config.Server) (adapters.Bidder, error) {
    23  	bidder := &AdprimeAdapter{
    24  		URI: config.Endpoint,
    25  	}
    26  	return bidder, nil
    27  }
    28  
    29  // MakeRequests create bid request for adprime demand
    30  func (a *AdprimeAdapter) MakeRequests(request *openrtb2.BidRequest, reqInfo *adapters.ExtraRequestInfo) ([]*adapters.RequestData, []error) {
    31  	var errs []error
    32  	var err error
    33  
    34  	var adapterRequests []*adapters.RequestData
    35  
    36  	var bidderExt adapters.ExtImpBidder
    37  	var adprimeExt openrtb_ext.ExtImpAdprime
    38  
    39  	reqCopy := *request
    40  	for _, imp := range request.Imp {
    41  		reqCopy.Imp = []openrtb2.Imp{imp}
    42  
    43  		err = json.Unmarshal(reqCopy.Imp[0].Ext, &bidderExt)
    44  		if err != nil {
    45  			errs = append(errs, err)
    46  			return nil, errs
    47  		}
    48  
    49  		err = json.Unmarshal(bidderExt.Bidder, &adprimeExt)
    50  		if err != nil {
    51  			errs = append(errs, err)
    52  			return nil, errs
    53  		}
    54  
    55  		// tagId
    56  		tagID := adprimeExt.TagID
    57  		reqCopy.Imp[0].TagID = tagID
    58  
    59  		// placementId
    60  		newExt, err := json.Marshal(
    61  			map[string]interface{}{
    62  				"bidder": map[string]interface{}{
    63  					"TagID":       tagID,
    64  					"placementId": tagID,
    65  				},
    66  			})
    67  		if err != nil {
    68  			errs = append(errs, err)
    69  			return nil, errs
    70  		}
    71  		reqCopy.Imp[0].Ext = newExt
    72  
    73  		// keywords
    74  		if reqCopy.Site != nil && len(adprimeExt.Keywords) > 0 {
    75  			siteCopy := *reqCopy.Site
    76  			siteCopy.Keywords = strings.Join(adprimeExt.Keywords, ",")
    77  			reqCopy.Site = &siteCopy
    78  		}
    79  
    80  		// audiences
    81  		if reqCopy.Site != nil && len(adprimeExt.Audiences) > 0 {
    82  			if reqCopy.User == nil {
    83  				reqCopy.User = &openrtb2.User{}
    84  			}
    85  			userCopy := *reqCopy.User
    86  			userCopy.CustomData = strings.Join(adprimeExt.Audiences, ",")
    87  			reqCopy.User = &userCopy
    88  		}
    89  
    90  		adapterReq, errors := a.makeRequest(&reqCopy)
    91  		if adapterReq != nil {
    92  			adapterRequests = append(adapterRequests, adapterReq)
    93  		}
    94  		errs = append(errs, errors...)
    95  	}
    96  	return adapterRequests, errs
    97  }
    98  
    99  func (a *AdprimeAdapter) makeRequest(request *openrtb2.BidRequest) (*adapters.RequestData, []error) {
   100  
   101  	var errs []error
   102  
   103  	reqJSON, err := json.Marshal(request)
   104  
   105  	if err != nil {
   106  		errs = append(errs, err)
   107  		return nil, errs
   108  	}
   109  
   110  	headers := http.Header{}
   111  	headers.Add("Content-Type", "application/json;charset=utf-8")
   112  	headers.Add("Accept", "application/json")
   113  	return &adapters.RequestData{
   114  		Method:  "POST",
   115  		Uri:     a.URI,
   116  		Body:    reqJSON,
   117  		Headers: headers,
   118  		ImpIDs:  openrtb_ext.GetImpIDs(request.Imp),
   119  	}, errs
   120  }
   121  
   122  // MakeBids makes the bids
   123  func (a *AdprimeAdapter) MakeBids(internalRequest *openrtb2.BidRequest, externalRequest *adapters.RequestData, response *adapters.ResponseData) (*adapters.BidderResponse, []error) {
   124  	var errs []error
   125  
   126  	if response.StatusCode == http.StatusNoContent {
   127  		return nil, nil
   128  	}
   129  
   130  	if response.StatusCode == http.StatusNotFound {
   131  		return nil, []error{&errortypes.BadServerResponse{
   132  			Message: fmt.Sprintf("Page not found: %d. Run with request.debug = 1 for more info", response.StatusCode),
   133  		}}
   134  	}
   135  
   136  	if response.StatusCode != http.StatusOK {
   137  		return nil, []error{&errortypes.BadServerResponse{
   138  			Message: fmt.Sprintf("Unexpected status code: %d. Run with request.debug = 1 for more info", response.StatusCode),
   139  		}}
   140  	}
   141  
   142  	var bidResp openrtb2.BidResponse
   143  
   144  	if err := json.Unmarshal(response.Body, &bidResp); err != nil {
   145  		return nil, []error{err}
   146  	}
   147  
   148  	bidResponse := adapters.NewBidderResponseWithBidsCapacity(1)
   149  
   150  	for _, sb := range bidResp.SeatBid {
   151  		for i := range sb.Bid {
   152  			bidType, err := getBidMediaType(&sb.Bid[i])
   153  
   154  			if err != nil {
   155  				errs = append(errs, err)
   156  			} else {
   157  				b := &adapters.TypedBid{
   158  					Bid:     &sb.Bid[i],
   159  					BidType: bidType,
   160  				}
   161  				bidResponse.Bids = append(bidResponse.Bids, b)
   162  			}
   163  		}
   164  	}
   165  	return bidResponse, errs
   166  }
   167  
   168  func getBidMediaType(bid *openrtb2.Bid) (openrtb_ext.BidType, error) {
   169  	switch bid.MType {
   170  	case openrtb2.MarkupBanner:
   171  		return openrtb_ext.BidTypeBanner, nil
   172  	case openrtb2.MarkupVideo:
   173  		return openrtb_ext.BidTypeVideo, nil
   174  	case openrtb2.MarkupNative:
   175  		return openrtb_ext.BidTypeNative, nil
   176  	default:
   177  		return "", fmt.Errorf("Unable to fetch mediaType in multi-format: %s", bid.ImpID)
   178  	}
   179  }