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

     1  package coinzilla
     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/config"
    10  	"github.com/prebid/prebid-server/v2/openrtb_ext"
    11  
    12  	"github.com/prebid/prebid-server/v2/adapters"
    13  	"github.com/prebid/prebid-server/v2/errortypes"
    14  )
    15  
    16  func Builder(bidderName openrtb_ext.BidderName, config config.Adapter, server config.Server) (adapters.Bidder, error) {
    17  	bidder := &adapter{
    18  		endpoint: config.Endpoint,
    19  	}
    20  	return bidder, nil
    21  }
    22  
    23  type adapter struct {
    24  	endpoint string
    25  }
    26  
    27  func (adapter *adapter) MakeRequests(openRTBRequest *openrtb2.BidRequest, reqInfo *adapters.ExtraRequestInfo) (requestsToBidder []*adapters.RequestData, errs []error) {
    28  	if len(openRTBRequest.Imp) == 0 {
    29  		return nil, []error{&errortypes.BadInput{
    30  			Message: "No impression in the bid request",
    31  		}}
    32  	}
    33  	openRTBRequestJSON, err := json.Marshal(openRTBRequest)
    34  	if err != nil {
    35  		errs = append(errs, err)
    36  		return nil, errs
    37  	}
    38  
    39  	headers := http.Header{}
    40  	headers.Add("Content-Type", "application/json;charset=utf-8")
    41  	headers.Add("Accept", "application/json")
    42  	headers.Add("x-openrtb-version", "2.5")
    43  
    44  	requestToBidder := &adapters.RequestData{
    45  		Method:  "POST",
    46  		Uri:     adapter.endpoint,
    47  		Body:    openRTBRequestJSON,
    48  		Headers: headers,
    49  		ImpIDs:  openrtb_ext.GetImpIDs(openRTBRequest.Imp),
    50  	}
    51  	requestsToBidder = append(requestsToBidder, requestToBidder)
    52  
    53  	return requestsToBidder, errs
    54  }
    55  
    56  func (adapter *adapter) MakeBids(openRTBRequest *openrtb2.BidRequest, requestToBidder *adapters.RequestData, bidderRawResponse *adapters.ResponseData) (bidderResponse *adapters.BidderResponse, errs []error) {
    57  	switch bidderRawResponse.StatusCode {
    58  	case http.StatusOK:
    59  		break
    60  	case http.StatusNoContent:
    61  		return nil, nil
    62  	default:
    63  		err := &errortypes.BadServerResponse{
    64  			Message: fmt.Sprintf("Unexpected code: %d. Run with request.debug = 1", bidderRawResponse.StatusCode),
    65  		}
    66  		return nil, []error{err}
    67  	}
    68  
    69  	var openRTBBidderResponse openrtb2.BidResponse
    70  	if err := json.Unmarshal(bidderRawResponse.Body, &openRTBBidderResponse); err != nil {
    71  		return nil, []error{err}
    72  	}
    73  
    74  	bidsCapacity := len(openRTBBidderResponse.SeatBid[0].Bid)
    75  	bidderResponse = adapters.NewBidderResponseWithBidsCapacity(bidsCapacity)
    76  	bidderResponse.Currency = openRTBBidderResponse.Cur
    77  	var typedBid *adapters.TypedBid
    78  	for _, seatBid := range openRTBBidderResponse.SeatBid {
    79  		for i := range seatBid.Bid {
    80  			typedBid = &adapters.TypedBid{Bid: &seatBid.Bid[i], BidType: "banner"}
    81  			bidderResponse.Bids = append(bidderResponse.Bids, typedBid)
    82  		}
    83  	}
    84  
    85  	return bidderResponse, nil
    86  }