Skip to content

Commit

Permalink
Improve Digital adapter: Add deal id and rewardred inventory logic (p…
Browse files Browse the repository at this point in the history
…rebid#2439)

Co-authored-by: Samiul Amin <[email protected]>
  • Loading branch information
jbartek25 and samiul-shanto authored Nov 17, 2022
1 parent 79ae565 commit 563f6c6
Show file tree
Hide file tree
Showing 3 changed files with 840 additions and 6 deletions.
75 changes: 69 additions & 6 deletions adapters/improvedigital/improvedigital.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"net/http"
"strconv"
"strings"

"github.com/prebid/openrtb/v17/openrtb2"
Expand All @@ -13,10 +14,27 @@ import (
"github.com/prebid/prebid-server/openrtb_ext"
)

const (
buyingTypeRTB = "rtb"
isRewardedInventory = "is_rewarded_inventory"
stateRewardedInventoryEnable = "1"
consentProvidersSettingsInputKey = "ConsentedProvidersSettings"
consentProvidersSettingsOutKey = "consented_providers_settings"
consentedProvidersKey = "consented_providers"
)

type ImprovedigitalAdapter struct {
endpoint string
}

// BidExt represents Improved Digital bid extension with line item ID and buying type values
type BidExt struct {
Improvedigital struct {
LineItemID int `json:"line_item_id"`
BuyingType string `json:"buying_type"`
}
}

// MakeRequests makes the HTTP requests which should be made to fetch bids.
func (a *ImprovedigitalAdapter) MakeRequests(request *openrtb2.BidRequest, reqInfo *adapters.ExtraRequestInfo) ([]*adapters.RequestData, []error) {
numRequests := len(request.Imp)
Expand All @@ -36,6 +54,15 @@ func (a *ImprovedigitalAdapter) MakeRequests(request *openrtb2.BidRequest, reqIn
}

func (a *ImprovedigitalAdapter) makeRequest(request openrtb2.BidRequest, imp openrtb2.Imp) (*adapters.RequestData, error) {
// Handle Rewarded Inventory
impExt, err := getImpExtWithRewardedInventory(imp)
if err != nil {
return nil, err
}
if impExt != nil {
imp.Ext = impExt
}

request.Imp = []openrtb2.Imp{imp}

userExtAddtlConsent, err := a.getAdditionalConsentProvidersUserExt(request)
Expand Down Expand Up @@ -114,6 +141,19 @@ func (a *ImprovedigitalAdapter) MakeBids(internalRequest *openrtb2.BidRequest, e
return nil, []error{err}
}

if bid.Ext != nil {
var bidExt BidExt
err = json.Unmarshal(bid.Ext, &bidExt)
if err != nil {
return nil, []error{err}
}

bidExtImprovedigital := bidExt.Improvedigital
if bidExtImprovedigital.LineItemID != 0 && bidExtImprovedigital.BuyingType != "" && bidExtImprovedigital.BuyingType != buyingTypeRTB {
bid.DealID = strconv.Itoa(bidExtImprovedigital.LineItemID)
}
}

bidResponse.Bids = append(bidResponse.Bids, &adapters.TypedBid{
Bid: &bid,
BidType: bidType,
Expand Down Expand Up @@ -160,12 +200,6 @@ func getMediaTypeForImp(impID string, imps []openrtb2.Imp) (openrtb_ext.BidType,

// This method responsible to clone request and convert additional consent providers string to array when additional consent provider found
func (a *ImprovedigitalAdapter) getAdditionalConsentProvidersUserExt(request openrtb2.BidRequest) ([]byte, error) {
const (
consentProvidersSettingsInputKey = "ConsentedProvidersSettings"
consentProvidersSettingsOutKey = "consented_providers_settings"
consentedProvidersKey = "consented_providers"
)

var cpStr string

// If user/user.ext not defined, no need to parse additional consent
Expand Down Expand Up @@ -222,3 +256,32 @@ func (a *ImprovedigitalAdapter) getAdditionalConsentProvidersUserExt(request ope

return extJson, nil
}

func getImpExtWithRewardedInventory(imp openrtb2.Imp) ([]byte, error) {
var ext = make(map[string]json.RawMessage)
if err := json.Unmarshal(imp.Ext, &ext); err != nil {
return nil, err
}

prebidJSONValue, prebidJSONFound := ext["prebid"]
if !prebidJSONFound {
return nil, nil
}

var prebidMap = make(map[string]json.RawMessage)
if err := json.Unmarshal(prebidJSONValue, &prebidMap); err != nil {
return nil, err
}

if rewardedInventory, foundRewardedInventory := prebidMap[isRewardedInventory]; foundRewardedInventory && string(rewardedInventory) == stateRewardedInventoryEnable {
ext[isRewardedInventory] = json.RawMessage(`true`)
impExt, err := json.Marshal(ext)
if err != nil {
return nil, err
}

return impExt, nil
}

return nil, nil
}
Loading

0 comments on commit 563f6c6

Please sign in to comment.