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

     1  package automatad
     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 Automatad 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. Malformed request syntax received.",
    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. Something went wrong on the bidder's side.", 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  			bidResponse.Bids = append(bidResponse.Bids, &adapters.TypedBid{
    72  				Bid:     &seatBid.Bid[i],
    73  				BidType: openrtb_ext.BidTypeBanner,
    74  			})
    75  		}
    76  	}
    77  	return bidResponse, nil
    78  }