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

     1  package oms
     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 OMS 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  
    45  	if responseData.StatusCode == http.StatusNoContent {
    46  		return nil, nil
    47  	}
    48  
    49  	if responseData.StatusCode == http.StatusBadRequest {
    50  		err := &errortypes.BadInput{
    51  			Message: "Unexpected status code: 400. Bad request from publisher. Run with request.debug = 1 for more info.",
    52  		}
    53  		return nil, []error{err}
    54  	}
    55  
    56  	if responseData.StatusCode != http.StatusOK {
    57  		err := &errortypes.BadServerResponse{
    58  			Message: fmt.Sprintf("Unexpected status code: %d. Run with request.debug = 1 for more info.", responseData.StatusCode),
    59  		}
    60  		return nil, []error{err}
    61  	}
    62  
    63  	var response openrtb2.BidResponse
    64  	if err := json.Unmarshal(responseData.Body, &response); err != nil {
    65  		return nil, []error{err}
    66  	}
    67  
    68  	bidResponse := adapters.NewBidderResponseWithBidsCapacity(len(request.Imp))
    69  	if len(response.Cur) == 0 {
    70  		bidResponse.Currency = response.Cur
    71  	}
    72  	for _, seatBid := range response.SeatBid {
    73  		for i := range seatBid.Bid {
    74  			bidResponse.Bids = append(bidResponse.Bids, &adapters.TypedBid{
    75  				Bid:     &seatBid.Bid[i],
    76  				BidType: openrtb_ext.BidTypeBanner,
    77  			})
    78  		}
    79  	}
    80  
    81  	return bidResponse, nil
    82  }