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

     1  package visiblemeasures
     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 adapter struct {
    16  	endpoint string
    17  }
    18  
    19  type reqBodyExt struct {
    20  	VisibleMeasuresBidderExt reqBodyExtBidder `json:"bidder"`
    21  }
    22  
    23  type reqBodyExtBidder struct {
    24  	Type        string `json:"type"`
    25  	PlacementID string `json:"placementId,omitempty"`
    26  	EndpointID  string `json:"endpointId,omitempty"`
    27  }
    28  
    29  func Builder(bidderName openrtb_ext.BidderName, config config.Adapter, server config.Server) (adapters.Bidder, error) {
    30  	bidder := &adapter{
    31  		endpoint: config.Endpoint,
    32  	}
    33  	return bidder, nil
    34  }
    35  
    36  func (a *adapter) MakeRequests(request *openrtb2.BidRequest, reqInfo *adapters.ExtraRequestInfo) ([]*adapters.RequestData, []error) {
    37  	var err error
    38  	var adapterRequests []*adapters.RequestData
    39  
    40  	reqCopy := *request
    41  	for _, imp := range request.Imp {
    42  		reqCopy.Imp = []openrtb2.Imp{imp}
    43  
    44  		var bidderExt adapters.ExtImpBidder
    45  		var visiblemeasuresExt openrtb_ext.ImpExtVisibleMeasures
    46  
    47  		if err = json.Unmarshal(reqCopy.Imp[0].Ext, &bidderExt); err != nil {
    48  			return nil, []error{err}
    49  		}
    50  		if err = json.Unmarshal(bidderExt.Bidder, &visiblemeasuresExt); err != nil {
    51  			return nil, []error{err}
    52  		}
    53  
    54  		temp := reqBodyExt{VisibleMeasuresBidderExt: reqBodyExtBidder{}}
    55  
    56  		if visiblemeasuresExt.PlacementID != "" {
    57  			temp.VisibleMeasuresBidderExt.PlacementID = visiblemeasuresExt.PlacementID
    58  			temp.VisibleMeasuresBidderExt.Type = "publisher"
    59  		} else if visiblemeasuresExt.EndpointID != "" {
    60  			temp.VisibleMeasuresBidderExt.EndpointID = visiblemeasuresExt.EndpointID
    61  			temp.VisibleMeasuresBidderExt.Type = "network"
    62  		}
    63  
    64  		finalyImpExt, err := json.Marshal(temp)
    65  		if err != nil {
    66  			return nil, []error{err}
    67  		}
    68  
    69  		reqCopy.Imp[0].Ext = finalyImpExt
    70  
    71  		adapterReq, err := a.makeRequest(&reqCopy)
    72  		if err != nil {
    73  			return nil, []error{err}
    74  		}
    75  
    76  		if adapterReq != nil {
    77  			adapterRequests = append(adapterRequests, adapterReq)
    78  		}
    79  	}
    80  	return adapterRequests, nil
    81  }
    82  
    83  func (a *adapter) makeRequest(request *openrtb2.BidRequest) (*adapters.RequestData, error) {
    84  	reqJSON, err := json.Marshal(request)
    85  	if err != nil {
    86  		return nil, err
    87  	}
    88  
    89  	headers := http.Header{}
    90  	headers.Add("Content-Type", "application/json;charset=utf-8")
    91  	headers.Add("Accept", "application/json")
    92  	return &adapters.RequestData{
    93  		Method:  "POST",
    94  		Uri:     a.endpoint,
    95  		Body:    reqJSON,
    96  		Headers: headers,
    97  		ImpIDs:  openrtb_ext.GetImpIDs(request.Imp),
    98  	}, nil
    99  }
   100  
   101  func (a *adapter) MakeBids(request *openrtb2.BidRequest, requestData *adapters.RequestData, responseData *adapters.ResponseData) (*adapters.BidderResponse, []error) {
   102  	if responseData.StatusCode == http.StatusNoContent {
   103  		return nil, nil
   104  	}
   105  
   106  	if responseData.StatusCode != http.StatusOK {
   107  		err := &errortypes.BadServerResponse{
   108  			Message: fmt.Sprintf("Unexpected status code: %d. Run with request.debug = 1 for more info.", responseData.StatusCode),
   109  		}
   110  		return nil, []error{err}
   111  	}
   112  
   113  	var response openrtb2.BidResponse
   114  	if err := json.Unmarshal(responseData.Body, &response); err != nil {
   115  		return nil, []error{err}
   116  	}
   117  
   118  	bidResponse := adapters.NewBidderResponseWithBidsCapacity(len(request.Imp))
   119  	bidResponse.Currency = response.Cur
   120  
   121  	impsMappedByID := make(map[string]openrtb2.Imp, len(request.Imp))
   122  	for i, imp := range request.Imp {
   123  		impsMappedByID[request.Imp[i].ID] = imp
   124  	}
   125  
   126  	for _, seatBid := range response.SeatBid {
   127  		for i := range seatBid.Bid {
   128  			bidType, err := getMediaTypeForImp(seatBid.Bid[i].ImpID, request.Imp, impsMappedByID)
   129  			if err != nil {
   130  				return nil, []error{err}
   131  			}
   132  
   133  			b := &adapters.TypedBid{
   134  				Bid:     &seatBid.Bid[i],
   135  				BidType: bidType,
   136  			}
   137  			bidResponse.Bids = append(bidResponse.Bids, b)
   138  		}
   139  	}
   140  	return bidResponse, nil
   141  }
   142  
   143  func getMediaTypeForImp(impID string, imps []openrtb2.Imp, impMap map[string]openrtb2.Imp) (openrtb_ext.BidType, error) {
   144  	if index, found := impMap[impID]; found {
   145  		if index.Banner != nil {
   146  			return openrtb_ext.BidTypeBanner, nil
   147  		}
   148  		if index.Video != nil {
   149  			return openrtb_ext.BidTypeVideo, nil
   150  		}
   151  		if index.Native != nil {
   152  			return openrtb_ext.BidTypeNative, nil
   153  		}
   154  	}
   155  
   156  	return "", &errortypes.BadInput{
   157  		Message: fmt.Sprintf("Failed to find impression \"%s\"", impID),
   158  	}
   159  }