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

     1  package aja
     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 AJAAdapter struct {
    16  	endpoint string
    17  }
    18  
    19  func (a *AJAAdapter) MakeRequests(bidReq *openrtb2.BidRequest, extraInfo *adapters.ExtraRequestInfo) (adapterReqs []*adapters.RequestData, errs []error) {
    20  	// split imps by tagid
    21  	tagIDs := []string{}
    22  	impsByTagID := map[string][]openrtb2.Imp{}
    23  	for _, imp := range bidReq.Imp {
    24  		extAJA, err := parseExtAJA(imp)
    25  		if err != nil {
    26  			errs = append(errs, err)
    27  			continue
    28  		}
    29  		imp.TagID = extAJA.AdSpotID
    30  		imp.Ext = nil
    31  		if _, ok := impsByTagID[imp.TagID]; !ok {
    32  			tagIDs = append(tagIDs, imp.TagID)
    33  		}
    34  		impsByTagID[imp.TagID] = append(impsByTagID[imp.TagID], imp)
    35  	}
    36  
    37  	req := *bidReq
    38  	for _, tagID := range tagIDs {
    39  		req.Imp = impsByTagID[tagID]
    40  		body, err := json.Marshal(req)
    41  		if err != nil {
    42  			errs = append(errs, &errortypes.BadInput{
    43  				Message: fmt.Sprintf("Failed to unmarshal bidrequest ID: %s err: %s", bidReq.ID, err),
    44  			})
    45  			continue
    46  		}
    47  		adapterReqs = append(adapterReqs, &adapters.RequestData{
    48  			Method: "POST",
    49  			Uri:    a.endpoint,
    50  			Body:   body,
    51  			ImpIDs: openrtb_ext.GetImpIDs(req.Imp),
    52  		})
    53  	}
    54  
    55  	return
    56  }
    57  
    58  func parseExtAJA(imp openrtb2.Imp) (openrtb_ext.ExtImpAJA, error) {
    59  	var (
    60  		extImp adapters.ExtImpBidder
    61  		extAJA openrtb_ext.ExtImpAJA
    62  	)
    63  
    64  	if err := json.Unmarshal(imp.Ext, &extImp); err != nil {
    65  		return extAJA, &errortypes.BadInput{
    66  			Message: fmt.Sprintf("Failed to unmarshal ext impID: %s err: %s", imp.ID, err),
    67  		}
    68  	}
    69  
    70  	if err := json.Unmarshal(extImp.Bidder, &extAJA); err != nil {
    71  		return extAJA, &errortypes.BadInput{
    72  			Message: fmt.Sprintf("Failed to unmarshal ext.bidder impID: %s err: %s", imp.ID, err),
    73  		}
    74  	}
    75  
    76  	return extAJA, nil
    77  }
    78  
    79  func (a *AJAAdapter) MakeBids(bidReq *openrtb2.BidRequest, adapterReq *adapters.RequestData, adapterResp *adapters.ResponseData) (*adapters.BidderResponse, []error) {
    80  	if adapterResp.StatusCode != http.StatusOK {
    81  		if adapterResp.StatusCode == http.StatusNoContent {
    82  			return nil, nil
    83  		}
    84  		if adapterResp.StatusCode == http.StatusBadRequest {
    85  			return nil, []error{&errortypes.BadInput{
    86  				Message: fmt.Sprintf("Unexpected status code: %d", adapterResp.StatusCode),
    87  			}}
    88  		}
    89  		return nil, []error{&errortypes.BadServerResponse{
    90  			Message: fmt.Sprintf("Unexpected status code: %d", adapterResp.StatusCode),
    91  		}}
    92  	}
    93  
    94  	var bidResp openrtb2.BidResponse
    95  	if err := json.Unmarshal(adapterResp.Body, &bidResp); err != nil {
    96  		return nil, []error{&errortypes.BadServerResponse{
    97  			Message: fmt.Sprintf("Failed to unmarshal bid response: %s", err.Error()),
    98  		}}
    99  	}
   100  
   101  	bidderResp := adapters.NewBidderResponseWithBidsCapacity(len(bidReq.Imp))
   102  	var errors []error
   103  
   104  	for _, seatbid := range bidResp.SeatBid {
   105  		for _, bid := range seatbid.Bid {
   106  			for _, imp := range bidReq.Imp {
   107  				if imp.ID == bid.ImpID {
   108  					var bidType openrtb_ext.BidType
   109  					if imp.Banner != nil {
   110  						bidType = openrtb_ext.BidTypeBanner
   111  					} else if imp.Video != nil {
   112  						bidType = openrtb_ext.BidTypeVideo
   113  					} else {
   114  						errors = append(errors, &errortypes.BadServerResponse{
   115  							Message: fmt.Sprintf("Response received for unexpected type of bid bidID: %s", bid.ID),
   116  						})
   117  						continue
   118  					}
   119  					bidderResp.Bids = append(bidderResp.Bids, &adapters.TypedBid{
   120  						Bid:     &bid,
   121  						BidType: bidType,
   122  					})
   123  					break
   124  				}
   125  			}
   126  		}
   127  	}
   128  	if bidResp.Cur != "" {
   129  		bidderResp.Currency = bidResp.Cur
   130  	}
   131  	return bidderResp, errors
   132  }
   133  
   134  // Builder builds a new instance of the AJA adapter for the given bidder with the given config.
   135  func Builder(bidderName openrtb_ext.BidderName, config config.Adapter, server config.Server) (adapters.Bidder, error) {
   136  	bidder := &AJAAdapter{
   137  		endpoint: config.Endpoint,
   138  	}
   139  	return bidder, nil
   140  }