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

     1  package ccx
     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  type adapter struct {
    17  	endpoint string
    18  }
    19  
    20  // Builder builds a new instance of the Foo adapter for the given bidder with the given config.
    21  func Builder(bidderName openrtb_ext.BidderName, config config.Adapter, server config.Server) (adapters.Bidder, error) {
    22  	bidder := &adapter{
    23  		endpoint: config.Endpoint,
    24  	}
    25  	return bidder, nil
    26  }
    27  
    28  func (a *adapter) MakeRequests(request *openrtb2.BidRequest, requestInfo *adapters.ExtraRequestInfo) ([]*adapters.RequestData, []error) {
    29  
    30  	requestJSON, err := json.Marshal(request)
    31  	if err != nil {
    32  		return nil, []error{err}
    33  	}
    34  
    35  	requestData := &adapters.RequestData{
    36  		Method: "POST",
    37  		Uri:    a.endpoint,
    38  		Body:   requestJSON,
    39  		ImpIDs: openrtb_ext.GetImpIDs(request.Imp),
    40  	}
    41  
    42  	return []*adapters.RequestData{requestData}, nil
    43  }
    44  
    45  func (a *adapter) MakeBids(request *openrtb2.BidRequest, requestData *adapters.RequestData, responseData *adapters.ResponseData) (*adapters.BidderResponse, []error) {
    46  	if responseData.StatusCode == http.StatusNoContent {
    47  		return nil, nil
    48  	}
    49  
    50  	if responseData.StatusCode == http.StatusBadRequest {
    51  		err := &errortypes.BadInput{
    52  			Message: "Unexpected status code: 400. Bad request from publisher. Run with request.debug = 1 for more info.",
    53  		}
    54  		return nil, []error{err}
    55  	}
    56  
    57  	if responseData.StatusCode != http.StatusOK {
    58  		err := &errortypes.BadServerResponse{
    59  			Message: fmt.Sprintf("Unexpected status code: %d. Run with request.debug = 1 for more info.", responseData.StatusCode),
    60  		}
    61  		return nil, []error{err}
    62  	}
    63  
    64  	var response openrtb2.BidResponse
    65  	if err := json.Unmarshal(responseData.Body, &response); err != nil {
    66  		return nil, []error{err}
    67  	}
    68  
    69  	bidResponse := adapters.NewBidderResponseWithBidsCapacity(len(request.Imp))
    70  	bidResponse.Currency = response.Cur
    71  	for _, seatBid := range response.SeatBid {
    72  		for i := range seatBid.Bid {
    73  			b := &adapters.TypedBid{
    74  				Bid:     &seatBid.Bid[i],
    75  				BidType: openrtb_ext.BidTypeBanner,
    76  			}
    77  			bidResponse.Bids = append(bidResponse.Bids, b)
    78  		}
    79  	}
    80  	return bidResponse, nil
    81  }