-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
75 lines (56 loc) · 1.6 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"sort"
log "github.com/sirupsen/logrus"
adapter "github.com/bancodobrasil/featws-resolver-adapter-go"
"github.com/bancodobrasil/featws-resolver-adapter-go/types"
"github.com/bancodobrasil/featws-resolver-climatempo/config"
)
var cfg = config.Config{}
func main() {
err := config.LoadConfig(&cfg)
if err != nil {
log.Fatalf("Não foi possível carregar as configurações: %s\n", err)
}
adapter.Run(resolver, adapter.Config{
Port: cfg.Port,
})
}
func resolver(resolveInput types.ResolveInput, output *types.ResolveOutput) {
sort.Strings(resolveInput.Load)
if contains(resolveInput.Load, "weather") {
resolveWeather(resolveInput, output)
}
}
func resolveWeather(resolveInput types.ResolveInput, output *types.ResolveOutput) {
locale, ok := resolveInput.Context["locale"]
if !ok {
output.Errors["weather"] = "The context 'locale' maybe be bounded for resolve 'weather'"
} else {
serviceLink := fmt.Sprintf("http://apiadvisor.climatempo.com.br/api/v1/weather/locale/%s/current?token=%s", locale, cfg.Token)
resp, err := http.Get(serviceLink)
if err != nil {
log.Fatalln(err)
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatalln(err)
}
result := make(map[string]interface{})
json.Unmarshal(body, &result)
err2, ok := result["error"]
if ok && err2.(bool) {
output.Errors["weather"] = result
} else {
output.Context["weather"] = result
}
}
}
func contains(s []string, searchterm string) bool {
i := sort.SearchStrings(s, searchterm)
return i < len(s) && s[i] == searchterm
}