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

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