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

     1  package vrtcal
     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 VrtcalAdapter struct {
    16  	endpoint string
    17  }
    18  
    19  func (a *VrtcalAdapter) MakeRequests(request *openrtb2.BidRequest, reqInfo *adapters.ExtraRequestInfo) ([]*adapters.RequestData, []error) {
    20  	var errs []error
    21  	var adapterRequests []*adapters.RequestData
    22  
    23  	reqJSON, err := json.Marshal(request)
    24  	if err != nil {
    25  		errs = append(errs, err)
    26  		return nil, errs
    27  	}
    28  
    29  	headers := http.Header{}
    30  	headers.Add("Content-Type", "application/json;charset=utf-8")
    31  
    32  	reqData := adapters.RequestData{
    33  		Method:  "POST",
    34  		Uri:     a.endpoint,
    35  		Body:    reqJSON,
    36  		Headers: headers,
    37  		ImpIDs:  openrtb_ext.GetImpIDs(request.Imp),
    38  	}
    39  
    40  	adapterRequests = append(adapterRequests, &reqData)
    41  
    42  	return adapterRequests, errs
    43  }
    44  
    45  // MakeBids make the bids for the bid response.
    46  func (a *VrtcalAdapter) MakeBids(internalRequest *openrtb2.BidRequest, externalRequest *adapters.RequestData, response *adapters.ResponseData) (*adapters.BidderResponse, []error) {
    47  
    48  	if response.StatusCode == http.StatusNoContent {
    49  		return nil, nil
    50  	}
    51  
    52  	if response.StatusCode == http.StatusBadRequest {
    53  		return nil, []error{&errortypes.BadInput{
    54  			Message: fmt.Sprintf("Unexpected status code: %d. Run with request.debug = 1 for more info", response.StatusCode),
    55  		}}
    56  	}
    57  
    58  	if response.StatusCode != http.StatusOK {
    59  		return nil, []error{&errortypes.BadServerResponse{
    60  			Message: fmt.Sprintf("Unexpected status code: %d. Run with request.debug = 1 for more info", response.StatusCode),
    61  		}}
    62  	}
    63  
    64  	var bidResp openrtb2.BidResponse
    65  
    66  	if err := json.Unmarshal(response.Body, &bidResp); err != nil {
    67  		return nil, []error{err}
    68  	}
    69  
    70  	bidResponse := adapters.NewBidderResponseWithBidsCapacity(1)
    71  
    72  	var errs []error
    73  	for _, sb := range bidResp.SeatBid {
    74  		for i := range sb.Bid {
    75  			bidType, err := getReturnTypeForImp(sb.Bid[i].MType)
    76  			if err == nil {
    77  				bidResponse.Bids = append(bidResponse.Bids, &adapters.TypedBid{
    78  					Bid:     &sb.Bid[i],
    79  					BidType: bidType,
    80  				})
    81  			} else {
    82  				errs = append(errs, err)
    83  			}
    84  		}
    85  	}
    86  	return bidResponse, errs
    87  
    88  }
    89  
    90  // Builder builds a new instance of the Vrtcal adapter for the given bidder with the given config.
    91  func Builder(bidderName openrtb_ext.BidderName, config config.Adapter, server config.Server) (adapters.Bidder, error) {
    92  	bidder := &VrtcalAdapter{
    93  		endpoint: config.Endpoint,
    94  	}
    95  	return bidder, nil
    96  }
    97  
    98  func getReturnTypeForImp(mType openrtb2.MarkupType) (openrtb_ext.BidType, error) {
    99  	if mType == openrtb2.MarkupBanner {
   100  		return openrtb_ext.BidTypeBanner, nil
   101  	} else if mType == openrtb2.MarkupVideo {
   102  		return openrtb_ext.BidTypeVideo, nil
   103  	} else if mType == openrtb2.MarkupNative {
   104  		return openrtb_ext.BidTypeNative, nil
   105  	} else {
   106  		return "", &errortypes.BadServerResponse{
   107  			Message: "Unsupported return type"}
   108  	}
   109  }