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

     1  package salunamedia
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"net/http"
     7  
     8  	"github.com/prebid/openrtb/v20/openrtb2"
     9  
    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/openrtb_ext"
    14  )
    15  
    16  type adapter struct {
    17  	endpoint string
    18  }
    19  
    20  type bidExt struct {
    21  	MediaType string `json:"mediaType"`
    22  }
    23  
    24  func Builder(bidderName openrtb_ext.BidderName, config config.Adapter, server config.Server) (adapters.Bidder, error) {
    25  	bidder := &adapter{
    26  		endpoint: config.Endpoint,
    27  	}
    28  	return bidder, nil
    29  }
    30  
    31  func (a *adapter) MakeRequests(
    32  	openRTBRequest *openrtb2.BidRequest,
    33  	reqInfo *adapters.ExtraRequestInfo,
    34  ) (
    35  	requestsToBidder []*adapters.RequestData,
    36  	errs []error,
    37  ) {
    38  
    39  	reqJSON, err := json.Marshal(openRTBRequest)
    40  	if err != nil {
    41  		return nil, []error{err}
    42  	}
    43  
    44  	headers := http.Header{}
    45  	headers.Add("Content-Type", "application/json;charset=utf-8")
    46  	headers.Add("Accept", "application/json")
    47  
    48  	return []*adapters.RequestData{{
    49  		Method:  http.MethodPost,
    50  		Body:    reqJSON,
    51  		Uri:     a.endpoint,
    52  		Headers: headers,
    53  		ImpIDs:  openrtb_ext.GetImpIDs(openRTBRequest.Imp),
    54  	}}, nil
    55  }
    56  
    57  func (a *adapter) MakeBids(
    58  	openRTBRequest *openrtb2.BidRequest,
    59  	requestToBidder *adapters.RequestData,
    60  	bidderRawResponse *adapters.ResponseData,
    61  ) (
    62  	bidderResponse *adapters.BidderResponse,
    63  	errs []error,
    64  ) {
    65  	if bidderRawResponse.StatusCode == http.StatusNoContent {
    66  		return nil, nil
    67  	}
    68  
    69  	if bidderRawResponse.StatusCode == http.StatusBadRequest {
    70  		return nil, []error{&errortypes.BadInput{
    71  			Message: fmt.Sprintf("Bad Request. %s", string(bidderRawResponse.Body)),
    72  		}}
    73  	}
    74  
    75  	if bidderRawResponse.StatusCode == http.StatusServiceUnavailable {
    76  		return nil, []error{&errortypes.BadInput{
    77  			Message: "Bidder unavailable. Please contact the bidder support.",
    78  		}}
    79  	}
    80  
    81  	if bidderRawResponse.StatusCode != http.StatusOK {
    82  		return nil, []error{&errortypes.BadServerResponse{
    83  			Message: fmt.Sprintf("Status Code: [ %d ] %s", bidderRawResponse.StatusCode, string(bidderRawResponse.Body)),
    84  		}}
    85  	}
    86  
    87  	responseBody := bidderRawResponse.Body
    88  	var bidResp openrtb2.BidResponse
    89  	if err := json.Unmarshal(responseBody, &bidResp); err != nil {
    90  		return nil, []error{err}
    91  	}
    92  
    93  	if len(bidResp.SeatBid) == 0 {
    94  		return nil, []error{&errortypes.BadServerResponse{
    95  			Message: "Empty SeatBid",
    96  		}}
    97  	}
    98  
    99  	bidResponse := adapters.NewBidderResponseWithBidsCapacity(1)
   100  
   101  	bids := bidResp.SeatBid[0].Bid
   102  
   103  	if len(bids) == 0 {
   104  		return nil, []error{&errortypes.BadServerResponse{
   105  			Message: "Empty SeatBid.Bids",
   106  		}}
   107  	}
   108  
   109  	bid := bids[0]
   110  
   111  	var bidExt bidExt
   112  	var bidType openrtb_ext.BidType
   113  
   114  	if err := json.Unmarshal(bid.Ext, &bidExt); err != nil {
   115  		return nil, []error{&errortypes.BadServerResponse{
   116  			Message: "Missing BidExt",
   117  		}}
   118  	}
   119  
   120  	bidType, err := getBidType(bidExt)
   121  
   122  	if err != nil {
   123  		return nil, []error{err}
   124  	}
   125  
   126  	bidResponse.Bids = append(bidResponse.Bids, &adapters.TypedBid{
   127  		Bid:     &bid,
   128  		BidType: bidType,
   129  	})
   130  	return bidResponse, nil
   131  }
   132  
   133  func getBidType(ext bidExt) (openrtb_ext.BidType, error) {
   134  	return openrtb_ext.ParseBidType(ext.MediaType)
   135  }