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

     1  package cwire
     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  /*
    16  Bid adapter implements and exports the requirements:
    17  
    18  - The adapters.Builder method to create a new instance of the adapter based on
    19    the host configuration
    20  
    21  - The adapters.Bidder interface consisting of the MakeRequests method to create
    22    outgoing requests to your bidding server and the MakeBids method to create bid
    23    responses.
    24  */
    25  
    26  type adapter struct {
    27  	endpoint string
    28  }
    29  
    30  // Builder builds a new instance of the CWire adapter for the given bidder with the given config.
    31  func Builder(bidderName openrtb_ext.BidderName, config config.Adapter, server config.Server) (adapters.Bidder, error) {
    32  	bidder := &adapter{
    33  		endpoint: config.Endpoint,
    34  	}
    35  	return bidder, nil
    36  }
    37  
    38  /*
    39  This method creates an HTTP requests that should be sent to the CWire OpenRTB endpoint.
    40  It's only provided with valid impressions for the adapter, it's not called if there is none.
    41  For optimization purposes bid adapters are forbidden from directly initiating any form of
    42  network communication and must entirely rely upon the core framework (adapters.RequestData).
    43  */
    44  func (a *adapter) MakeRequests(request *openrtb2.BidRequest, reqInfo *adapters.ExtraRequestInfo) ([]*adapters.RequestData, []error) {
    45  	headers := http.Header{}
    46  	headers.Add("Content-Type", "application/json;charset=utf-8")
    47  	headers.Add("Accept", "application/json")
    48  
    49  	resJSON, err := json.Marshal(request)
    50  	if err != nil {
    51  		return nil, []error{fmt.Errorf("Error while encoding OpenRTB BidRequest: %v", err)}
    52  	}
    53  
    54  	reqs := []*adapters.RequestData{
    55  		{
    56  			Method:  "POST",
    57  			Uri:     a.endpoint,
    58  			Body:    resJSON,
    59  			Headers: headers,
    60  			ImpIDs:  openrtb_ext.GetImpIDs(request.Imp),
    61  		},
    62  	}
    63  
    64  	return reqs, nil
    65  }
    66  
    67  /*
    68  This method is called for every Bid Response from CWire's OpenRTB endpoint.
    69  It maps the responses to core framework's OpenRTB 2.5 Bid Response object model.
    70  */
    71  func (a *adapter) MakeBids(bidReq *openrtb2.BidRequest, unused *adapters.RequestData, httpRes *adapters.ResponseData) (*adapters.BidderResponse, []error) {
    72  	if httpRes.StatusCode == http.StatusNoContent {
    73  		return nil, nil
    74  	}
    75  
    76  	if httpRes.StatusCode != http.StatusOK {
    77  		return nil, []error{
    78  			fmt.Errorf("Unexpected status code: %d. Run with request.debug = 1 for more info", httpRes.StatusCode),
    79  		}
    80  	}
    81  
    82  	var resp openrtb2.BidResponse
    83  	if err := json.Unmarshal(httpRes.Body, &resp); err != nil {
    84  		return nil, []error{&errortypes.BadServerResponse{
    85  			Message: fmt.Sprintf("Error while decoding response, err: %s", err),
    86  		}}
    87  	}
    88  
    89  	bidderResponse := adapters.NewBidderResponse()
    90  	bidderResponse.Currency = resp.Cur
    91  	for _, sb := range resp.SeatBid {
    92  		for i := range sb.Bid {
    93  			bidderResponse.Bids = append(bidderResponse.Bids, &adapters.TypedBid{
    94  				Bid:     &sb.Bid[i],
    95  				BidType: openrtb_ext.BidTypeBanner,
    96  			})
    97  		}
    98  	}
    99  
   100  	return bidderResponse, nil
   101  }