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

     1  package mgid
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/json"
     6  	"fmt"
     7  	"net/http"
     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  type MgidAdapter struct {
    17  	endpoint string
    18  }
    19  
    20  type ReqExt struct {
    21  	PlacementId string `json:"placementId"`
    22  	AccountId   string `json:"accountId"`
    23  }
    24  
    25  type RespBidExt struct {
    26  	CreativeType openrtb_ext.BidType `json:"crtype"`
    27  }
    28  
    29  func (a *MgidAdapter) MakeRequests(request *openrtb2.BidRequest, reqInfo *adapters.ExtraRequestInfo) (adapterRequests []*adapters.RequestData, errs []error) {
    30  
    31  	adapterReq, errs := a.makeRequest(request)
    32  	if adapterReq != nil && len(errs) == 0 {
    33  		adapterRequests = append(adapterRequests, adapterReq)
    34  	}
    35  
    36  	return
    37  }
    38  
    39  func (a *MgidAdapter) makeRequest(request *openrtb2.BidRequest) (*adapters.RequestData, []error) {
    40  	var errs []error
    41  
    42  	path, err := preprocess(request)
    43  	if err != nil {
    44  		errs = append(errs, err)
    45  		return nil, errs
    46  	}
    47  
    48  	// Last Step
    49  	reqJSON, err := json.Marshal(request)
    50  	if err != nil {
    51  		errs = append(errs, err)
    52  		return nil, errs
    53  	}
    54  
    55  	headers := http.Header{}
    56  	headers.Add("Content-Type", "application/json;charset=utf-8")
    57  	headers.Add("Accept", "application/json")
    58  
    59  	return &adapters.RequestData{
    60  		Method:  "POST",
    61  		Uri:     a.endpoint + path,
    62  		Body:    reqJSON,
    63  		Headers: headers,
    64  		ImpIDs:  openrtb_ext.GetImpIDs(request.Imp),
    65  	}, errs
    66  }
    67  
    68  // Mutate the request to get it ready to send to yieldmo.
    69  func preprocess(request *openrtb2.BidRequest) (path string, err error) {
    70  	if request.TMax == 0 {
    71  		request.TMax = 200
    72  	}
    73  	for i := 0; i < len(request.Imp); i++ {
    74  		var imp = request.Imp[i]
    75  		var bidderExt adapters.ExtImpBidder
    76  
    77  		if err := json.Unmarshal(imp.Ext, &bidderExt); err != nil {
    78  			return "", &errortypes.BadInput{
    79  				Message: err.Error(),
    80  			}
    81  		}
    82  
    83  		var mgidExt openrtb_ext.ExtImpMgid
    84  
    85  		if err := json.Unmarshal(bidderExt.Bidder, &mgidExt); err != nil {
    86  			return "", &errortypes.BadInput{
    87  				Message: err.Error(),
    88  			}
    89  		}
    90  
    91  		if path == "" {
    92  			path = mgidExt.AccountId
    93  		}
    94  		if mgidExt.PlacementId == "" {
    95  			request.Imp[i].TagID = imp.ID
    96  		} else {
    97  			request.Imp[i].TagID = mgidExt.PlacementId + "/" + imp.ID
    98  		}
    99  
   100  		cur := ""
   101  		if mgidExt.Currency != "" && mgidExt.Currency != "USD" {
   102  			cur = mgidExt.Currency
   103  		}
   104  		if cur == "" && mgidExt.Cur != "" && mgidExt.Cur != "USD" {
   105  			cur = mgidExt.Cur
   106  		}
   107  		bidfloor := mgidExt.BidFloor
   108  		if bidfloor <= 0 {
   109  			bidfloor = mgidExt.BidFloor2
   110  		}
   111  		if bidfloor > 0 {
   112  			request.Imp[i].BidFloor = bidfloor
   113  		}
   114  		if cur != "" {
   115  			request.Imp[i].BidFloorCur = cur
   116  		}
   117  	}
   118  	if path == "" {
   119  		return "", &errortypes.BadInput{
   120  			Message: "accountId is not set",
   121  		}
   122  	}
   123  
   124  	return
   125  }
   126  
   127  func (a *MgidAdapter) MakeBids(bidReq *openrtb2.BidRequest, unused *adapters.RequestData, response *adapters.ResponseData) (*adapters.BidderResponse, []error) {
   128  	if response.StatusCode == http.StatusNoContent {
   129  		return nil, nil
   130  	}
   131  
   132  	if response.StatusCode == http.StatusBadRequest {
   133  		return nil, []error{&errortypes.BadInput{
   134  			Message: fmt.Sprintf("Unexpected status code: %d. Run with request.debug = 1 for more info", response.StatusCode),
   135  		}}
   136  	}
   137  
   138  	if response.StatusCode != http.StatusOK {
   139  		return nil, []error{&errortypes.BadServerResponse{
   140  			Message: fmt.Sprintf("Unexpected status code: %d. Run with request.debug = 1 for more info", response.StatusCode),
   141  		}}
   142  	}
   143  
   144  	var bidResp openrtb2.BidResponse
   145  
   146  	if err := json.Unmarshal(response.Body, &bidResp); err != nil {
   147  		return nil, []error{err}
   148  	}
   149  
   150  	bidResponse := adapters.NewBidderResponseWithBidsCapacity(1)
   151  
   152  	bidResponse.Currency = bidResp.Cur
   153  
   154  	for _, sb := range bidResp.SeatBid {
   155  		for i := range sb.Bid {
   156  			bidType := openrtb_ext.BidTypeBanner
   157  			if len(sb.Bid[i].Ext) > 0 && bytes.Contains(sb.Bid[i].Ext, []byte("crtype")) {
   158  				ext := RespBidExt{}
   159  				if err := json.Unmarshal(sb.Bid[i].Ext, &ext); err == nil && len(ext.CreativeType) > 0 {
   160  					bidType = ext.CreativeType
   161  				}
   162  			}
   163  			bidResponse.Bids = append(bidResponse.Bids, &adapters.TypedBid{
   164  				Bid:     &sb.Bid[i],
   165  				BidType: bidType,
   166  			})
   167  		}
   168  	}
   169  	return bidResponse, nil
   170  }
   171  
   172  // Builder builds a new instance of the Mgid adapter for the given bidder with the given config.
   173  func Builder(bidderName openrtb_ext.BidderName, config config.Adapter, server config.Server) (adapters.Bidder, error) {
   174  	bidder := &MgidAdapter{
   175  		endpoint: config.Endpoint,
   176  	}
   177  	return bidder, nil
   178  }