-
Notifications
You must be signed in to change notification settings - Fork 861
New Adapter: iqiyi #4605
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Heihuang
wants to merge
11
commits into
prebid:master
Choose a base branch
from
Heihuang:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
New Adapter: iqiyi #4605
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
e254d40
New Adapter: iqiyi
9e70614
Trigger workflow
9c8c4cb
fix: formatted code with go fmt
e0a4b3f
Optimize some code
0164a55
Optimized utest
8202caf
add params_test.go and optimize code
55901f9
fix fmt
4efc7ae
Add BidVideo when constructing the Bid object
ea86389
Add BidVideo when constructing the Bid object
a9ca818
Optimize the getBidVideo code
f10f875
Merge branch 'master' into master
Heihuang File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,184 @@ | ||
| package iqiyi | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "fmt" | ||
| "github.com/prebid/openrtb/v20/openrtb2" | ||
| "github.com/prebid/prebid-server/v3/adapters" | ||
| "github.com/prebid/prebid-server/v3/config" | ||
| "github.com/prebid/prebid-server/v3/errortypes" | ||
| "github.com/prebid/prebid-server/v3/macros" | ||
| "github.com/prebid/prebid-server/v3/openrtb_ext" | ||
| "github.com/prebid/prebid-server/v3/util/jsonutil" | ||
| "net/http" | ||
| "text/template" | ||
| ) | ||
|
|
||
| const ( | ||
| defaultBannerFormatIndex = 0 | ||
| minBannerSize = 0 | ||
| defaultCurrency = "USD" | ||
| ) | ||
|
|
||
| type adapter struct { | ||
| endpoint *template.Template | ||
| } | ||
|
|
||
| func selectCurrency(req *openrtb2.BidRequest, resp *openrtb2.BidResponse) string { | ||
| if resp.Cur != "" { | ||
| return resp.Cur | ||
| } | ||
|
|
||
| if len(req.Cur) > 0 && req.Cur[0] != "" { | ||
| return req.Cur[0] | ||
| } | ||
|
|
||
| return defaultCurrency | ||
| } | ||
|
|
||
| func Builder(bidderName openrtb_ext.BidderName, config config.Adapter, server config.Server) (adapters.Bidder, error) { | ||
| template, err := template.New("endpointTemplate").Parse(config.Endpoint) | ||
| if err == nil { | ||
| _, err = macros.ResolveMacros(template, macros.EndpointTemplateParams{}) | ||
| } | ||
|
|
||
| if err != nil { | ||
| return nil, fmt.Errorf("unable to parse endpoint url template: %w", err) | ||
| } | ||
|
|
||
| bidder := &adapter{ | ||
| endpoint: template, | ||
| } | ||
| return bidder, nil | ||
| } | ||
|
|
||
| func (a *adapter) buildEndpointURL(params *openrtb_ext.ExtImpIqiyi) (string, error) { | ||
| endpointParams := macros.EndpointTemplateParams{AccountID: params.AccountID} | ||
| return macros.ResolveMacros(a.endpoint, endpointParams) | ||
| } | ||
|
|
||
| func (a *adapter) MakeRequests(request *openrtb2.BidRequest, reqInfo *adapters.ExtraRequestInfo) ([]*adapters.RequestData, []error) { | ||
| var errs []error | ||
|
|
||
| var bidderExt adapters.ExtImpBidder | ||
| if err := jsonutil.Unmarshal(request.Imp[0].Ext, &bidderExt); err != nil { | ||
| errs = append(errs, &errortypes.BadInput{Message: fmt.Sprintf("error unmarshalling impression ext: %v", err)}) | ||
| return nil, errs | ||
| } | ||
|
|
||
| var iqiyiExt openrtb_ext.ExtImpIqiyi | ||
| if err := jsonutil.Unmarshal(bidderExt.Bidder, &iqiyiExt); err != nil { | ||
| errs = append(errs, &errortypes.BadInput{Message: fmt.Sprintf("error unmarshalling Iqiyi bidder params: %v", err)}) | ||
| return nil, errs | ||
| } | ||
|
|
||
| requestCopy := *request | ||
| requestCopy.Imp = make([]openrtb2.Imp, len(request.Imp)) | ||
| copy(requestCopy.Imp, request.Imp) | ||
|
|
||
| for i := range requestCopy.Imp { | ||
| imp := &requestCopy.Imp[i] | ||
| if imp.Banner != nil { | ||
| banner := imp.Banner | ||
| if (banner.W == nil || banner.H == nil || *banner.W == minBannerSize || *banner.H == minBannerSize) && len(banner.Format) > defaultBannerFormatIndex { | ||
| bannerCopy := *banner | ||
| first := bannerCopy.Format[defaultBannerFormatIndex] | ||
| bannerCopy.W = &first.W | ||
| bannerCopy.H = &first.H | ||
| imp.Banner = &bannerCopy | ||
| } | ||
| } | ||
| if imp.BidFloorCur == "" && imp.BidFloor > minBannerSize { | ||
| imp.BidFloorCur = defaultCurrency | ||
| } | ||
| } | ||
|
|
||
| url, err := a.buildEndpointURL(&iqiyiExt) | ||
| if err != nil { | ||
| errs = append(errs, err) | ||
| return nil, errs | ||
| } | ||
|
|
||
| reqJSON, err := json.Marshal(&requestCopy) | ||
| if err != nil { | ||
| errs = append(errs, err) | ||
| return nil, errs | ||
| } | ||
|
|
||
| headers := http.Header{} | ||
| headers.Add("Content-Type", "application/json;charset=utf-8") | ||
| headers.Add("Accept", "application/json") | ||
|
|
||
| return []*adapters.RequestData{{ | ||
| Method: "POST", | ||
| Uri: url, | ||
| Body: reqJSON, | ||
| Headers: headers, | ||
| ImpIDs: openrtb_ext.GetImpIDs(requestCopy.Imp), | ||
| }}, nil | ||
| } | ||
|
|
||
| func (a *adapter) MakeBids(internalRequest *openrtb2.BidRequest, externalRequest *adapters.RequestData, response *adapters.ResponseData) (*adapters.BidderResponse, []error) { | ||
| if response.StatusCode == http.StatusNoContent { | ||
| return nil, nil | ||
| } | ||
|
|
||
| if response.StatusCode != http.StatusOK { | ||
| return nil, []error{&errortypes.BadServerResponse{ | ||
| Message: fmt.Sprintf("Unexpected http status code: %d", response.StatusCode), | ||
| }} | ||
| } | ||
|
|
||
| var serverBidResponse openrtb2.BidResponse | ||
| if err := jsonutil.Unmarshal(response.Body, &serverBidResponse); err != nil { | ||
| return nil, []error{err} | ||
| } | ||
|
|
||
| bidResponse := adapters.NewBidderResponseWithBidsCapacity(1) | ||
| bidResponse.Currency = selectCurrency(internalRequest, &serverBidResponse) | ||
|
|
||
| for _, seatbid := range serverBidResponse.SeatBid { | ||
| for i := range seatbid.Bid { | ||
| mediaType, err := getMediaTypeForImp(seatbid.Bid[i]) | ||
| if err != nil { | ||
| return nil, []error{err} | ||
| } | ||
| bidResponse.Bids = append(bidResponse.Bids, &adapters.TypedBid{ | ||
| Bid: &seatbid.Bid[i], | ||
| BidType: mediaType, | ||
| BidVideo: getBidVideo(&seatbid.Bid[i], mediaType), | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| return bidResponse, nil | ||
| } | ||
|
|
||
| func getMediaTypeForImp(bid openrtb2.Bid) (openrtb_ext.BidType, error) { | ||
| switch bid.MType { | ||
| case openrtb2.MarkupBanner: | ||
| return openrtb_ext.BidTypeBanner, nil | ||
| case openrtb2.MarkupVideo: | ||
| return openrtb_ext.BidTypeVideo, nil | ||
| case openrtb2.MarkupNative: | ||
| return openrtb_ext.BidTypeNative, nil | ||
| default: | ||
| return "", &errortypes.BadServerResponse{ | ||
| Message: fmt.Sprintf("Unsupported mtype %d for bid %s", bid.MType, bid.ID), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func getBidVideo(bid *openrtb2.Bid, bidType openrtb_ext.BidType) *openrtb_ext.ExtBidPrebidVideo { | ||
| if bidType != openrtb_ext.BidTypeVideo { | ||
| return nil | ||
| } | ||
| bidVideo := openrtb_ext.ExtBidPrebidVideo{} | ||
| if len(bid.Cat) > 0 { | ||
| bidVideo.PrimaryCategory = bid.Cat[0] | ||
| } | ||
| if bid.Dur > 0 { | ||
| bidVideo.Duration = int(bid.Dur) | ||
| } | ||
| return &bidVideo | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nitpick: Use
adapters.IsResponseStatusCodeNoContentandadapters.CheckResponseStatusCodeForErrorsfor checking errors