Skip to content

Commit

Permalink
add adapter applovin
Browse files Browse the repository at this point in the history
  • Loading branch information
shunj-nb committed Mar 24, 2023
1 parent 8f73580 commit 54a12e9
Show file tree
Hide file tree
Showing 6 changed files with 185 additions and 0 deletions.
147 changes: 147 additions & 0 deletions adapters/applovin/applovin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
package applovin

import (
"encoding/json"
"errors"
"fmt"
"net/http"

"github.com/prebid/openrtb/v17/openrtb2"
"github.com/prebid/prebid-server/adapters"
"github.com/prebid/prebid-server/config"
"github.com/prebid/prebid-server/errortypes"
"github.com/prebid/prebid-server/openrtb_ext"
)

type ApplovinAdapter struct {
endpoint string
token string
}

func (a *ApplovinAdapter) MakeRequests(request *openrtb2.BidRequest, _ *adapters.ExtraRequestInfo) ([]*adapters.RequestData, []error) {
headers := http.Header{}
headers.Add("Content-Type", "application/json;charset=utf-8")
headers.Add("Accept", "application/json")
headers.Add("X-Account-Key", a.token)
impressions := request.Imp
result := make([]*adapters.RequestData, 0, len(impressions))
errs := make([]error, 0, len(impressions))

for _, impression := range impressions {
if impression.Banner == nil && impression.Video == nil && impression.Native == nil {
errs = append(errs, &errortypes.BadInput{
Message: "Applovin only supports banner, video or native ads",
})
continue
}
if impression.Banner != nil {
if impression.Banner.W == nil || impression.Banner.H == nil || *impression.Banner.W == 0 || *impression.Banner.H == 0 {
if len(impression.Banner.Format) == 0 {
errs = append(errs, &errortypes.BadInput{
Message: "banner size information missing",
})
continue
}
banner := *impression.Banner
banner.W = openrtb2.Int64Ptr(banner.Format[0].W)
banner.H = openrtb2.Int64Ptr(banner.Format[0].H)
impression.Banner = &banner
}
}
if len(impression.Ext) == 0 {
errs = append(errs, errors.New("impression extensions required"))
continue
}
var bidderExt adapters.ExtImpBidder
err := json.Unmarshal(impression.Ext, &bidderExt)
if err != nil {
errs = append(errs, err)
continue
}
if len(bidderExt.Bidder) == 0 {
errs = append(errs, errors.New("bidder required"))
continue
}
var impressionExt openrtb_ext.ExtImpApplovin
err = json.Unmarshal(bidderExt.Bidder, &impressionExt)
if err != nil {
errs = append(errs, err)
continue
}
if impressionExt.TagID == "" {
errs = append(errs, errors.New("Applovin token required"))
continue
}
request.Imp = []openrtb2.Imp{impression}
body, err := json.Marshal(request)
if err != nil {
errs = append(errs, err)
continue
}
result = append(result, &adapters.RequestData{
Method: "POST",
Uri: a.endpoint,
Body: body,
Headers: headers,
})
}

request.Imp = impressions

if len(result) == 0 {
return nil, errs
}
return result, errs
}

func (a *ApplovinAdapter) MakeBids(bidReq *openrtb2.BidRequest, adapterReq *adapters.RequestData, adapterResp *adapters.ResponseData) (*adapters.BidderResponse, []error) {
if adapterResp.StatusCode != http.StatusOK {
if adapterResp.StatusCode == http.StatusNoContent {
return nil, nil
}
if adapterResp.StatusCode == http.StatusBadRequest {
return nil, []error{&errortypes.BadInput{
Message: fmt.Sprintf("Unexpected status code: %d", adapterResp.StatusCode),
}}
}
return nil, []error{&errortypes.BadServerResponse{
Message: fmt.Sprintf("Unexpected status code: %d", adapterResp.StatusCode),
}}
}

var bidResp openrtb2.BidResponse
if err := json.Unmarshal(adapterResp.Body, &bidResp); err != nil {
return nil, []error{&errortypes.BadServerResponse{
Message: fmt.Sprintf("Failed to unmarshal bid response: %s", err.Error()),
}}
}

bidderResp := adapters.NewBidderResponseWithBidsCapacity(len(bidReq.Imp))
var errors []error

for _, seatbid := range bidResp.SeatBid {
for _, bid := range seatbid.Bid {
bidderResp.Bids = append(bidderResp.Bids, &adapters.TypedBid{
Bid: &bid,
BidType: openrtb_ext.BidTypeBanner,
})
break
}
}

if bidResp.Cur != "" {
bidderResp.Currency = bidResp.Cur
}
return bidderResp, errors
}

func Builder(bidderName openrtb_ext.BidderName, config config.Adapter, server config.Server) (adapters.Bidder, error) {
if config.AppSecret == "" {
return nil, errors.New("AppSecret is not configured. Did you set adapters.applovin.app_secret in the app config?")
}
bidder := &ApplovinAdapter{
endpoint: config.Endpoint,
token: config.AppSecret,
}
return bidder, nil
}
2 changes: 2 additions & 0 deletions exchange/adapter_builders.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"github.com/prebid/prebid-server/adapters/amx"
"github.com/prebid/prebid-server/adapters/apacdex"
"github.com/prebid/prebid-server/adapters/applogy"
"github.com/prebid/prebid-server/adapters/applovin"
"github.com/prebid/prebid-server/adapters/appnexus"
"github.com/prebid/prebid-server/adapters/appush"
"github.com/prebid/prebid-server/adapters/audienceNetwork"
Expand Down Expand Up @@ -275,6 +276,7 @@ func newAdapterBuilders() map[openrtb_ext.BidderName]adapters.Builder {
openrtb_ext.BidderPangle: pangle.Builder,
openrtb_ext.BidderPGAM: adtelligent.Builder,
openrtb_ext.BidderPubmatic: pubmatic.Builder,
openrtb_ext.BidderApplovin: applovin.Builder,
openrtb_ext.BidderPubnative: pubnative.Builder,
openrtb_ext.BidderPulsepoint: pulsepoint.Builder,
openrtb_ext.BidderQuantumdex: apacdex.Builder,
Expand Down
2 changes: 2 additions & 0 deletions openrtb_ext/bidders.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ const (
BidderPangle BidderName = "pangle"
BidderPGAM BidderName = "pgam"
BidderPubmatic BidderName = "pubmatic"
BidderApplovin BidderName = "applovin"
BidderPubnative BidderName = "pubnative"
BidderPulsepoint BidderName = "pulsepoint"
BidderQuantumdex BidderName = "quantumdex"
Expand Down Expand Up @@ -363,6 +364,7 @@ func CoreBidderNames() []BidderName {
BidderPangle,
BidderPGAM,
BidderPubmatic,
BidderApplovin,
BidderPubnative,
BidderPulsepoint,
BidderQuantumdex,
Expand Down
5 changes: 5 additions & 0 deletions openrtb_ext/imp_applovin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package openrtb_ext

type ExtImpApplovin struct {
TagID string `json:"tagid"`
}
15 changes: 15 additions & 0 deletions static/bidder-info/applovin.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
endpoint: "https://s2s.applovin.com/ad"
disabled: true
maintainer:
email: "support@applovin.com"
capabilities:
app:
mediaTypes:
- banner
- video
- native
site:
mediaTypes:
- banner
- video
- native
14 changes: 14 additions & 0 deletions static/bidder-params/applovin.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"$schema": "http://json-schema.org/draft-04/schema#",
"title": "Applovin Adapter Params",
"description": "A schema which validates params accepted by the Applovin adapter",
"type": "object",
"properties": {
"tagid": {
"type": "string",
"description": "Applovin token"
}
},
"required": ["tagid"]
}

0 comments on commit 54a12e9

Please sign in to comment.