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

     1  package kiviads
     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  	KiviadsBidderExt 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 kiviadsExt openrtb_ext.ImpExtKiviads
    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, &kiviadsExt); err != nil {
    51  			return nil, []error{err}
    52  		}
    53  
    54  		temp := reqBodyExt{KiviadsBidderExt: reqBodyExtBidder{}}
    55  
    56  		if kiviadsExt.PlacementID != "" {
    57  			temp.KiviadsBidderExt.PlacementID = kiviadsExt.PlacementID
    58  			temp.KiviadsBidderExt.Type = "publisher"
    59  		} else if kiviadsExt.EndpointID != "" {
    60  			temp.KiviadsBidderExt.EndpointID = kiviadsExt.EndpointID
    61  			temp.KiviadsBidderExt.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  	}, err
    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  	for _, seatBid := range response.SeatBid {
   121  		for i := range seatBid.Bid {
   122  			bidType, err := getMediaTypeForImp(seatBid.Bid[i].ImpID, request.Imp)
   123  			if err != nil {
   124  				return nil, []error{err}
   125  			}
   126  
   127  			b := &adapters.TypedBid{
   128  				Bid:     &seatBid.Bid[i],
   129  				BidType: bidType,
   130  			}
   131  			bidResponse.Bids = append(bidResponse.Bids, b)
   132  		}
   133  	}
   134  	return bidResponse, nil
   135  }
   136  
   137  func getMediaTypeForImp(impID string, imps []openrtb2.Imp) (openrtb_ext.BidType, error) {
   138  	for _, imp := range imps {
   139  		if imp.ID == impID {
   140  			if imp.Banner != nil {
   141  				return openrtb_ext.BidTypeBanner, nil
   142  			}
   143  			if imp.Video != nil {
   144  				return openrtb_ext.BidTypeVideo, nil
   145  			}
   146  			if imp.Native != nil {
   147  				return openrtb_ext.BidTypeNative, nil
   148  			}
   149  		}
   150  	}
   151  
   152  	return "", &errortypes.BadInput{
   153  		Message: fmt.Sprintf("Failed to find impression \"%s\"", impID),
   154  	}
   155  }