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

     1  package infytv
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"net/http"
     7  
     8  	"github.com/prebid/openrtb/v20/openrtb2"
     9  
    10  	"github.com/prebid/prebid-server/v2/adapters"
    11  	"github.com/prebid/prebid-server/v2/config"
    12  	"github.com/prebid/prebid-server/v2/errortypes"
    13  	"github.com/prebid/prebid-server/v2/openrtb_ext"
    14  )
    15  
    16  type adapter struct {
    17  	endpoint string
    18  }
    19  
    20  // Builder builds a new instance of the InfyTV 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  	requestJSON, err := json.Marshal(request)
    30  	if err != nil {
    31  		return nil, []error{err}
    32  	}
    33  
    34  	requestData := &adapters.RequestData{
    35  		Method: "POST",
    36  		Uri:    a.endpoint,
    37  		Body:   requestJSON,
    38  		ImpIDs: openrtb_ext.GetImpIDs(request.Imp),
    39  	}
    40  
    41  	return []*adapters.RequestData{requestData}, nil
    42  }
    43  
    44  func (a *adapter) MakeBids(request *openrtb2.BidRequest, requestData *adapters.RequestData, responseData *adapters.ResponseData) (*adapters.BidderResponse, []error) {
    45  
    46  	if responseData.StatusCode == http.StatusNoContent {
    47  		return nil, nil
    48  	}
    49  
    50  	if responseData.StatusCode == http.StatusBadRequest {
    51  		return nil, []error{&errortypes.BadInput{
    52  			Message: fmt.Sprintf("Bad Request. %s", string(responseData.Body)),
    53  		}}
    54  	}
    55  
    56  	if responseData.StatusCode == http.StatusServiceUnavailable {
    57  		return nil, nil
    58  	}
    59  
    60  	if responseData.StatusCode != http.StatusOK {
    61  		return nil, []error{&errortypes.BadServerResponse{
    62  			Message: fmt.Sprintf("Something went wrong, please contact your Account Manager. Status Code: [ %d ] ", responseData.StatusCode),
    63  		}}
    64  	}
    65  
    66  	var response openrtb2.BidResponse
    67  	if err := json.Unmarshal(responseData.Body, &response); err != nil {
    68  		return nil, []error{&errortypes.BadServerResponse{
    69  			Message: fmt.Sprintf("Bad response, %s", err),
    70  		}}
    71  	}
    72  
    73  	if len(response.SeatBid) == 0 {
    74  		return nil, []error{&errortypes.BadServerResponse{
    75  			Message: "Empty seatbid",
    76  		}}
    77  	}
    78  
    79  	bidResponse := adapters.NewBidderResponseWithBidsCapacity(len(request.Imp))
    80  	bidResponse.Currency = response.Cur
    81  	for _, seatBid := range response.SeatBid {
    82  		for i := range seatBid.Bid {
    83  			b := &adapters.TypedBid{
    84  				Bid:     &seatBid.Bid[i],
    85  				BidType: openrtb_ext.BidTypeVideo,
    86  			}
    87  			bidResponse.Bids = append(bidResponse.Bids, b)
    88  		}
    89  	}
    90  	return bidResponse, nil
    91  }