forked from bsm/openrtb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
request.go
119 lines (104 loc) · 3.48 KB
/
request.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package openrtb
import (
"encoding/json"
"errors"
"io"
)
// The top-level bid request object contains a globally unique bid request or auction ID. This "id"
// attribute is required as is at least one "imp" (i.e., impression) object. Other attributes are
// optional since an exchange may establish default values.
type Request struct {
Id *string `json:"id"` // Unique ID of the bid request
Imp []Impression `json:"imp,omitempty"`
Site *Site `json:"site,omitempty"`
App *App `json:"app,omitempty"`
Device *Device `json:"device,omitempty"`
User *User `json:"user,omitempty"`
At *int `json:"at"` // Auction type, Default: 2 ("1": first price auction, "2": then second price auction)
Tmax *int `json:"tmax,omitempty"` // Maximum amount of time in milliseconds to submit a bid
Wseat []string `json:"wseat,omitempty"` // Array of buyer seats allowed to bid on this auction
Allimps *int `json:"allimps,omitempty"` // Flag to indicate whether exchange can verify that all impressions offered represent all of the impressions available in context, Default: 0
Cur []string `json:"cur,omitempty"` // Array of allowed currencies
Bcat []string `json:"bcat,omitempty"` // Blocked Advertiser Categories.
Badv []string `json:"badv,omitempty"` // Array of strings of blocked toplevel domains of advertisers
Regs *Regulations `json:"regs,omitempty"`
Ext Extensions `json:"ext,omitempty"`
Pmp *Pmp `json:"pmp,omitempty"` // DEPRECATED: kept for backwards compatibility
}
// Parses an OpenRTB Request struct from an io.Reader
// Optionally validates and applies defaults to the Request object (recommended)
func ParseRequest(reader io.Reader) (req *Request, err error) {
dec := json.NewDecoder(reader)
if err = dec.Decode(&req); err != nil {
return nil, err
}
return req.WithDefaults(), nil
}
// Parses an OpenRTB Request from bytes
// Optionally validates and applies defaults to the Request object (recommended)
func ParseRequestBytes(data []byte) (req *Request, err error) {
if err = json.Unmarshal(data, &req); err != nil {
return nil, err
}
return req.WithDefaults(), nil
}
// Validation errors
var (
ErrInvalidReqID = errors.New("openrtb parse: request ID missing")
ErrInvalidReqImp = errors.New("openrtb parse: no impressions")
ErrInvalidReqSrc = errors.New("openrtb parse: request has site and app")
)
// Validates the request
func (req *Request) Valid() (bool, error) {
if req.Id == nil {
return false, ErrInvalidReqID
} else if len(req.Imp) == 0 {
return false, ErrInvalidReqImp
} else if req.Site != nil && req.App != nil {
return false, ErrInvalidReqSrc
}
for _, imp := range req.Imp {
if ok, err := (&imp).Valid(); !ok {
return ok, err
}
}
return true, nil
}
// Applies defaults
func (req *Request) WithDefaults() *Request {
if req.At == nil {
req.At = new(int)
*req.At = 2
}
if req.Site != nil {
req.Site.WithDefaults()
}
if req.App != nil {
req.App.WithDefaults()
}
if req.Device != nil {
req.Device.WithDefaults()
}
for i, imp := range req.Imp {
req.Imp[i] = *(&imp).WithDefaults()
}
return req
}
// Set the ID
func (req *Request) SetId(id string) *Request {
if req.Id == nil {
req.Id = new(string)
}
*req.Id = id
return req
}
// Set the Site
func (req *Request) SetSite(site *Site) *Request {
req.Site = site
return req
}
// Set the App
func (req *Request) SetApp(app *App) *Request {
req.App = app
return req
}