-
Notifications
You must be signed in to change notification settings - Fork 26
/
HousesMapping.go
76 lines (61 loc) · 1.87 KB
/
HousesMapping.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
76
package main
import (
"encoding/json"
"log"
"net/http"
"time"
"github.com/go-resty/resty/v2"
)
var (
TibiadataHousesMapping HousesMapping
)
type AssetsHouse struct {
HouseID int `json:"house_id"`
Town string `json:"town"`
HouseType string `json:"type"`
}
type HousesMapping struct {
Houses []AssetsHouse `json:"houses"`
}
// TibiaDataHousesMappingInitiator func - used to load data from local JSON file
func TibiaDataHousesMappingInitiator() {
// Setting up resty client
client := resty.New()
// Set client timeout and retry
client.SetTimeout(5 * time.Second)
client.SetRetryCount(2)
// Set headers for all requests
client.SetHeaders(map[string]string{
"Content-Type": "application/json",
"User-Agent": TibiadataUserAgent,
})
// Enabling Content length value for all request
client.SetContentLength(true)
// Disable redirection of client (so we skip parsing maintenance page)
client.SetRedirectPolicy(resty.NoRedirectPolicy())
TibiadataAssetsURL := "https://raw.githubusercontent.com/TibiaData/tibiadata-api-assets/main/data/houses_mapping.json"
res, err := client.R().Get(TibiadataAssetsURL)
switch res.StatusCode() {
case http.StatusOK:
// adding response into the data field
data := HousesMapping{}
err = json.Unmarshal([]byte(res.Body()), &data)
if err != nil {
log.Println("[error] TibiaData API failed to parse content from houses_mapping.json")
} else {
// storing data so it's accessible from other places
TibiadataHousesMapping = data
}
default:
log.Printf("[error] TibiaData API failed to load houses mapping. %s", err)
}
}
// TibiaDataHousesMapResolver func - used to return both town and type
func TibiaDataHousesMapResolver(houseid int) (town string, housetype string) {
for _, value := range TibiadataHousesMapping.Houses {
if houseid == value.HouseID {
return value.Town, value.HouseType
}
}
return "", ""
}