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

     1  package adpone
     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  // Builder builds a new instance of the Adpone adapter for the given bidder with the given config.
    17  func Builder(bidderName openrtb_ext.BidderName, config config.Adapter, server config.Server) (adapters.Bidder, error) {
    18  	bidder := &adponeAdapter{
    19  		endpoint: config.Endpoint,
    20  	}
    21  	return bidder, nil
    22  }
    23  
    24  type adponeAdapter struct {
    25  	endpoint string
    26  }
    27  
    28  func (adapter *adponeAdapter) MakeRequests(
    29  	openRTBRequest *openrtb2.BidRequest,
    30  	reqInfo *adapters.ExtraRequestInfo,
    31  ) (
    32  	requestsToBidder []*adapters.RequestData,
    33  	errs []error,
    34  ) {
    35  	if len(openRTBRequest.Imp) > 0 {
    36  		var imp = &openRTBRequest.Imp[0]
    37  		var bidderExt adapters.ExtImpBidder
    38  		if err := json.Unmarshal(imp.Ext, &bidderExt); err != nil {
    39  			errs = append(errs, newBadInputError(err.Error()))
    40  		}
    41  		var ttxExt openrtb_ext.ExtAdpone
    42  		if err := json.Unmarshal(bidderExt.Bidder, &ttxExt); err != nil {
    43  			errs = append(errs, newBadInputError(err.Error()))
    44  		}
    45  	}
    46  
    47  	if len(openRTBRequest.Imp) == 0 {
    48  		errs = append(errs, newBadInputError("No impression in the bid request"))
    49  		return nil, errs
    50  	}
    51  
    52  	openRTBRequestJSON, err := json.Marshal(openRTBRequest)
    53  	if err != nil {
    54  		errs = append(errs, err)
    55  		return nil, errs
    56  	}
    57  
    58  	headers := http.Header{}
    59  	headers.Add("Content-Type", "application/json;charset=utf-8")
    60  	headers.Add("Accept", "application/json")
    61  	headers.Add("x-openrtb-version", "2.5")
    62  
    63  	requestToBidder := &adapters.RequestData{
    64  		Method:  "POST",
    65  		Uri:     adapter.endpoint,
    66  		Body:    openRTBRequestJSON,
    67  		Headers: headers,
    68  		ImpIDs:  openrtb_ext.GetImpIDs(openRTBRequest.Imp),
    69  	}
    70  	requestsToBidder = append(requestsToBidder, requestToBidder)
    71  
    72  	return requestsToBidder, errs
    73  }
    74  
    75  const unexpectedStatusCodeFormat = "" +
    76  	"Unexpected status code: %d. Run with request.debug = 1 for more info"
    77  
    78  func (adapter *adponeAdapter) MakeBids(
    79  	openRTBRequest *openrtb2.BidRequest,
    80  	requestToBidder *adapters.RequestData,
    81  	bidderRawResponse *adapters.ResponseData,
    82  ) (
    83  	bidderResponse *adapters.BidderResponse,
    84  	errs []error,
    85  ) {
    86  	switch bidderRawResponse.StatusCode {
    87  	case http.StatusOK:
    88  		break
    89  	case http.StatusNoContent:
    90  		return nil, nil
    91  	case http.StatusBadRequest:
    92  		err := &errortypes.BadInput{
    93  			Message: fmt.Sprintf(unexpectedStatusCodeFormat, bidderRawResponse.StatusCode),
    94  		}
    95  		return nil, []error{err}
    96  	default:
    97  		err := &errortypes.BadServerResponse{
    98  			Message: fmt.Sprintf(unexpectedStatusCodeFormat, bidderRawResponse.StatusCode),
    99  		}
   100  		return nil, []error{err}
   101  	}
   102  
   103  	var openRTBBidderResponse openrtb2.BidResponse
   104  	if err := json.Unmarshal(bidderRawResponse.Body, &openRTBBidderResponse); err != nil {
   105  		return nil, []error{err}
   106  	}
   107  
   108  	bidsCapacity := len(openRTBBidderResponse.SeatBid[0].Bid)
   109  	bidderResponse = adapters.NewBidderResponseWithBidsCapacity(bidsCapacity)
   110  	var typedBid *adapters.TypedBid
   111  	for _, seatBid := range openRTBBidderResponse.SeatBid {
   112  		for _, bid := range seatBid.Bid {
   113  			bid := bid
   114  			typedBid = &adapters.TypedBid{Bid: &bid, BidType: "banner"}
   115  			bidderResponse.Bids = append(bidderResponse.Bids, typedBid)
   116  		}
   117  	}
   118  
   119  	return bidderResponse, nil
   120  
   121  }
   122  
   123  func newBadInputError(message string) error {
   124  	return &errortypes.BadInput{
   125  		Message: message,
   126  	}
   127  }