2
2
3
3
namespace supermetrics \lib ;
4
4
5
+ use DateTime ;
5
6
use Exception ;
6
7
use InvalidArgumentException ;
7
8
use supermetrics \exception \InvalidTokenException ;
@@ -40,6 +41,10 @@ class SupermetricsClient
40
41
* Token registered with the API
41
42
*/
42
43
private $ token ;
44
+ /**
45
+ * Curl handlers
46
+ */
47
+ private $ ch ;
43
48
44
49
public function __construct (string $ email , string $ name , $ clientId )
45
50
{
@@ -60,8 +65,8 @@ public function enableLog($log_file_path = null)
60
65
}
61
66
62
67
/**
63
- * This function will handle all API calls
64
- * If postData is not null, consider this as a post request. otherwise, its a get request
68
+ * This function will handle single API request
69
+ * If postData is not null, consider this as a POST request. otherwise, its a GET request
65
70
*/
66
71
private function sendRequest (string $ requestPath , array $ params = [], array $ postData = []): object
67
72
{
@@ -75,16 +80,12 @@ private function sendRequest(string $requestPath, array $params = [], array $pos
75
80
curl_setopt ($ ch , CURLOPT_POSTFIELDS , $ postData );
76
81
}
77
82
curl_setopt ($ ch , CURLOPT_RETURNTRANSFER , true );
78
- if ($ this ->isLog ) {
79
- $ this ->log (self ::API_BASE_URL . $ requestPath );
80
- if (!empty ($ postData )) {
81
- $ this ->log ('Post Data : ' . json_encode ($ postData ));
82
- }
83
+ $ this ->log (self ::API_BASE_URL . $ requestPath );
84
+ if (!empty ($ postData )) {
85
+ $ this ->log ('Post Data : ' . json_encode ($ postData ));
83
86
}
84
87
$ result = curl_exec ($ ch );
85
- if ($ this ->isLog ) {
86
- $ this ->log ('Response : ' . $ result );
87
- }
88
+ $ this ->log ('Response : ' . $ result );
88
89
89
90
if (empty ($ result )) {
90
91
throw new Exception ('No response from the API ' );
@@ -96,6 +97,68 @@ private function sendRequest(string $requestPath, array $params = [], array $pos
96
97
return $ response ;
97
98
}
98
99
100
+ /**
101
+ * This function will handle setup CURL for multipal API calls simultaneously
102
+ * If postData is not null, consider this as a post request. otherwise, its a get request
103
+ */
104
+ private function setMultiRequest (string $ requestPath , array $ params = [], array $ postData = []): void
105
+ {
106
+ $ ch = curl_init ();
107
+ if (!empty ($ params )) {
108
+ $ requestPath .= '? ' . http_build_query ($ params );
109
+ }
110
+ curl_setopt ($ ch , CURLOPT_URL , self ::API_BASE_URL . $ requestPath );
111
+ if (!empty ($ postData )) {
112
+ curl_setopt ($ ch , CURLOPT_POST , 1 );
113
+ curl_setopt ($ ch , CURLOPT_POSTFIELDS , $ postData );
114
+ }
115
+ curl_setopt ($ ch , CURLOPT_RETURNTRANSFER , true );
116
+ $ this ->log (self ::API_BASE_URL . $ requestPath );
117
+ if (!empty ($ postData )) {
118
+ $ this ->log ('Post Data : ' . json_encode ($ postData ));
119
+ }
120
+ $ this ->ch [] = $ ch ;
121
+ }
122
+
123
+ protected function executeMultiRequest (): array
124
+ {
125
+ $ retVal = [];
126
+ $ mh = curl_multi_init ();
127
+
128
+ foreach ($ this ->ch as $ ch ) {
129
+ curl_multi_add_handle ($ mh , $ ch );
130
+ }
131
+ $ active = null ;
132
+ //execute the handles
133
+ do {
134
+ $ mrc = curl_multi_exec ($ mh , $ active );
135
+ } while ($ mrc == CURLM_CALL_MULTI_PERFORM );
136
+
137
+ while ($ active && $ mrc == CURLM_OK ) {
138
+ if (curl_multi_select ($ mh ) != -1 ) {
139
+ do {
140
+ $ mrc = curl_multi_exec ($ mh , $ active );
141
+ } while ($ mrc == CURLM_CALL_MULTI_PERFORM );
142
+ }
143
+ }
144
+ foreach ($ this ->ch as $ ch ) {
145
+ $ result = curl_multi_getcontent ($ ch ); // get the content
146
+ curl_multi_remove_handle ($ mh , $ ch );
147
+
148
+ $ this ->log ('Response : ' . $ result );
149
+
150
+ if (empty ($ result )) {
151
+ throw new Exception ('No response from the API ' );
152
+ }
153
+ $ response = json_decode ($ result );
154
+ if (!empty ($ response ->error ) && $ response ->error ->message == 'Invalid SL Token ' ) {
155
+ throw new InvalidTokenException ('Expired or Invalid SL Token ' );
156
+ }
157
+ $ retVal [] = $ response ;
158
+ }
159
+ return $ retVal ;
160
+ }
161
+
99
162
/**
100
163
* Get new token request
101
164
*/
@@ -113,10 +176,9 @@ private function refreshToken(): void
113
176
}
114
177
115
178
/**
116
- * Setup curl options and call the API
117
- * If postData is not null, consider this as a post request. otherwise, its a get request
179
+ * Single API call
118
180
*/
119
- protected function callAPI (string $ requestPath , array $ params = [], array $ postData = []): object
181
+ protected function callApi (string $ requestPath , array $ params = [], array $ postData = []): object
120
182
{
121
183
if ($ this ->token === null ) {
122
184
$ this ->refreshToken ();
@@ -135,12 +197,29 @@ protected function callAPI(string $requestPath, array $params = [], array $postD
135
197
return $ retVal ;
136
198
}
137
199
200
+ /**
201
+ * Setup curl options for multipal API calls
202
+ * If postData is not null, consider this as a POST request. otherwise, its a GET request
203
+ */
204
+ protected function setupApiCall (string $ requestPath , array $ params = [], array $ postData = []): void
205
+ {
206
+ if ($ this ->token === null ) {
207
+ $ this ->refreshToken ();
208
+ }
209
+ $ params ['sl_token ' ] = $ this ->token ;
210
+
211
+ $ this ->setMultiRequest ($ requestPath , $ params , $ postData );
212
+ }
213
+
138
214
/**
139
215
* Write logfile
140
216
*/
141
217
private function log (string $ data ): void
142
218
{
143
- file_put_contents ($ this ->logFile , date ("Y-m-d H:i:s - " ) . $ data . "\n" , FILE_APPEND );
219
+ if ($ this ->isLog ) {
220
+ $ now = DateTime::createFromFormat ('U.u ' , microtime (true ));
221
+ file_put_contents ($ this ->logFile , $ now ->format ("Y-m-d H:i:s.v " ) . $ data . "\n" , FILE_APPEND );
222
+ }
144
223
}
145
224
146
225
}
0 commit comments