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

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