@@ -2,8 +2,11 @@ package main
2
2
3
3
import (
4
4
"encoding/json"
5
- "io/ioutil"
6
5
"log"
6
+ "net/http"
7
+ "time"
8
+
9
+ "github.com/go-resty/resty/v2"
7
10
)
8
11
9
12
var (
@@ -21,22 +24,44 @@ type HousesMapping struct {
21
24
22
25
// TibiaDataHousesMappingInitiator func - used to load data from local JSON file
23
26
func TibiaDataHousesMappingInitiator () {
24
- // load content from file into variable file
25
- file , err := ioutil .ReadFile ("houses_mapping.json" )
26
27
27
- if err != nil {
28
- log .Println ("[error] TibiaData API failed to load content from houses_mapping.json" )
29
- } else {
30
- // loading json and mapping it into the data variable
28
+ // Setting up resty client
29
+ client := resty .New ()
30
+
31
+ // Set client timeout and retry
32
+ client .SetTimeout (5 * time .Second )
33
+ client .SetRetryCount (2 )
34
+
35
+ // Set headers for all requests
36
+ client .SetHeaders (map [string ]string {
37
+ "Content-Type" : "application/json" ,
38
+ "User-Agent" : TibiadataUserAgent ,
39
+ })
40
+
41
+ // Enabling Content length value for all request
42
+ client .SetContentLength (true )
43
+
44
+ // Disable redirection of client (so we skip parsing maintenance page)
45
+ client .SetRedirectPolicy (resty .NoRedirectPolicy ())
46
+
47
+ TibiadataAssetsURL := "https://raw.githubusercontent.com/TibiaData/tibiadata-api-assets/main/data/houses_mapping.json"
48
+ res , err := client .R ().Get (TibiadataAssetsURL )
49
+
50
+ switch res .StatusCode () {
51
+ case http .StatusOK :
52
+ // adding response into the data field
31
53
data := HousesMapping {}
32
- err = json .Unmarshal ([]byte (file ), & data )
54
+ err = json .Unmarshal ([]byte (res . Body () ), & data )
33
55
34
56
if err != nil {
35
57
log .Println ("[error] TibiaData API failed to parse content from houses_mapping.json" )
36
58
} else {
37
59
// storing data so it's accessible from other places
38
60
TibiadataHousesMapping = data
39
61
}
62
+
63
+ default :
64
+ log .Printf ("[error] TibiaData API failed to load houses mapping. %s" , err )
40
65
}
41
66
}
42
67
0 commit comments