-
Notifications
You must be signed in to change notification settings - Fork 753
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a0cba28
commit d31c072
Showing
17 changed files
with
1,004 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,160 @@ | ||
package applogy | ||
|
||
import ( | ||
"encoding/json" | ||
"errors" | ||
"net/http" | ||
"strconv" | ||
|
||
"github.com/mxmCherry/openrtb" | ||
"github.com/prebid/prebid-server/adapters" | ||
"github.com/prebid/prebid-server/errortypes" | ||
"github.com/prebid/prebid-server/openrtb_ext" | ||
) | ||
|
||
type ApplogyAdapter struct { | ||
endpoint string | ||
} | ||
|
||
func (a *ApplogyAdapter) MakeRequests(request *openrtb.BidRequest, _ *adapters.ExtraRequestInfo) ([]*adapters.RequestData, []error) { | ||
headers := http.Header{} | ||
headers.Add("Content-Type", "application/json;charset=utf-8") | ||
headers.Add("Accept", "application/json") | ||
impressions := request.Imp | ||
result := make([]*adapters.RequestData, 0, len(impressions)) | ||
errs := make([]error, 0, len(impressions)) | ||
|
||
for i, impression := range impressions { | ||
if impression.Banner == nil && impression.Video == nil && impression.Native == nil { | ||
errs = append(errs, &errortypes.BadInput{ | ||
Message: "Applogy only supports banner, video or native ads", | ||
}) | ||
continue | ||
} | ||
if impression.Banner != nil { | ||
banner := impression.Banner | ||
if banner.W == nil || banner.H == nil || *banner.W == 0 || *banner.H == 0 { | ||
if len(banner.Format) == 0 { | ||
errs = append(errs, &errortypes.BadInput{ | ||
Message: "banner size information missing", | ||
}) | ||
continue | ||
} | ||
format := banner.Format[0] | ||
banner.W = &format.W | ||
banner.H = &format.H | ||
} | ||
} | ||
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.ExtImpApplogy | ||
err = json.Unmarshal(bidderExt.Bidder, &impressionExt) | ||
if err != nil { | ||
errs = append(errs, err) | ||
continue | ||
} | ||
if impressionExt.Token == "" { | ||
errs = append(errs, errors.New("Applogy token required")) | ||
continue | ||
} | ||
request.Imp = impressions[i : i+1] | ||
body, err := json.Marshal(request) | ||
if err != nil { | ||
errs = append(errs, err) | ||
continue | ||
} | ||
result = append(result, &adapters.RequestData{ | ||
Method: "POST", | ||
Uri: a.endpoint + "/" + impressionExt.Token, | ||
Body: body, | ||
Headers: headers, | ||
}) | ||
} | ||
|
||
request.Imp = impressions | ||
|
||
if len(result) == 0 { | ||
return nil, errs | ||
} | ||
return result, errs | ||
} | ||
|
||
func (a *ApplogyAdapter) MakeBids(request *openrtb.BidRequest, _ *adapters.RequestData, responseData *adapters.ResponseData) (*adapters.BidderResponse, []error) { | ||
var errs []error | ||
|
||
switch responseData.StatusCode { | ||
case http.StatusNoContent: | ||
return nil, nil | ||
case http.StatusBadRequest: | ||
return nil, []error{&errortypes.BadInput{ | ||
Message: "unexpected status code: " + strconv.Itoa(responseData.StatusCode), | ||
}} | ||
case http.StatusOK: | ||
break | ||
default: | ||
return nil, []error{&errortypes.BadServerResponse{ | ||
Message: "unexpected status code: " + strconv.Itoa(responseData.StatusCode), | ||
}} | ||
} | ||
|
||
var bidResponse openrtb.BidResponse | ||
err := json.Unmarshal(responseData.Body, &bidResponse) | ||
if err != nil { | ||
return nil, []error{&errortypes.BadServerResponse{ | ||
Message: err.Error(), | ||
}} | ||
} | ||
|
||
response := adapters.NewBidderResponseWithBidsCapacity(len(request.Imp)) | ||
|
||
for _, seatBid := range bidResponse.SeatBid { | ||
for _, bid := range seatBid.Bid { | ||
bid := bid // pin https://github.com/kyoh86/scopelint#whats-this | ||
var bidType openrtb_ext.BidType | ||
for _, impression := range request.Imp { | ||
if impression.ID != bid.ImpID { | ||
continue | ||
} | ||
switch { | ||
case impression.Banner != nil: | ||
bidType = openrtb_ext.BidTypeBanner | ||
case impression.Video != nil: | ||
bidType = openrtb_ext.BidTypeVideo | ||
case impression.Native != nil: | ||
bidType = openrtb_ext.BidTypeNative | ||
} | ||
break | ||
} | ||
if bidType == "" { | ||
errs = append(errs, &errortypes.BadServerResponse{ | ||
Message: "ignoring bid id=" + bid.ID + ", request doesn't contain any valid impression with id=" + bid.ImpID, | ||
}) | ||
continue | ||
} | ||
response.Bids = append(response.Bids, &adapters.TypedBid{ | ||
Bid: &bid, | ||
BidType: bidType, | ||
}) | ||
} | ||
} | ||
|
||
return response, errs | ||
} | ||
|
||
func NewApplogyBidder(endpoint string) *ApplogyAdapter { | ||
return &ApplogyAdapter{ | ||
endpoint: endpoint, | ||
} | ||
} |
This file contains 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,11 @@ | ||
package applogy | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/prebid/prebid-server/adapters/adapterstest" | ||
) | ||
|
||
func TestJsonSamples(t *testing.T) { | ||
adapterstest.RunJSONBidderTest(t, "applogytest", NewApplogyBidder("http://example.com/prebid")) | ||
} |
170 changes: 170 additions & 0 deletions
170
adapters/applogy/applogytest/exemplary/simple-banner.json
This file contains 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,170 @@ | ||
{ | ||
"mockBidRequest": { | ||
"id": "test-request-id", | ||
"imp": [{ | ||
"id": "test-impression-id-1", | ||
"banner": { | ||
"format": [{ | ||
"h": 250, | ||
"w": 300 | ||
}] | ||
}, | ||
"ext": { | ||
"bidder": { | ||
"token": "test-token-1" | ||
} | ||
} | ||
}, { | ||
"id": "test-impression-id-2", | ||
"banner": { | ||
"w": 300, | ||
"h": 250 | ||
}, | ||
"ext": { | ||
"bidder": { | ||
"token": "test-token-1" | ||
} | ||
} | ||
}, { | ||
"id": "test-impression-id-3", | ||
"banner": { | ||
"w": 300, | ||
"h": 250 | ||
}, | ||
"ext": { | ||
"bidder": { | ||
"token": "test-token-2" | ||
} | ||
} | ||
}] | ||
}, | ||
"httpCalls": [{ | ||
"expectedRequest": { | ||
"uri": "http://example.com/prebid/test-token-1", | ||
"body": { | ||
"id": "test-request-id", | ||
"imp": [{ | ||
"id": "test-impression-id-1", | ||
"banner": { | ||
"format": [{ | ||
"h": 250, | ||
"w": 300 | ||
}], | ||
"h": 250, | ||
"w": 300 | ||
}, | ||
"ext": { | ||
"bidder": { | ||
"token": "test-token-1" | ||
} | ||
} | ||
}] | ||
} | ||
}, | ||
"mockResponse": { | ||
"status": 200, | ||
"body": { | ||
"id": "test-response-id", | ||
"seatbid": [{ | ||
"bid": [{ | ||
"id": "test-bid-id-1", | ||
"impid": "test-impression-id-1", | ||
"price": 1 | ||
}] | ||
}] | ||
} | ||
} | ||
}, { | ||
"expectedRequest": { | ||
"uri": "http://example.com/prebid/test-token-1", | ||
"body": { | ||
"id": "test-request-id", | ||
"imp": [{ | ||
"id": "test-impression-id-2", | ||
"banner": { | ||
"h": 250, | ||
"w": 300 | ||
}, | ||
"ext": { | ||
"bidder": { | ||
"token": "test-token-1" | ||
} | ||
} | ||
}] | ||
} | ||
}, | ||
"mockResponse": { | ||
"status": 200, | ||
"body": { | ||
"id": "test-response-id-2", | ||
"seatbid": [{ | ||
"bid": [{ | ||
"id": "test-bid-id-2", | ||
"impid": "test-impression-id-2", | ||
"price": 2 | ||
}] | ||
}] | ||
} | ||
} | ||
}, { | ||
"expectedRequest": { | ||
"uri": "http://example.com/prebid/test-token-2", | ||
"body": { | ||
"id": "test-request-id", | ||
"imp": [{ | ||
"id": "test-impression-id-3", | ||
"banner": { | ||
"h": 250, | ||
"w": 300 | ||
}, | ||
"ext": { | ||
"bidder": { | ||
"token": "test-token-2" | ||
} | ||
} | ||
}] | ||
} | ||
}, | ||
"mockResponse": { | ||
"status": 200, | ||
"body": { | ||
"id": "test-response-id-3", | ||
"seatbid": [{ | ||
"bid": [{ | ||
"id": "test-bid-id-3", | ||
"impid": "test-impression-id-3", | ||
"price": 3 | ||
}] | ||
}] | ||
} | ||
} | ||
}], | ||
"expectedBidResponses": [{ | ||
"bids": [{ | ||
"bid": { | ||
"id": "test-bid-id-1", | ||
"impid": "test-impression-id-1", | ||
"price": 1 | ||
}, | ||
"type": "banner" | ||
}] | ||
}, { | ||
"bids": [{ | ||
"bid": { | ||
"id": "test-bid-id-2", | ||
"impid": "test-impression-id-2", | ||
"price": 2 | ||
}, | ||
"type": "banner" | ||
}] | ||
}, { | ||
"bids": [{ | ||
"bid": { | ||
"id": "test-bid-id-3", | ||
"impid": "test-impression-id-3", | ||
"price": 3 | ||
}, | ||
"type": "banner" | ||
}] | ||
}] | ||
} |
Oops, something went wrong.