1: <?php
2:
3: namespace predictionio;
4:
5: use GuzzleHttp\Client;
6: use GuzzleHttp\Exception\ClientException;
7:
8: /**
9: * Base client for Event and Engine client
10: *
11: */
12: abstract class BaseClient {
13: private $baseUrl;
14: public $client;
15:
16: /**
17: * @param string Base URL to the server
18: * @param float Timeout of the request in seconds. Use 0 to wait indefinitely
19: * @param float Number of seconds to wait while trying to connect to a server
20: */
21: public function __construct($baseUrl, $timeout, $connectTimeout) {
22: $this->baseUrl = $baseUrl;
23: $this->client = new Client([
24: 'base_url' => $this->baseUrl,
25: 'defaults' => ['timeout' => $timeout,
26: 'connect_timeout' => $connectTimeout]
27: ]);
28:
29: }
30:
31: /**
32: * Get the status of the Event Server or Engine Instance
33: *
34: * @return string status
35: */
36: public function getStatus() {
37: return $this->client->get('/')->getBody();
38: }
39:
40: /**
41: * Send a HTTP request to the server
42: *
43: * @param string HTTP request method
44: * @param string Relative or absolute url
45: * @param string HTTP request body
46: *
47: * @return array JSON response
48: * @throws PredictionIOAPIError Request error
49: */
50: protected function sendRequest($method, $url, $body) {
51: $options = ['headers' => ['Content-Type' => 'application/json'],
52: 'body' => $body];
53: $request = $this->client->createRequest($method, $url, $options);
54:
55: try {
56: $response = $this->client->send($request);
57: return $response->json();
58: } catch (ClientException $e) {
59: throw new PredictionIOAPIError($e->getMessage());
60: }
61: }
62: }
63: ?>
64: