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

     1  package mabidder
     2  
     3  import (
     4  	"encoding/json"
     5  
     6  	"github.com/prebid/openrtb/v20/openrtb2"
     7  	"github.com/prebid/prebid-server/v2/adapters"
     8  	"github.com/prebid/prebid-server/v2/config"
     9  	"github.com/prebid/prebid-server/v2/openrtb_ext"
    10  )
    11  
    12  type serverResponse struct {
    13  	Responses       []bidResponse
    14  	PrivateIdStatus string `json:"-"`
    15  }
    16  
    17  type bidResponse struct {
    18  	RequestID         string  `json:"requestId"`
    19  	Currency          string  `json:"currency"`
    20  	Width             int32   `json:"width"`
    21  	Height            int32   `json:"height"`
    22  	PlacementId       string  `json:"creativeId"`
    23  	Deal              string  `json:"dealId,omitempty"`
    24  	NetRevenue        bool    `json:"netRevenue"`
    25  	TimeToLiveSeconds int32   `json:"ttl"`
    26  	AdTag             string  `json:"ad"`
    27  	MediaType         string  `json:"mediaType"`
    28  	Meta              meta    `json:"meta"`
    29  	CPM               float32 `json:"cpm"`
    30  }
    31  
    32  type meta struct {
    33  	AdDomain []string `json:"advertiserDomains"`
    34  }
    35  
    36  type adapter struct {
    37  	endpoint string
    38  }
    39  
    40  // Builder builds a new instance of the Mabidder adapter for the given bidder with the given config.
    41  func Builder(bidderName openrtb_ext.BidderName, config config.Adapter, server config.Server) (adapters.Bidder, error) {
    42  	bidder := &adapter{
    43  		endpoint: config.Endpoint,
    44  	}
    45  	return bidder, nil
    46  }
    47  
    48  func (a *adapter) MakeRequests(request *openrtb2.BidRequest, requestInfo *adapters.ExtraRequestInfo) ([]*adapters.RequestData, []error) {
    49  	requestJSON, err := json.Marshal(request)
    50  	if err != nil {
    51  		return nil, []error{err}
    52  	}
    53  
    54  	requestData := &adapters.RequestData{
    55  		Method: "POST",
    56  		Uri:    a.endpoint,
    57  		Body:   requestJSON,
    58  		ImpIDs: openrtb_ext.GetImpIDs(request.Imp),
    59  	}
    60  
    61  	return []*adapters.RequestData{requestData}, nil
    62  }
    63  
    64  func (a *adapter) MakeBids(request *openrtb2.BidRequest, requestData *adapters.RequestData, responseData *adapters.ResponseData) (*adapters.BidderResponse, []error) {
    65  	if adapters.IsResponseStatusCodeNoContent(responseData) {
    66  		return nil, nil
    67  	}
    68  
    69  	if err := adapters.CheckResponseStatusCodeForErrors(responseData); err != nil {
    70  		return nil, []error{err}
    71  	}
    72  
    73  	var response serverResponse
    74  	if err := json.Unmarshal(responseData.Body, &response); err != nil {
    75  		return nil, []error{err}
    76  	}
    77  
    78  	bidResponse := adapters.NewBidderResponseWithBidsCapacity(len(request.Imp))
    79  	for _, maBidResp := range response.Responses {
    80  		b := &adapters.TypedBid{
    81  			Bid: &openrtb2.Bid{
    82  				ID:      maBidResp.RequestID,
    83  				ImpID:   maBidResp.RequestID,
    84  				Price:   float64(maBidResp.CPM),
    85  				AdM:     maBidResp.AdTag,
    86  				W:       int64(maBidResp.Width),
    87  				H:       int64(maBidResp.Height),
    88  				CrID:    maBidResp.PlacementId,
    89  				DealID:  maBidResp.Deal,
    90  				ADomain: maBidResp.Meta.AdDomain,
    91  			},
    92  			BidType: openrtb_ext.BidType(maBidResp.MediaType),
    93  		}
    94  		bidResponse.Bids = append(bidResponse.Bids, b)
    95  		if maBidResp.Currency != "" {
    96  			bidResponse.Currency = maBidResp.Currency
    97  		}
    98  	}
    99  	return bidResponse, nil
   100  }