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

     1  package vox
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  
     7  	"github.com/prebid/openrtb/v20/openrtb2"
     8  	"github.com/prebid/prebid-server/v2/adapters"
     9  	"github.com/prebid/prebid-server/v2/config"
    10  	"github.com/prebid/prebid-server/v2/openrtb_ext"
    11  )
    12  
    13  type adapter struct {
    14  	endpoint string
    15  }
    16  
    17  func Builder(bidderName openrtb_ext.BidderName, config config.Adapter, server config.Server) (adapters.Bidder, error) {
    18  	bidder := &adapter{
    19  		endpoint: config.Endpoint,
    20  	}
    21  	return bidder, nil
    22  }
    23  
    24  func (a *adapter) MakeRequests(request *openrtb2.BidRequest, requestInfo *adapters.ExtraRequestInfo) ([]*adapters.RequestData, []error) {
    25  	requestJSON, err := json.Marshal(request)
    26  	if err != nil {
    27  		return nil, []error{err}
    28  	}
    29  
    30  	requestData := &adapters.RequestData{
    31  		Method: "POST",
    32  		Uri:    a.endpoint,
    33  		Body:   requestJSON,
    34  		ImpIDs: openrtb_ext.GetImpIDs(request.Imp),
    35  	}
    36  
    37  	return []*adapters.RequestData{requestData}, nil
    38  }
    39  
    40  func (a *adapter) MakeBids(request *openrtb2.BidRequest, requestData *adapters.RequestData, responseData *adapters.ResponseData) (*adapters.BidderResponse, []error) {
    41  	if adapters.IsResponseStatusCodeNoContent(responseData) {
    42  		return nil, nil
    43  	}
    44  
    45  	if err := adapters.CheckResponseStatusCodeForErrors(responseData); err != nil {
    46  		return nil, []error{err}
    47  	}
    48  
    49  	var response openrtb2.BidResponse
    50  	if err := json.Unmarshal(responseData.Body, &response); err != nil {
    51  		return nil, []error{err}
    52  	}
    53  
    54  	bidResponse := adapters.NewBidderResponseWithBidsCapacity(len(request.Imp))
    55  	bidResponse.Currency = response.Cur
    56  
    57  	for _, seatBid := range response.SeatBid {
    58  		for i, bid := range seatBid.Bid {
    59  			bidType, err := getMediaTypeForBid(bid)
    60  			if err != nil {
    61  				return nil, []error{err}
    62  			}
    63  
    64  			b := &adapters.TypedBid{
    65  				Bid:     &seatBid.Bid[i],
    66  				BidType: bidType,
    67  			}
    68  			bidResponse.Bids = append(bidResponse.Bids, b)
    69  		}
    70  	}
    71  
    72  	return bidResponse, nil
    73  }
    74  
    75  func getMediaTypeForBid(bid openrtb2.Bid) (openrtb_ext.BidType, error) {
    76  	switch bid.MType {
    77  	case openrtb2.MarkupBanner:
    78  		return openrtb_ext.BidTypeBanner, nil
    79  	case openrtb2.MarkupVideo:
    80  		return openrtb_ext.BidTypeVideo, nil
    81  	case openrtb2.MarkupAudio:
    82  		return openrtb_ext.BidTypeAudio, nil
    83  	case openrtb2.MarkupNative:
    84  		return openrtb_ext.BidTypeNative, nil
    85  	default:
    86  		return "", fmt.Errorf("Unable to fetch mediaType in multi-format: %s", bid.ImpID)
    87  	}
    88  }