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

     1  package adxcg
     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  // Builder builds a new instance of the Adxcg adapter for the given bidder with the given config.
    16  func Builder(bidderName openrtb_ext.BidderName, config config.Adapter, server config.Server) (adapters.Bidder, error) {
    17  	bidder := &adapter{
    18  		endpoint: config.Endpoint,
    19  	}
    20  	return bidder, nil
    21  }
    22  
    23  type adapter struct {
    24  	endpoint string
    25  }
    26  
    27  // MakeRequests prepares the HTTP requests which should be made to fetch bids.
    28  func (adapter *adapter) MakeRequests(
    29  	openRTBRequest *openrtb2.BidRequest,
    30  	reqInfo *adapters.ExtraRequestInfo,
    31  ) (
    32  	requestsToBidder []*adapters.RequestData,
    33  	errs []error,
    34  ) {
    35  	openRTBRequestJSON, err := json.Marshal(openRTBRequest)
    36  	if err != nil {
    37  		errs = append(errs, err)
    38  		return nil, errs
    39  	}
    40  
    41  	headers := http.Header{}
    42  	headers.Add("Content-Type", "application/json;charset=utf-8")
    43  	requestToBidder := &adapters.RequestData{
    44  		Method:  "POST",
    45  		Uri:     adapter.endpoint,
    46  		Body:    openRTBRequestJSON,
    47  		Headers: headers,
    48  		ImpIDs:  openrtb_ext.GetImpIDs(openRTBRequest.Imp),
    49  	}
    50  	requestsToBidder = append(requestsToBidder, requestToBidder)
    51  
    52  	return requestsToBidder, errs
    53  }
    54  
    55  const unexpectedStatusCodeFormat = "Unexpected status code: %d. Run with request.debug = 1 for more info"
    56  
    57  // MakeBids unpacks the server's response into Bids.
    58  func (adapter *adapter) MakeBids(
    59  	openRTBRequest *openrtb2.BidRequest,
    60  	requestToBidder *adapters.RequestData,
    61  	bidderRawResponse *adapters.ResponseData,
    62  ) (
    63  	bidderResponse *adapters.BidderResponse,
    64  	errs []error,
    65  ) {
    66  	switch bidderRawResponse.StatusCode {
    67  	case http.StatusOK:
    68  		break
    69  	case http.StatusNoContent:
    70  		return nil, nil
    71  	case http.StatusBadRequest:
    72  		err := &errortypes.BadInput{
    73  			Message: fmt.Sprintf(unexpectedStatusCodeFormat, bidderRawResponse.StatusCode),
    74  		}
    75  		return nil, []error{err}
    76  	default:
    77  		err := &errortypes.BadServerResponse{
    78  			Message: fmt.Sprintf(unexpectedStatusCodeFormat, bidderRawResponse.StatusCode),
    79  		}
    80  		return nil, []error{err}
    81  	}
    82  
    83  	var openRTBBidderResponse openrtb2.BidResponse
    84  	if err := json.Unmarshal(bidderRawResponse.Body, &openRTBBidderResponse); err != nil {
    85  		return nil, []error{err}
    86  	}
    87  
    88  	bidderResponse = adapters.NewBidderResponseWithBidsCapacity(len(openRTBRequest.Imp))
    89  	bidderResponse.Currency = openRTBBidderResponse.Cur
    90  	var typedBid *adapters.TypedBid
    91  	for _, seatBid := range openRTBBidderResponse.SeatBid {
    92  		for _, bid := range seatBid.Bid {
    93  			activeBid := bid
    94  			bidType, err := getReturnTypeFromMtypeForImp(activeBid.MType)
    95  			if err != nil {
    96  				errs = append(errs, err)
    97  				continue
    98  			}
    99  
   100  			typedBid = &adapters.TypedBid{Bid: &activeBid, BidType: bidType}
   101  			bidderResponse.Bids = append(bidderResponse.Bids, typedBid)
   102  		}
   103  	}
   104  
   105  	return bidderResponse, nil
   106  
   107  }
   108  
   109  func getReturnTypeFromMtypeForImp(mType openrtb2.MarkupType) (openrtb_ext.BidType, error) {
   110  	switch mType {
   111  	case openrtb2.MarkupBanner:
   112  		return openrtb_ext.BidTypeBanner, nil
   113  	case openrtb2.MarkupVideo:
   114  		return openrtb_ext.BidTypeVideo, nil
   115  	case openrtb2.MarkupNative:
   116  		return openrtb_ext.BidTypeNative, nil
   117  	case openrtb2.MarkupAudio:
   118  		return openrtb_ext.BidTypeAudio, nil
   119  	default:
   120  		return "", &errortypes.BadServerResponse{Message: "Unsupported return type"}
   121  	}
   122  }