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

     1  package aceex
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"net/http"
     7  	"text/template"
     8  
     9  	"github.com/prebid/openrtb/v20/openrtb2"
    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/macros"
    14  	"github.com/prebid/prebid-server/v2/openrtb_ext"
    15  )
    16  
    17  type adapter struct {
    18  	endpoint *template.Template
    19  }
    20  
    21  // Builder builds a new instance of the Aceex adapter for the given bidder with the given config.
    22  func Builder(bidderName openrtb_ext.BidderName, config config.Adapter, server config.Server) (adapters.Bidder, error) {
    23  	template, err := template.New("endpointTemplate").Parse(config.Endpoint)
    24  	if err != nil {
    25  		return nil, fmt.Errorf("unable to parse endpoint url template: %v", err)
    26  	}
    27  
    28  	bidder := &adapter{
    29  		endpoint: template,
    30  	}
    31  	return bidder, nil
    32  }
    33  
    34  func getHeaders(request *openrtb2.BidRequest) http.Header {
    35  	headers := http.Header{}
    36  	headers.Add("Content-Type", "application/json;charset=utf-8")
    37  	headers.Add("Accept", "application/json")
    38  	headers.Add("X-Openrtb-Version", "2.5")
    39  
    40  	if request.Device != nil {
    41  		if len(request.Device.UA) > 0 {
    42  			headers.Add("User-Agent", request.Device.UA)
    43  		}
    44  
    45  		if len(request.Device.IPv6) > 0 {
    46  			headers.Add("X-Forwarded-For", request.Device.IPv6)
    47  		}
    48  
    49  		if len(request.Device.IP) > 0 {
    50  			headers.Add("X-Forwarded-For", request.Device.IP)
    51  		}
    52  	}
    53  
    54  	return headers
    55  }
    56  
    57  func (a *adapter) MakeRequests(
    58  	openRTBRequest *openrtb2.BidRequest,
    59  	reqInfo *adapters.ExtraRequestInfo,
    60  ) (
    61  	requestsToBidder []*adapters.RequestData,
    62  	errs []error,
    63  ) {
    64  
    65  	var errors []error
    66  
    67  	aceexExt, err := a.getImpressionExt(&openRTBRequest.Imp[0])
    68  	if err != nil {
    69  		return nil, append(errors, err)
    70  	}
    71  
    72  	url, err := a.buildEndpointURL(aceexExt)
    73  	if err != nil {
    74  		return nil, []error{err}
    75  	}
    76  
    77  	reqJSON, err := json.Marshal(openRTBRequest)
    78  	if err != nil {
    79  		return nil, []error{err}
    80  	}
    81  
    82  	return []*adapters.RequestData{{
    83  		Method:  http.MethodPost,
    84  		Body:    reqJSON,
    85  		Uri:     url,
    86  		Headers: getHeaders(openRTBRequest),
    87  		ImpIDs:  openrtb_ext.GetImpIDs(openRTBRequest.Imp),
    88  	}}, nil
    89  }
    90  
    91  func (a *adapter) getImpressionExt(imp *openrtb2.Imp) (*openrtb_ext.ExtAceex, error) {
    92  	var bidderExt adapters.ExtImpBidder
    93  	if err := json.Unmarshal(imp.Ext, &bidderExt); err != nil {
    94  		return nil, &errortypes.BadInput{
    95  			Message: "ext.bidder not provided",
    96  		}
    97  	}
    98  	var aceexExt openrtb_ext.ExtAceex
    99  	if err := json.Unmarshal(bidderExt.Bidder, &aceexExt); err != nil {
   100  		return nil, &errortypes.BadInput{
   101  			Message: "ext.bidder not provided",
   102  		}
   103  	}
   104  	imp.Ext = nil
   105  	return &aceexExt, nil
   106  }
   107  
   108  func (a *adapter) buildEndpointURL(params *openrtb_ext.ExtAceex) (string, error) {
   109  	endpointParams := macros.EndpointTemplateParams{AccountID: params.AccountID}
   110  	return macros.ResolveMacros(a.endpoint, endpointParams)
   111  }
   112  
   113  func (a *adapter) checkResponseStatusCodes(response *adapters.ResponseData) error {
   114  	if response.StatusCode == http.StatusBadRequest {
   115  		return &errortypes.BadInput{
   116  			Message: fmt.Sprintf("Unexpected status code: [ %d ]", response.StatusCode),
   117  		}
   118  	}
   119  
   120  	if response.StatusCode == http.StatusServiceUnavailable {
   121  		return &errortypes.BadInput{
   122  			Message: fmt.Sprintf("Something went wrong, please contact your Account Manager. Status Code: [ %d ] ", response.StatusCode),
   123  		}
   124  	}
   125  
   126  	if response.StatusCode != http.StatusOK {
   127  		return &errortypes.BadInput{
   128  			Message: fmt.Sprintf("Unexpected status code: [ %d ]. Run with request.debug = 1 for more info", response.StatusCode),
   129  		}
   130  	}
   131  
   132  	return nil
   133  }
   134  
   135  func (a *adapter) MakeBids(
   136  	openRTBRequest *openrtb2.BidRequest,
   137  	requestToBidder *adapters.RequestData,
   138  	bidderRawResponse *adapters.ResponseData,
   139  ) (
   140  	bidderResponse *adapters.BidderResponse,
   141  	errs []error,
   142  ) {
   143  	if bidderRawResponse.StatusCode == http.StatusNoContent {
   144  		return nil, nil
   145  	}
   146  
   147  	httpStatusError := a.checkResponseStatusCodes(bidderRawResponse)
   148  	if httpStatusError != nil {
   149  		return nil, []error{httpStatusError}
   150  	}
   151  
   152  	responseBody := bidderRawResponse.Body
   153  	var bidResp openrtb2.BidResponse
   154  	if err := json.Unmarshal(responseBody, &bidResp); err != nil {
   155  		return nil, []error{&errortypes.BadServerResponse{
   156  			Message: "Bad Server Response",
   157  		}}
   158  	}
   159  
   160  	if len(bidResp.SeatBid) == 0 {
   161  		return nil, []error{&errortypes.BadServerResponse{
   162  			Message: "Empty SeatBid array",
   163  		}}
   164  	}
   165  
   166  	bidResponse := adapters.NewBidderResponseWithBidsCapacity(len(bidResp.SeatBid[0].Bid))
   167  	sb := bidResp.SeatBid[0]
   168  
   169  	for _, bid := range sb.Bid {
   170  		bidResponse.Bids = append(bidResponse.Bids, &adapters.TypedBid{
   171  			Bid:     &bid,
   172  			BidType: getMediaTypeForImp(bid.ImpID, openRTBRequest.Imp),
   173  		})
   174  	}
   175  	return bidResponse, nil
   176  }
   177  
   178  func getMediaTypeForImp(impId string, imps []openrtb2.Imp) openrtb_ext.BidType {
   179  	mediaType := openrtb_ext.BidTypeBanner
   180  	for _, imp := range imps {
   181  		if imp.ID == impId {
   182  			if imp.Video != nil {
   183  				mediaType = openrtb_ext.BidTypeVideo
   184  			} else if imp.Native != nil {
   185  				mediaType = openrtb_ext.BidTypeNative
   186  			}
   187  			return mediaType
   188  		}
   189  	}
   190  	return mediaType
   191  }