@@ -12,31 +12,58 @@ class Guzzle
12
12
const ENDPOINT_DEV = 'https://xivapi.local ' ;
13
13
const TIMEOUT = 10.0 ;
14
14
const VERIFY = false ;
15
-
15
+
16
+ /** @var Client */
17
+ private static $ client = null ;
18
+ /** @var bool */
19
+ private static $ async = false ;
20
+ /** @var array */
16
21
private static $ options = [];
22
+
23
+ /**
24
+ * Set the Guzzle client
25
+ */
26
+ private static function setClient ()
27
+ {
28
+ if (self ::$ client === null ) {
29
+ self ::$ client = new Client ([
30
+ 'base_uri ' => self ::ENDPOINT_PROD ,
31
+ 'timeout ' => self ::TIMEOUT ,
32
+ 'verify ' => self ::VERIFY ,
33
+ ]);
34
+ }
35
+ }
17
36
18
37
public static function query ($ method , $ apiEndpoint , $ options = [])
19
38
{
39
+ self ::setClient ();
40
+
41
+ // set XIVAPI key
20
42
if ($ key = getenv (Environment::XIVAPI_KEY )) {
21
43
$ options [RequestOptions::QUERY ]['key ' ] = $ key ;
22
44
}
23
45
46
+ // set request queries
24
47
foreach (self ::$ options as $ query => $ value ) {
25
48
$ value = is_array ($ value ) ? implode (', ' , $ value ) : $ value ;
26
49
$ options [RequestOptions::QUERY ][$ query ] = $ value ;
27
50
}
28
-
29
- $ client = new Client ([
30
- 'base_uri ' => self ::ENDPOINT_PROD ,
31
- 'timeout ' => self ::TIMEOUT ,
32
- 'verify ' => self ::VERIFY ,
33
- ]);
51
+
52
+ // allow async
53
+ if (self ::$ async ) {
54
+ return self ::$ client ->requestAsync ($ method , $ apiEndpoint , $ options );
55
+ }
34
56
35
57
return \GuzzleHttp \json_decode (
36
- $ client ->request ($ method , $ apiEndpoint , $ options )->getBody ()
58
+ self :: $ client ->request ($ method , $ apiEndpoint , $ options )->getBody ()
37
59
);
38
60
}
39
61
62
+ public static function setAsync ()
63
+ {
64
+ self ::$ async = true ;
65
+ }
66
+
40
67
public static function resetQuery ()
41
68
{
42
69
self ::$ options = [];
0 commit comments