1
|
|
|
package main |
2
|
|
|
|
3
|
|
|
import ( |
4
|
|
|
"encoding/json" |
5
|
|
|
"errors" |
6
|
|
|
"fmt" |
7
|
|
|
|
8
|
|
|
"github.com/mkideal/cli" |
9
|
|
|
|
10
|
|
|
"github.com/evalphobia/minfraud-api-go/config" |
11
|
|
|
"github.com/evalphobia/minfraud-api-go/minfraud" |
12
|
|
|
) |
13
|
|
|
|
14
|
|
|
const ( |
15
|
|
|
commandScore = "score" |
16
|
|
|
commandInsights = "insights" |
17
|
|
|
commandFactors = "factors" |
18
|
|
|
) |
19
|
|
|
|
20
|
|
|
var validCommands = map[string]struct{}{ |
21
|
|
|
commandScore: {}, |
22
|
|
|
commandInsights: {}, |
23
|
|
|
commandFactors: {}, |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
// parameters of 'single' command. |
27
|
|
|
type singleT struct { |
28
|
|
|
cli.Helper |
29
|
|
|
Command string `cli:"*c,command" usage:"set type of api [score, insights, factors] --command='score'"` |
30
|
|
|
IPAddress string `cli:"i,ipaddr" usage:"input ip address --ipaddr='8.8.8.8'"` |
31
|
|
|
Email string `cli:"e,email" usage:"input email address --email='[email protected]'"` |
32
|
|
|
Debug bool `cli:"debug" usage:"set if you use HTTP debug feature --debug"` |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
func (a *singleT) Validate(ctx *cli.Context) error { |
36
|
|
|
if _, ok := validCommands[a.Command]; !ok { |
37
|
|
|
keys := make([]string, 0, len(validCommands)) |
38
|
|
|
for k := range validCommands { |
39
|
|
|
keys = append(keys, k) |
40
|
|
|
} |
41
|
|
|
return fmt.Errorf("command should be one of the %v", keys) |
42
|
|
|
} |
43
|
|
|
if a.IPAddress == "" && a.Email == "" { |
44
|
|
|
return errors.New("you must set --ipaddr or --email") |
45
|
|
|
} |
46
|
|
|
return nil |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
var singleC = &cli.Command{ |
50
|
|
|
Name: "single", |
51
|
|
|
Desc: "Exec single api call for minFraud API", |
52
|
|
|
Argv: func() interface{} { return new(singleT) }, |
53
|
|
|
Fn: execSingle, |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
func execSingle(ctx *cli.Context) error { |
57
|
|
|
argv := ctx.Argv().(*singleT) |
58
|
|
|
|
59
|
|
|
r := newSingleRunner(*argv) |
60
|
|
|
return r.Run() |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
type SingleRunner struct { |
64
|
|
|
// parameters |
65
|
|
|
Command string |
66
|
|
|
IPAddress string |
67
|
|
|
Email string |
68
|
|
|
Debug bool |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
func newSingleRunner(p singleT) SingleRunner { |
72
|
|
|
return SingleRunner{ |
73
|
|
|
Command: p.Command, |
74
|
|
|
IPAddress: p.IPAddress, |
75
|
|
|
Email: p.Email, |
76
|
|
|
Debug: p.Debug, |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
func (r *SingleRunner) Run() error { |
81
|
|
|
conf := config.Config{ |
82
|
|
|
Debug: r.Debug, |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
svc, err := minfraud.New(conf) |
86
|
|
|
if err != nil { |
87
|
|
|
return err |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
resp, err := r.execAPI(svc) |
91
|
|
|
if err != nil { |
92
|
|
|
return err |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
b, err := json.Marshal(resp) |
96
|
|
|
if err != nil { |
97
|
|
|
return err |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
// just print response in json format |
101
|
|
|
fmt.Printf("%s\n", string(b)) |
102
|
|
|
return nil |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
func (r *SingleRunner) execAPI(svc *minfraud.MinFraud) (minfraud.APIResponse, error) { |
106
|
|
|
params := minfraud.BaseRequest{ |
107
|
|
|
Device: &minfraud.DeviceData{ |
108
|
|
|
IPAddress: r.IPAddress, |
109
|
|
|
}, |
110
|
|
|
Email: &minfraud.EmailData{ |
111
|
|
|
Address: r.Email, |
112
|
|
|
}, |
113
|
|
|
} |
114
|
|
|
return execAPI(svc, params, r.Command) |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
func execAPI(svc *minfraud.MinFraud, req minfraud.BaseRequest, command string) (resp minfraud.APIResponse, err error) { |
118
|
|
|
switch command { |
119
|
|
|
case commandScore: |
120
|
|
|
r, err := svc.Score(req) |
121
|
|
|
if err != nil { |
122
|
|
|
return resp, err |
123
|
|
|
} |
124
|
|
|
resp = r.APIResponse() |
125
|
|
|
case commandInsights: |
126
|
|
|
r, err := svc.Insights(req) |
127
|
|
|
if err != nil { |
128
|
|
|
return resp, err |
129
|
|
|
} |
130
|
|
|
resp = r.APIResponse() |
131
|
|
|
case commandFactors: |
132
|
|
|
r, err := svc.Factors(req) |
133
|
|
|
if err != nil { |
134
|
|
|
return resp, err |
135
|
|
|
} |
136
|
|
|
resp = r.APIResponse() |
137
|
|
|
} |
138
|
|
|
return resp, nil |
139
|
|
|
} |
140
|
|
|
|