forked from linkpoolio/bridges
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
executable file
·61 lines (56 loc) · 1.4 KB
/
main.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
package main
import (
"errors"
"github.com/linkpoolio/bridges"
"net/http"
"os"
"strings"
)
// WolframAlpha is a bridge that queries the WolframAlpha short answers API
// https://products.wolframalpha.com/short-answers-api/documentation/
//
// This providers a natural way for a Chainlink end user to query data.
// For example:
//
// {
// "query": "How far is Los Angeles from New York?",
// "index": 1
// }
//
// Returning:
//
// {
// "full": "about 2464 miles",
// "result": "2464",
// "unit": "miles"
// }
type WolframAlpha struct{}
// Run implements Bridge Run for querying the Wolfram short answers API
func (cc *WolframAlpha) Run(h *bridges.Helper) (interface{}, error) {
b, err := h.HTTPCallRawWithOpts(
http.MethodGet,
"https://api.wolframalpha.com/v1/result",
bridges.CallOpts{
Auth: bridges.NewAuth(bridges.AuthParam, "appid", os.Getenv("APP_ID")),
Query: map[string]interface{}{
"i": h.GetParam("query"),
},
},
)
val := strings.Split(string(b), " ")
i := h.GetIntParam("index")
if i > int64(len(val)-1) {
return nil, errors.New("Invalid index")
}
return map[string]string{"result": val[i], "full": string(b), "unit": val[len(val)-1]}, err
}
// Opts is the bridge.Bridge implementation
func (cc *WolframAlpha) Opts() *bridges.Opts {
return &bridges.Opts{
Name: "WolframAlpha",
Lambda: true,
}
}
func main() {
bridges.NewServer(&WolframAlpha{}).Start(8080)
}