Skip to content

Commit 9d4c906

Browse files
author
zhoubing01
committed
New Adapter: iqiyi
remove go.mod and go.sum files add go.mod go.sum format json file
1 parent 8070000 commit 9d4c906

File tree

15 files changed

+831
-0
lines changed

15 files changed

+831
-0
lines changed

adapters/iqiyi/iqiyi.go

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
package iqiyi
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"github.com/prebid/openrtb/v20/openrtb2"
7+
"github.com/prebid/prebid-server/v3/adapters"
8+
"github.com/prebid/prebid-server/v3/config"
9+
"github.com/prebid/prebid-server/v3/errortypes"
10+
"github.com/prebid/prebid-server/v3/macros"
11+
"github.com/prebid/prebid-server/v3/openrtb_ext"
12+
"github.com/prebid/prebid-server/v3/util/jsonutil"
13+
"net/http"
14+
"text/template"
15+
)
16+
17+
type IqiyiAdapter struct {
18+
endpoint *template.Template
19+
}
20+
21+
func Builder(bidderName openrtb_ext.BidderName, config config.Adapter, server config.Server) (adapters.Bidder, error) {
22+
template, err := template.New("endpointTemplate").Parse(config.Endpoint)
23+
if err != nil {
24+
return nil, fmt.Errorf("unable to parse endpoint url template: %v", err)
25+
}
26+
27+
bidder := &IqiyiAdapter{
28+
endpoint: template,
29+
}
30+
return bidder, nil
31+
}
32+
33+
func (a *IqiyiAdapter) buildEndpointURL(params *openrtb_ext.ExtImpIqiyi) (string, error) {
34+
endpointParams := macros.EndpointTemplateParams{AccountID: params.AccountID}
35+
return macros.ResolveMacros(a.endpoint, endpointParams)
36+
}
37+
38+
func (a *IqiyiAdapter) MakeRequests(request *openrtb2.BidRequest, reqInfo *adapters.ExtraRequestInfo) ([]*adapters.RequestData, []error) {
39+
var errs []error
40+
if len(request.Imp) == 0 {
41+
return nil, []error{&errortypes.BadInput{
42+
Message: "No impression in the request",
43+
}}
44+
}
45+
46+
var bidderExt adapters.ExtImpBidder
47+
if err := jsonutil.Unmarshal(request.Imp[0].Ext, &bidderExt); err != nil {
48+
return nil, []error{&errortypes.BadInput{Message: "bad Iqiyi bidder ext"}}
49+
}
50+
51+
var iqiyiExt openrtb_ext.ExtImpIqiyi
52+
if err := jsonutil.Unmarshal(bidderExt.Bidder, &iqiyiExt); err != nil {
53+
return nil, []error{&errortypes.BadInput{Message: "bad Iqiyi bidder ext"}}
54+
}
55+
56+
for i := range request.Imp {
57+
imp := &request.Imp[i]
58+
if imp.Banner != nil {
59+
b := *imp.Banner
60+
if (b.W == nil || b.H == nil || *b.W == 0 || *b.H == 0) && len(b.Format) > 0 {
61+
fmt := b.Format[0]
62+
b.W = &fmt.W
63+
b.H = &fmt.H
64+
imp.Banner = &b
65+
}
66+
}
67+
if imp.BidFloorCur == "" && imp.BidFloor > 0 {
68+
imp.BidFloorCur = "USD"
69+
}
70+
}
71+
72+
url, err := a.buildEndpointURL(&iqiyiExt)
73+
if err != nil {
74+
return nil, []error{err}
75+
}
76+
77+
reqJson, err := json.Marshal(request)
78+
if err != nil {
79+
errs = append(errs, err)
80+
return nil, errs
81+
}
82+
83+
headers := http.Header{}
84+
headers.Add("Content-Type", "application/json;charset=utf-8")
85+
headers.Add("Accept", "application/json")
86+
87+
return []*adapters.RequestData{{
88+
Method: "POST",
89+
Uri: url,
90+
Body: reqJson,
91+
Headers: headers,
92+
ImpIDs: openrtb_ext.GetImpIDs(request.Imp),
93+
}}, errs
94+
}
95+
96+
func (a *IqiyiAdapter) MakeBids(internalRequest *openrtb2.BidRequest, externalRequest *adapters.RequestData, response *adapters.ResponseData) (*adapters.BidderResponse, []error) {
97+
if response.StatusCode == http.StatusNoContent {
98+
return nil, nil
99+
}
100+
101+
if response.StatusCode != http.StatusOK {
102+
return nil, []error{&errortypes.BadServerResponse{
103+
Message: fmt.Sprintf("Unexpected http status code: %d", response.StatusCode),
104+
}}
105+
}
106+
107+
var serverBidResponse openrtb2.BidResponse
108+
if err := jsonutil.Unmarshal(response.Body, &serverBidResponse); err != nil {
109+
return nil, []error{err}
110+
}
111+
112+
bidResponse := adapters.NewBidderResponseWithBidsCapacity(1)
113+
114+
for _, seatbid := range serverBidResponse.SeatBid {
115+
for i := range seatbid.Bid {
116+
mediaType, err := getMediaTypeForImp(seatbid.Bid[i])
117+
if err != nil {
118+
return nil, []error{err}
119+
}
120+
bidResponse.Bids = append(bidResponse.Bids, &adapters.TypedBid{
121+
Bid: &seatbid.Bid[i],
122+
BidType: mediaType,
123+
})
124+
}
125+
}
126+
127+
return bidResponse, nil
128+
}
129+
130+
func getMediaTypeForImp(bid openrtb2.Bid) (openrtb_ext.BidType, error) {
131+
switch bid.MType {
132+
case openrtb2.MarkupBanner:
133+
return openrtb_ext.BidTypeBanner, nil
134+
case openrtb2.MarkupVideo:
135+
return openrtb_ext.BidTypeVideo, nil
136+
case openrtb2.MarkupNative:
137+
return openrtb_ext.BidTypeNative, nil
138+
default:
139+
return "", &errortypes.BadServerResponse{
140+
Message: fmt.Sprintf("Unsupported mtype %d for bid %s", bid.MType, bid.ID),
141+
}
142+
}
143+
}

adapters/iqiyi/iqiyi_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package iqiyi
2+
3+
import (
4+
"testing"
5+
6+
"github.com/prebid/prebid-server/v3/adapters/adapterstest"
7+
"github.com/prebid/prebid-server/v3/config"
8+
"github.com/prebid/prebid-server/v3/openrtb_ext"
9+
)
10+
11+
func TestJsonSamples(t *testing.T) {
12+
bidder, buildErr := Builder(openrtb_ext.BidderIqiyi, config.Adapter{
13+
Endpoint: "https://cupid.iqiyi.net/bid?a={{.AccountID}}"}, config.Server{ExternalUrl: "http://hosturl.com", GvlID: 1, DataCenter: "2"})
14+
15+
if buildErr != nil {
16+
t.Fatalf("Builder returned unexpected error %v", buildErr)
17+
}
18+
19+
adapterstest.RunJSONBidderTest(t, "iqiyitest", bidder)
20+
}
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
{
2+
"mockBidRequest": {
3+
"app": {
4+
"bundle": "com.example.app"
5+
},
6+
"id": "req-id",
7+
"device": {
8+
"ifa": "9d8fe0a9-c0dd-4482-b16b-5709b00c608d",
9+
"ip": "1.1.1.1",
10+
"ua": "Mozilla/5.0 (Linux; Android 8.0.0; SM-G960F Build/R16NW) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.84 Mobile Safari/537.36"
11+
},
12+
"imp": [
13+
{
14+
"ext": {
15+
"bidder": {
16+
"accountid": "100000099"
17+
}
18+
},
19+
"banner": {
20+
"w": 320,
21+
"h": 50
22+
},
23+
"id": "imp-id"
24+
}
25+
]
26+
},
27+
"httpCalls": [{
28+
"expectedRequest": {
29+
"uri": "https://cupid.iqiyi.net/bid?a=100000099",
30+
"body": {
31+
"app": {
32+
"bundle": "com.example.app"
33+
},
34+
"id": "req-id",
35+
"device": {
36+
"ifa": "9d8fe0a9-c0dd-4482-b16b-5709b00c608d",
37+
"ip": "1.1.1.1",
38+
"ua": "Mozilla/5.0 (Linux; Android 8.0.0; SM-G960F Build/R16NW) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.84 Mobile Safari/537.36"
39+
},
40+
"imp": [
41+
{
42+
"ext": {
43+
"bidder": {
44+
"accountid": "100000099"
45+
}
46+
},
47+
"banner": {
48+
"w": 320,
49+
"h": 50
50+
},
51+
"id": "imp-id"
52+
}
53+
]
54+
},
55+
"impIDs":["imp-id"]
56+
},
57+
"mockResponse": {
58+
"status": 200,
59+
"body": {
60+
"id": "req-id",
61+
"seatbid": [
62+
{
63+
"bid": [
64+
{
65+
"ext": {
66+
"prebid": {
67+
"meta": {
68+
"networkName": "iqiyi"
69+
}
70+
}
71+
},
72+
"nurl": "https://some.event.url/params",
73+
"crid": "123456789",
74+
"adomain": [],
75+
"price": 2.0,
76+
"id": "1234",
77+
"adm": "bannerhtml",
78+
"impid": "imp-id",
79+
"mtype": 1
80+
}
81+
]
82+
}
83+
]
84+
}
85+
}
86+
}],
87+
88+
"expectedBidResponses": [{
89+
"currency": "USD",
90+
"bids": [{
91+
"bid": {
92+
"id": "1234",
93+
"impid": "imp-id",
94+
"price": 2.0,
95+
"adm": "bannerhtml",
96+
"crid": "123456789",
97+
"nurl": "https://some.event.url/params",
98+
"mtype": 1,
99+
"ext": {
100+
"prebid": {
101+
"meta": {
102+
"networkName": "iqiyi"
103+
}
104+
}
105+
}
106+
},
107+
"type": "banner"
108+
}]
109+
}]
110+
}
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
{
2+
"mockBidRequest": {
3+
"app": {
4+
"bundle": "com.example.app"
5+
},
6+
"id": "req-id",
7+
"device": {
8+
"ifa": "9d8fe0a9-c0dd-4482-b16b-5709b00c608d",
9+
"ip": "1.1.1.1",
10+
"ua": "Mozilla/5.0 (Linux; Android 8.0.0; SM-G960F Build/R16NW) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.84 Mobile Safari/537.36"
11+
},
12+
"imp": [
13+
{
14+
"ext": {
15+
"bidder": {
16+
"accountid": "100000099"
17+
}
18+
},
19+
"native": {
20+
"request": "{\"ver\":\"1.2\",\"context\":2,\"contextsubtype\":20,\"plcmttype\":11,\"plcmtcnt\":1,\"aurlsupport\":1,\"durlsupport\":1,\"assets\":[{\"id\":123,\"required\":1,\"title\":{\"len\":140}},{\"id\":128,\"required\":0,\"img\":{\"wmin\":836,\"hmin\":627,\"type\":3}}]}"
21+
},
22+
"id": "imp-id"
23+
}
24+
]
25+
},
26+
"httpCalls": [{
27+
"expectedRequest": {
28+
"uri": "https://cupid.iqiyi.net/bid?a=100000099",
29+
"body": {
30+
"app": {
31+
"bundle": "com.example.app"
32+
},
33+
"id": "req-id",
34+
"device": {
35+
"ifa": "9d8fe0a9-c0dd-4482-b16b-5709b00c608d",
36+
"ip": "1.1.1.1",
37+
"ua": "Mozilla/5.0 (Linux; Android 8.0.0; SM-G960F Build/R16NW) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.84 Mobile Safari/537.36"
38+
},
39+
"imp": [
40+
{
41+
"ext": {
42+
"bidder": {
43+
"accountid": "100000099"
44+
}
45+
},
46+
"native": {
47+
"request": "{\"ver\":\"1.2\",\"context\":2,\"contextsubtype\":20,\"plcmttype\":11,\"plcmtcnt\":1,\"aurlsupport\":1,\"durlsupport\":1,\"assets\":[{\"id\":123,\"required\":1,\"title\":{\"len\":140}},{\"id\":128,\"required\":0,\"img\":{\"wmin\":836,\"hmin\":627,\"type\":3}}]}"
48+
},
49+
"id": "imp-id"
50+
}
51+
]
52+
},
53+
"impIDs":["imp-id"]
54+
},
55+
"mockResponse": {
56+
"status": 200,
57+
"body": {
58+
"id": "req-id",
59+
"seatbid": [
60+
{
61+
"bid": [
62+
{
63+
"ext": {
64+
"prebid": {
65+
"meta": {
66+
"networkName": "iqiyi"
67+
}
68+
}
69+
},
70+
"nurl": "https://some.event.url/params",
71+
"crid": "123456789",
72+
"adomain": [],
73+
"price": 2.0,
74+
"id": "1234",
75+
"adm": "native-json",
76+
"impid": "imp-id",
77+
"mtype": 4
78+
}
79+
]
80+
}
81+
]
82+
}
83+
}
84+
}],
85+
86+
"expectedBidResponses": [{
87+
"currency": "USD",
88+
"bids": [{
89+
"bid": {
90+
"id": "1234",
91+
"impid": "imp-id",
92+
"price": 2.0,
93+
"adm": "native-json",
94+
"crid": "123456789",
95+
"nurl": "https://some.event.url/params",
96+
"mtype": 4,
97+
"ext": {
98+
"prebid": {
99+
"meta": {
100+
"networkName": "iqiyi"
101+
}
102+
}
103+
}
104+
},
105+
"type": "native"
106+
}]
107+
}]
108+
}

0 commit comments

Comments
 (0)