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

     1  package zeta_global_ssp
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"net/http"
     7  
     8  	"github.com/prebid/openrtb/v20/openrtb2"
     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  
    15  type adapter struct {
    16  	endpoint string
    17  }
    18  
    19  func Builder(bidderName openrtb_ext.BidderName, config config.Adapter, server config.Server) (adapters.Bidder, error) {
    20  	bidder := &adapter{
    21  		endpoint: config.Endpoint,
    22  	}
    23  	return bidder, nil
    24  }
    25  
    26  func (a *adapter) MakeRequests(request *openrtb2.BidRequest, requestInfo *adapters.ExtraRequestInfo) ([]*adapters.RequestData, []error) {
    27  	requestJson, err := json.Marshal(request)
    28  	if err != nil {
    29  		return nil, []error{err}
    30  	}
    31  
    32  	headers := http.Header{}
    33  	headers.Add("Content-Type", "application/json;charset=utf-8")
    34  	headers.Add("Accept", "application/json")
    35  
    36  	requestData := &adapters.RequestData{
    37  		Method:  "POST",
    38  		Uri:     a.endpoint,
    39  		Body:    requestJson,
    40  		Headers: headers,
    41  		ImpIDs:  openrtb_ext.GetImpIDs(request.Imp),
    42  	}
    43  
    44  	return []*adapters.RequestData{requestData}, nil
    45  }
    46  
    47  func (a *adapter) MakeBids(request *openrtb2.BidRequest, requestData *adapters.RequestData, responseData *adapters.ResponseData) (*adapters.BidderResponse, []error) {
    48  	if adapters.IsResponseStatusCodeNoContent(responseData) {
    49  		return nil, nil
    50  	}
    51  
    52  	if err := adapters.CheckResponseStatusCodeForErrors(responseData); err != nil {
    53  		return nil, []error{err}
    54  	}
    55  
    56  	var response openrtb2.BidResponse
    57  	if err := json.Unmarshal(responseData.Body, &response); err != nil {
    58  		return nil, []error{err}
    59  	}
    60  
    61  	bidResponse := adapters.NewBidderResponseWithBidsCapacity(len(request.Imp))
    62  	bidResponse.Currency = response.Cur
    63  	var errors []error
    64  	for _, seatBid := range response.SeatBid {
    65  		for i := range seatBid.Bid {
    66  			bidType, err := getMediaTypeForBid(seatBid.Bid[i])
    67  			if err != nil {
    68  				errors = append(errors, err)
    69  				continue
    70  			}
    71  			b := &adapters.TypedBid{
    72  				Bid:     &seatBid.Bid[i],
    73  				BidType: bidType,
    74  			}
    75  			bidResponse.Bids = append(bidResponse.Bids, b)
    76  		}
    77  	}
    78  	return bidResponse, errors
    79  }
    80  
    81  func getMediaTypeForBid(bid openrtb2.Bid) (openrtb_ext.BidType, error) {
    82  	if bid.Ext != nil {
    83  		var bidExt openrtb_ext.ExtBid
    84  		err := json.Unmarshal(bid.Ext, &bidExt)
    85  		if err == nil && bidExt.Prebid != nil {
    86  			return openrtb_ext.ParseBidType(string(bidExt.Prebid.Type))
    87  		}
    88  	}
    89  
    90  	return "", &errortypes.BadServerResponse{
    91  		Message: fmt.Sprintf("Failed to parse impression \"%s\" mediatype", bid.ImpID),
    92  	}
    93  }