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

     1  package adrino
     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  func (a *adapter) MakeRequests(request *openrtb2.BidRequest, reqInfo *adapters.ExtraRequestInfo) ([]*adapters.RequestData, []error) {
    20  	var errs []error
    21  
    22  	reqJson, err := json.Marshal(request)
    23  	if err != nil {
    24  		errs = append(errs, err)
    25  		return nil, errs
    26  	}
    27  
    28  	headers := http.Header{}
    29  	headers.Add("Content-Type", "application/json;charset=utf-8")
    30  
    31  	return []*adapters.RequestData{{
    32  		Method:  "POST",
    33  		Uri:     a.endpoint,
    34  		Body:    reqJson,
    35  		Headers: headers,
    36  		ImpIDs:  openrtb_ext.GetImpIDs(request.Imp),
    37  	}}, errs
    38  }
    39  
    40  func (a *adapter) MakeBids(internalRequest *openrtb2.BidRequest, externalRequest *adapters.RequestData, response *adapters.ResponseData) (*adapters.BidderResponse, []error) {
    41  	var errs []error
    42  
    43  	if response.StatusCode == http.StatusNoContent {
    44  		return nil, nil
    45  	}
    46  
    47  	if response.StatusCode == http.StatusBadRequest {
    48  		return nil, []error{&errortypes.BadInput{
    49  			Message: fmt.Sprintf("Unexpected status code: %d. Run with request.debug = 1 for more info", response.StatusCode),
    50  		}}
    51  	}
    52  
    53  	if response.StatusCode != http.StatusOK {
    54  		return nil, []error{&errortypes.BadServerResponse{
    55  			Message: fmt.Sprintf("Unexpected status code: %d. Run with request.debug = 1 for more info", response.StatusCode),
    56  		}}
    57  	}
    58  
    59  	var bidResp openrtb2.BidResponse
    60  
    61  	if err := json.Unmarshal(response.Body, &bidResp); err != nil {
    62  		return nil, []error{err}
    63  	}
    64  
    65  	bidResponse := adapters.NewBidderResponse()
    66  	bidResponse.Currency = bidResp.Cur
    67  
    68  	for _, seatBid := range bidResp.SeatBid {
    69  		for i := range seatBid.Bid {
    70  			bidType := openrtb_ext.BidTypeNative // our adserver supports only native ads
    71  			b := &adapters.TypedBid{
    72  				Bid:     &seatBid.Bid[i],
    73  				BidType: bidType,
    74  			}
    75  			bidResponse.Bids = append(bidResponse.Bids, b)
    76  		}
    77  	}
    78  	return bidResponse, errs
    79  }
    80  
    81  func Builder(bidderName openrtb_ext.BidderName, config config.Adapter, server config.Server) (adapters.Bidder, error) {
    82  	bidder := &adapter{
    83  		endpoint: config.Endpoint,
    84  	}
    85  	return bidder, nil
    86  }