forked from prebid/prebid-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdevice.go
123 lines (106 loc) · 3.81 KB
/
device.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
120
121
122
123
package openrtb_ext
import (
"encoding/json"
"errors"
"strconv"
"github.com/buger/jsonparser"
"github.com/prebid/prebid-server/errortypes"
)
// PrebidExtKey represents the prebid extension key used in requests
const PrebidExtKey = "prebid"
// PrebidExtBidderKey represents the field name within request.imp.ext.prebid reserved for bidder params.
const PrebidExtBidderKey = "bidder"
// ExtDevice defines the contract for bidrequest.device.ext
type ExtDevice struct {
// Attribute:
// atts
// Type:
// integer; optional - iOS Only
// Description:
// iOS app tracking authorization status.
// Extension Spec:
// https://github.com/InteractiveAdvertisingBureau/openrtb/blob/master/extensions/community_extensions/skadnetwork.md
ATTS *IOSAppTrackingStatus `json:"atts"`
// Attribute:
// prebid
// Type:
// object; optional
// Description:
// Prebid extensions for the Device object.
Prebid ExtDevicePrebid `json:"prebid"`
}
// IOSAppTrackingStatus describes the values for iOS app tracking authorization status.
type IOSAppTrackingStatus int
// Values of the IOSAppTrackingStatus enumeration.
const (
IOSAppTrackingStatusNotDetermined IOSAppTrackingStatus = 0
IOSAppTrackingStatusRestricted IOSAppTrackingStatus = 1
IOSAppTrackingStatusDenied IOSAppTrackingStatus = 2
IOSAppTrackingStatusAuthorized IOSAppTrackingStatus = 3
)
// IsKnownIOSAppTrackingStatus returns true if the value is a known iOS app tracking authorization status.
func IsKnownIOSAppTrackingStatus(v int64) bool {
switch IOSAppTrackingStatus(v) {
case IOSAppTrackingStatusNotDetermined:
return true
case IOSAppTrackingStatusRestricted:
return true
case IOSAppTrackingStatusDenied:
return true
case IOSAppTrackingStatusAuthorized:
return true
default:
return false
}
}
// ExtDevicePrebid defines the contract for bidrequest.device.ext.prebid
type ExtDevicePrebid struct {
Interstitial *ExtDeviceInt `json:"interstitial"`
}
// ExtDeviceInt defines the contract for bidrequest.device.ext.prebid.interstitial
type ExtDeviceInt struct {
MinWidthPerc int64 `json:"minwidtheperc"`
MinHeightPerc int64 `json:"minheightperc"`
}
func (edi *ExtDeviceInt) UnmarshalJSON(b []byte) error {
if len(b) == 0 {
return &errortypes.BadInput{Message: "request.device.ext.prebid.interstitial must have some data in it"}
}
if value, dataType, _, _ := jsonparser.Get(b, "minwidthperc"); dataType != jsonparser.Number {
return &errortypes.BadInput{Message: "request.device.ext.prebid.interstitial.minwidthperc must be a number between 0 and 100"}
} else {
perc, err := strconv.Atoi(string(value))
if err != nil || perc < 0 || perc > 100 {
return &errortypes.BadInput{Message: "request.device.ext.prebid.interstitial.minwidthperc must be a number between 0 and 100"}
}
edi.MinWidthPerc = int64(perc)
}
if value, dataType, _, _ := jsonparser.Get(b, "minheightperc"); dataType != jsonparser.Number {
return &errortypes.BadInput{Message: "request.device.ext.prebid.interstitial.minheightperc must be a number between 0 and 100"}
} else {
perc, err := strconv.Atoi(string(value))
if err != nil || perc < 0 || perc > 100 {
return &errortypes.BadInput{Message: "request.device.ext.prebid.interstitial.minheightperc must be a number between 0 and 100"}
}
edi.MinHeightPerc = int64(perc)
}
return nil
}
// ParseDeviceExtATTS parses the ATTS value from the request.device.ext OpenRTB field.
func ParseDeviceExtATTS(deviceExt json.RawMessage) (*IOSAppTrackingStatus, error) {
v, err := jsonparser.GetInt(deviceExt, "atts")
// node not found error
if err == jsonparser.KeyPathNotFoundError {
return nil, nil
}
// unexpected parse error
if err != nil {
return nil, err
}
// invalid value error
if !IsKnownIOSAppTrackingStatus(v) {
return nil, errors.New("invalid status")
}
status := IOSAppTrackingStatus(v)
return &status, nil
}