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

     1  package inmobi
     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 InMobiAdapter struct {
    16  	endPoint string
    17  }
    18  
    19  // Builder builds a new instance of the InMobi 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 := &InMobiAdapter{
    22  		endPoint: config.Endpoint,
    23  	}
    24  	return bidder, nil
    25  }
    26  
    27  func (a *InMobiAdapter) MakeRequests(request *openrtb2.BidRequest, reqInfo *adapters.ExtraRequestInfo) ([]*adapters.RequestData, []error) {
    28  	var errs []error
    29  
    30  	if len(request.Imp) == 0 {
    31  		return nil, []error{&errortypes.BadInput{
    32  			Message: "No impression in the request",
    33  		}}
    34  	}
    35  
    36  	if err := preprocess(&request.Imp[0]); err != nil {
    37  		errs = append(errs, err)
    38  		return nil, errs
    39  	}
    40  
    41  	reqJson, err := json.Marshal(request)
    42  	if err != nil {
    43  		errs = append(errs, err)
    44  		return nil, errs
    45  	}
    46  
    47  	headers := http.Header{}
    48  	headers.Add("Content-Type", "application/json;charset=utf-8")
    49  	headers.Add("Accept", "application/json")
    50  
    51  	return []*adapters.RequestData{{
    52  		Method:  "POST",
    53  		Uri:     a.endPoint,
    54  		Body:    reqJson,
    55  		Headers: headers,
    56  		ImpIDs:  openrtb_ext.GetImpIDs(request.Imp),
    57  	}}, errs
    58  }
    59  
    60  func (a *InMobiAdapter) MakeBids(internalRequest *openrtb2.BidRequest, externalRequest *adapters.RequestData, response *adapters.ResponseData) (*adapters.BidderResponse, []error) {
    61  	if response.StatusCode == http.StatusNoContent {
    62  		return nil, nil
    63  	}
    64  
    65  	if response.StatusCode != http.StatusOK {
    66  		return nil, []error{&errortypes.BadServerResponse{
    67  			Message: fmt.Sprintf("Unexpected http status code: %d", response.StatusCode),
    68  		}}
    69  	}
    70  
    71  	var serverBidResponse openrtb2.BidResponse
    72  	if err := json.Unmarshal(response.Body, &serverBidResponse); err != nil {
    73  		return nil, []error{err}
    74  	}
    75  
    76  	bidResponse := adapters.NewBidderResponseWithBidsCapacity(1)
    77  
    78  	for _, sb := range serverBidResponse.SeatBid {
    79  		for i := range sb.Bid {
    80  			mediaType := getMediaTypeForImp(sb.Bid[i].ImpID, internalRequest.Imp)
    81  			bidResponse.Bids = append(bidResponse.Bids, &adapters.TypedBid{
    82  				Bid:     &sb.Bid[i],
    83  				BidType: mediaType,
    84  			})
    85  		}
    86  	}
    87  
    88  	return bidResponse, nil
    89  }
    90  
    91  func preprocess(imp *openrtb2.Imp) error {
    92  	var bidderExt adapters.ExtImpBidder
    93  	if err := json.Unmarshal(imp.Ext, &bidderExt); err != nil {
    94  		return &errortypes.BadInput{
    95  			Message: err.Error(),
    96  		}
    97  	}
    98  
    99  	var inMobiExt openrtb_ext.ExtImpInMobi
   100  	if err := json.Unmarshal(bidderExt.Bidder, &inMobiExt); err != nil {
   101  		return &errortypes.BadInput{Message: "bad InMobi bidder ext"}
   102  	}
   103  
   104  	if len(inMobiExt.Plc) == 0 {
   105  		return &errortypes.BadInput{Message: "'plc' is a required attribute for InMobi's bidder ext"}
   106  	}
   107  
   108  	if imp.Banner != nil {
   109  		banner := *imp.Banner
   110  		imp.Banner = &banner
   111  		if (banner.W == nil || banner.H == nil || *banner.W == 0 || *banner.H == 0) && len(banner.Format) > 0 {
   112  			format := banner.Format[0]
   113  			banner.W = &format.W
   114  			banner.H = &format.H
   115  		}
   116  	}
   117  
   118  	return nil
   119  }
   120  
   121  func getMediaTypeForImp(impId string, imps []openrtb2.Imp) openrtb_ext.BidType {
   122  	mediaType := openrtb_ext.BidTypeBanner
   123  	for _, imp := range imps {
   124  		if imp.ID == impId {
   125  			if imp.Video != nil {
   126  				mediaType = openrtb_ext.BidTypeVideo
   127  			}
   128  			if imp.Native != nil {
   129  				mediaType = openrtb_ext.BidTypeNative
   130  			}
   131  			break
   132  		}
   133  	}
   134  	return mediaType
   135  }