-
Notifications
You must be signed in to change notification settings - Fork 18
/
ethicalads.go
64 lines (57 loc) · 1.6 KB
/
ethicalads.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
package main
import (
"bytes"
"fmt"
"net/http"
"os"
)
type EthicalAdsAd struct {
Ad
Pixel []string
ReferralLink string
}
type EthicalAdsResponse struct {
Id string
Body string
Image string
Link string
ViewUrl string `json:"view_url"`
Nonce string
}
var hystrixEa = "EthicalAds"
var ethicaladsToken = os.Getenv("ETHICALADS_TOKEN")
var fetchEthicalAds = func(r *http.Request, keywords []string) (*EthicalAdsAd, error) {
keywordsString := ""
for i, keyword := range keywords {
if i > 0 {
keywordsString += ", "
}
keywordsString += fmt.Sprintf("\"%s\"", keyword)
}
ip := getIpAddress(r)
ua := r.UserAgent()
var body = []byte(`{ "publisher": "dailydev", "placements": [{ "div_id": "ad-div-1", "ad_type": "image-v1" }], "campaign_types": ["paid"], "user_ip": "` + ip + `", "user_ua": "` + ua + `", "keywords": [` + keywordsString + `] }`)
var res EthicalAdsResponse
req, _ := http.NewRequest("POST", "https://server.ethicalads.io/api/v1/decision/", bytes.NewBuffer(body))
req.Header.Set("User-Agent", "daily.dev ad server")
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", "Token "+ethicaladsToken)
req = req.WithContext(r.Context())
err := getJsonHystrix(hystrixEa, req, &res, true)
if err != nil {
return nil, err
}
if res.Body == "" {
return nil, nil
}
ad := EthicalAdsAd{}
ad.Company = "EthicalAds"
ad.Description = res.Body
ad.Link = res.Link
ad.Source = "EthicalAds"
ad.Pixel = []string{res.ViewUrl}
ad.Image = res.Image
ad.ReferralLink = "https://www.ethicalads.io/?ref=dailydev"
ad.ProviderId = "ethical"
return &ad, nil
}