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

     1  package epom
     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  // Builder builds a new instance of the Epom adapter for the given bidder with the given config.
    20  func Builder(bidderName openrtb_ext.BidderName, config config.Adapter, server config.Server) (adapters.Bidder, error) {
    21  	bidder := &adapter{
    22  		endpoint: config.Endpoint,
    23  	}
    24  	return bidder, nil
    25  }
    26  
    27  func (a *adapter) MakeRequests(request *openrtb2.BidRequest, reqInfo *adapters.ExtraRequestInfo) (requests []*adapters.RequestData, errors []error) {
    28  	rq, errs := a.makeRequest(request)
    29  
    30  	if len(errs) > 0 {
    31  		return nil, errs
    32  	}
    33  
    34  	if rq != nil {
    35  		requests = append(requests, rq)
    36  	}
    37  
    38  	return requests, nil
    39  }
    40  
    41  func (a *adapter) makeRequest(request *openrtb2.BidRequest) (*adapters.RequestData, []error) {
    42  	if request.Device == nil || request.Device.IP == "" {
    43  		return nil, []error{&errortypes.BadInput{
    44  			Message: "ipv4 address is required field",
    45  		}}
    46  	}
    47  
    48  	reqJSON, err := json.Marshal(request)
    49  	if err != nil {
    50  		return nil, []error{err}
    51  	}
    52  
    53  	headers := http.Header{}
    54  	headers.Add("Content-Type", "application/json;charset=utf-8")
    55  	headers.Add("Accept", "application/json")
    56  	return &adapters.RequestData{
    57  		Method:  "POST",
    58  		Uri:     a.endpoint,
    59  		Body:    reqJSON,
    60  		Headers: headers,
    61  		ImpIDs:  openrtb_ext.GetImpIDs(request.Imp),
    62  	}, nil
    63  }
    64  
    65  func (a *adapter) MakeBids(internalRequest *openrtb2.BidRequest, externalRequest *adapters.RequestData, response *adapters.ResponseData) (*adapters.BidderResponse, []error) {
    66  	if response.StatusCode == http.StatusNoContent {
    67  		return nil, nil
    68  	}
    69  
    70  	if response.StatusCode >= http.StatusInternalServerError {
    71  		return nil, []error{&errortypes.BadServerResponse{
    72  			Message: fmt.Sprintf("Unexpected status code: %d. Dsp server internal error", response.StatusCode),
    73  		}}
    74  	}
    75  
    76  	if response.StatusCode >= http.StatusBadRequest {
    77  		return nil, []error{&errortypes.BadInput{
    78  			Message: fmt.Sprintf("Unexpected status code: %d. Bad request to dsp", response.StatusCode),
    79  		}}
    80  	}
    81  
    82  	if response.StatusCode != http.StatusOK {
    83  		return nil, []error{&errortypes.BadServerResponse{
    84  			Message: fmt.Sprintf("Unexpected status code: %d", response.StatusCode),
    85  		}}
    86  	}
    87  
    88  	var bidResp openrtb2.BidResponse
    89  	if err := json.Unmarshal(response.Body, &bidResp); err != nil {
    90  		return nil, []error{err}
    91  	}
    92  
    93  	//additional no content check
    94  	if len(bidResp.SeatBid) == 0 || len(bidResp.SeatBid[0].Bid) == 0 {
    95  		return nil, []error{&errortypes.Warning{
    96  			Message: "No bids in response",
    97  		}}
    98  	}
    99  
   100  	bidResponse := adapters.NewBidderResponseWithBidsCapacity(len(bidResp.SeatBid[0].Bid))
   101  
   102  	var errs []error
   103  	for _, seatBid := range bidResp.SeatBid {
   104  		for _, bid := range seatBid.Bid {
   105  			b := &adapters.TypedBid{
   106  				Bid:     &bid,
   107  				BidType: getMediaTypeForImp(bid.ImpID, internalRequest.Imp),
   108  			}
   109  			bidResponse.Bids = append(bidResponse.Bids, b)
   110  		}
   111  	}
   112  	return bidResponse, errs
   113  }
   114  
   115  func getMediaTypeForImp(impID string, imps []openrtb2.Imp) openrtb_ext.BidType {
   116  	for _, imp := range imps {
   117  		if imp.ID == impID {
   118  			if imp.Banner != nil {
   119  				return openrtb_ext.BidTypeBanner
   120  			} else if imp.Video != nil {
   121  				return openrtb_ext.BidTypeVideo
   122  			} else if imp.Native != nil {
   123  				return openrtb_ext.BidTypeNative
   124  			}
   125  		}
   126  	}
   127  	return openrtb_ext.BidTypeBanner
   128  }