@@ -28,10 +28,41 @@ var (
2828 // TibiaData app resty vars
2929 TibiaDataUserAgent , TibiaDataProxyDomain string
3030
31+ // tibiaDataClient is a shared resty client reused across all requests
32+ tibiaDataClient * resty.Client
33+
3134 // ErrorNotFound will be returned if the requests ends up in a 404
3235 ErrorNotFound = errors .New ("page not found" )
3336)
3437
38+ // initTibiaDataClient creates the shared resty client with static configuration.
39+ // Must be called after TibiaDataUserAgent is set.
40+ func initTibiaDataClient () {
41+ tibiaDataClient = resty .New ()
42+
43+ // Set Debug if enabled by TibiaDataDebug var
44+ if TibiaDataDebug {
45+ tibiaDataClient .SetDebug (true )
46+ tibiaDataClient .EnableTrace ()
47+ }
48+
49+ // Set client timeout and retry
50+ tibiaDataClient .SetTimeout (5 * time .Second )
51+ tibiaDataClient .SetRetryCount (2 )
52+
53+ // Set headers for all requests
54+ tibiaDataClient .SetHeaders (map [string ]string {
55+ "Content-Type" : "application/json" ,
56+ "User-Agent" : TibiaDataUserAgent ,
57+ })
58+
59+ // Enabling Content length value for all request
60+ tibiaDataClient .SetContentLength (true )
61+
62+ // Disable redirection of client (so we skip parsing maintenance page)
63+ tibiaDataClient .SetRedirectPolicy (resty .NoRedirectPolicy ())
64+ }
65+
3566// DebugOutInformation wraps OutInformation with some debug info
3667type DebugOutInformation struct {
3768 Information Information `json:"information"`
@@ -1197,31 +1228,6 @@ func TibiaDataUserAgentGenerator(version int) string {
11971228
11981229// TibiaDataHTMLDataCollector func
11991230func TibiaDataHTMLDataCollector (TibiaDataRequest TibiaDataRequestStruct ) (string , error ) {
1200- // Setting up resty client
1201- client := resty .New ()
1202-
1203- // Set Debug if enabled by TibiaDataDebug var
1204- if TibiaDataDebug {
1205- client .SetDebug (true )
1206- client .EnableTrace ()
1207- }
1208-
1209- // Set client timeout and retry
1210- client .SetTimeout (5 * time .Second )
1211- client .SetRetryCount (2 )
1212-
1213- // Set headers for all requests
1214- client .SetHeaders (map [string ]string {
1215- "Content-Type" : "application/json" ,
1216- "User-Agent" : TibiaDataUserAgent ,
1217- })
1218-
1219- // Enabling Content length value for all request
1220- client .SetContentLength (true )
1221-
1222- // Disable redirection of client (so we skip parsing maintenance page)
1223- client .SetRedirectPolicy (resty .NoRedirectPolicy ())
1224-
12251231 // Replace domain with proxy if env TIBIADATA_PROXY set
12261232 if TibiaDataProxyDomain != "" {
12271233 TibiaDataRequest .URL = strings .ReplaceAll (TibiaDataRequest .URL , "https://www.tibia.com/" , TibiaDataProxyDomain )
@@ -1236,11 +1242,11 @@ func TibiaDataHTMLDataCollector(TibiaDataRequest TibiaDataRequestStruct) (string
12361242
12371243 switch TibiaDataRequest .Method {
12381244 case resty .MethodPost :
1239- res , err = client .R ().
1245+ res , err = tibiaDataClient .R ().
12401246 SetFormData (TibiaDataRequest .FormData ).
12411247 Post (TibiaDataRequest .URL )
12421248 default :
1243- res , err = client .R ().Get (TibiaDataRequest .URL )
1249+ res , err = tibiaDataClient .R ().Get (TibiaDataRequest .URL )
12441250 }
12451251
12461252 if TibiaDataDebug {
0 commit comments