|
| 1 | +package openrtb |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "io" |
| 6 | +) |
| 7 | + |
| 8 | +type NativeAdm struct { |
| 9 | + Native *NativeResponse |
| 10 | +} |
| 11 | + |
| 12 | +type NativeResponse struct { |
| 13 | + Ver *string `json:"ver,omitempty"` // Version of the Native Markup version in use |
| 14 | + Assets []ResponseAsset `json:"assets"` // Array of Asset Objects |
| 15 | + Link *Link `json:"link"` |
| 16 | + Imptrackers []string `json:"imptrackers,omitempty"` |
| 17 | + Jstracker *string `json:"jstracker, omitempty"` |
| 18 | + Ext Extensions `json:"ext,omitempty"` |
| 19 | +} |
| 20 | + |
| 21 | +type ResponseAsset struct { |
| 22 | + Id *int `json:"id"` // Unique asset ID, assigned by exchange |
| 23 | + Required *int `json:"required,omitempty"` // Set to 1 if asset is required |
| 24 | + Title *ResponseTitle `json:"title,omitempty"` // Title object for title assets |
| 25 | + Img *ResponseImg `json:"img,omitempty"` // Image object for image assets |
| 26 | + Video *ResponseVideo `json:"video,omitempty"` // Video object for video assets |
| 27 | + Data *ResponseData `json:"data,omitempty"` // Data object for ratings, price, etc. |
| 28 | + Link *Link `json:"link"` |
| 29 | + Ext Extensions `json:"ext,omitempty"` |
| 30 | +} |
| 31 | + |
| 32 | +type Link struct { |
| 33 | + Url *string `json:"url"` |
| 34 | + Clicktrackers []string `json:"clicktrackers,omitempty"` |
| 35 | + Fallback *string `json:"fallback,omitempty"` |
| 36 | + Ext Extensions `json:"ext,omitempty"` |
| 37 | +} |
| 38 | + |
| 39 | +type ResponseTitle struct { |
| 40 | + Text *string `json:"text"` |
| 41 | + Ext Extensions `json:"ext,omitempty"` |
| 42 | +} |
| 43 | + |
| 44 | +type ResponseImg struct { |
| 45 | + Url *string `json:"url"` |
| 46 | + W *int `json:"w,omitempty"` // Width |
| 47 | + H *int `json:"h,omitempty"` // Height |
| 48 | + Ext Extensions `json:"ext,omitempty"` |
| 49 | +} |
| 50 | + |
| 51 | +type ResponseData struct { |
| 52 | + Label *string `json:"label,omitempty"` |
| 53 | + Value *string `json:"value"` |
| 54 | + Ext Extensions `json:"ext,omitempty"` |
| 55 | +} |
| 56 | + |
| 57 | +type ResponseVideo struct { |
| 58 | + Vasttag *string `json:"vasttag"` |
| 59 | +} |
| 60 | + |
| 61 | +// Set the Asset |
| 62 | +func (nativeResponse *NativeResponse) SetAssets(a *ResponseAsset) *NativeResponse { |
| 63 | + nativeResponse.Assets = append(nativeResponse.Assets, *a) |
| 64 | + return nativeResponse |
| 65 | +} |
| 66 | + |
| 67 | +//Parses an OpenRTB Native Response from an io.Reader |
| 68 | +func ParseNativeAdm(reader io.Reader) (adm *NativeAdm, err error) { |
| 69 | + dec := json.NewDecoder(reader) |
| 70 | + if err = dec.Decode(&adm); err != nil { |
| 71 | + return nil, err |
| 72 | + } |
| 73 | + adm.Native = adm.Native.WithDefaults() |
| 74 | + return adm, nil |
| 75 | +} |
| 76 | + |
| 77 | +//Parses an OpenRTB Native Response from bytes |
| 78 | +func ParseNativeAdmBytes(data []byte) (adm *NativeAdm, err error) { |
| 79 | + if err = json.Unmarshal(data, &adm); err != nil { |
| 80 | + return nil, err |
| 81 | + } |
| 82 | + adm.Native = adm.Native.WithDefaults() |
| 83 | + return adm, nil |
| 84 | +} |
| 85 | + |
| 86 | +// Applies NativeResponse defaults |
| 87 | +func (resp *NativeResponse) WithDefaults() *NativeResponse { |
| 88 | + if resp.Ver == nil { |
| 89 | + resp.Ver = new(string) |
| 90 | + *resp.Ver = "1" |
| 91 | + } |
| 92 | + for id, asset := range resp.Assets { |
| 93 | + resp.Assets[id] = *asset.WithDefaults() |
| 94 | + } |
| 95 | + return resp |
| 96 | +} |
| 97 | + |
| 98 | +// Applies ResponseAsset defaults |
| 99 | +func (asset *ResponseAsset) WithDefaults() *ResponseAsset { |
| 100 | + if asset.Required == nil { |
| 101 | + asset.Required = new(int) |
| 102 | + *asset.Required = 0 |
| 103 | + } |
| 104 | + return asset |
| 105 | +} |
0 commit comments