|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Base Watson service class |
|
4
|
|
|
*/ |
|
5
|
|
|
namespace IBM\Watson\Common; |
|
6
|
|
|
|
|
7
|
|
|
use GuzzleHttp\Client; |
|
8
|
|
|
use GuzzleHttp\ClientInterface; |
|
9
|
|
|
use Symfony\Component\HttpFoundation\ParameterBag; |
|
10
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Base Watson service class |
|
14
|
|
|
* |
|
15
|
|
|
* This abstract class should be extended by all Watson services |
|
16
|
|
|
* throughout the SDK. It enforces implementation of |
|
17
|
|
|
* the ServiceInterface and defines various common attributes |
|
18
|
|
|
* and methods that all services should have. |
|
19
|
|
|
* |
|
20
|
|
|
* @see ServiceInterface |
|
21
|
|
|
*/ |
|
22
|
|
|
abstract class AbstractService implements ServiceInterface |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* @var \Symfony\Component\HttpFoundation\ParameterBag |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $parameters; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var \GuzzleHttp\ClientInterface |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $httpClient; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var \Symfony\Component\HttpFoundation\Request |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $httpRequest; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Create a new service instance |
|
41
|
|
|
* |
|
42
|
|
|
* @param ClientInterface $httpClient A Guzzle client to make API calls with |
|
43
|
|
|
* @param Request $httpRequest A Symfony HTTP request object |
|
44
|
|
|
*/ |
|
45
|
6 |
|
public function __construct(ClientInterface $httpClient = null, Request $httpRequest = null) |
|
46
|
|
|
{ |
|
47
|
6 |
|
$this->httpClient = $httpClient ?: $this->getDefaultHttpClient(); |
|
48
|
6 |
|
$this->httpRequest = $httpRequest ?: $this->getDefaultHttpRequest(); |
|
49
|
6 |
|
$this->initialize(); |
|
50
|
6 |
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Initialize this service with default parameters |
|
54
|
|
|
* |
|
55
|
|
|
* @param array $parameters |
|
56
|
|
|
* @return $this |
|
57
|
|
|
*/ |
|
58
|
15 |
|
public function initialize(array $parameters = []) |
|
59
|
|
|
{ |
|
60
|
15 |
|
$this->parameters = new ParameterBag; |
|
61
|
|
|
|
|
62
|
15 |
|
foreach ($this->getDefaultParameters() as $key => $value) { |
|
63
|
15 |
|
if (is_array($value)) { |
|
64
|
3 |
|
$this->parameters->set($key, reset($value)); |
|
65
|
4 |
|
} else { |
|
66
|
15 |
|
$this->parameters->set($key, $value); |
|
67
|
|
|
} |
|
68
|
10 |
|
} |
|
69
|
|
|
|
|
70
|
15 |
|
Helper::initialize($this, $parameters); |
|
71
|
|
|
|
|
72
|
15 |
|
return $this; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @return array |
|
77
|
|
|
*/ |
|
78
|
15 |
|
public function getDefaultParameters() |
|
79
|
|
|
{ |
|
80
|
|
|
return [ |
|
81
|
15 |
|
'username' => '', |
|
82
|
|
|
'password' => '' |
|
83
|
10 |
|
]; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @return array |
|
88
|
|
|
*/ |
|
89
|
6 |
|
public function getParameters() |
|
90
|
|
|
{ |
|
91
|
6 |
|
return $this->parameters->all(); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @param string $key |
|
96
|
|
|
* @return mixed |
|
97
|
|
|
*/ |
|
98
|
6 |
|
public function getParameter($key) |
|
99
|
|
|
{ |
|
100
|
6 |
|
return $this->parameters->get($key); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* @param string $key |
|
105
|
|
|
* @param mixed $value |
|
106
|
|
|
* @return $this |
|
107
|
|
|
*/ |
|
108
|
6 |
|
public function setParameter($key, $value) |
|
109
|
|
|
{ |
|
110
|
6 |
|
$this->parameters->set($key, $value); |
|
111
|
|
|
|
|
112
|
6 |
|
return $this; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* @return string |
|
117
|
|
|
*/ |
|
118
|
3 |
|
public function getUsername() |
|
119
|
|
|
{ |
|
120
|
3 |
|
return $this->getParameter('username'); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* @param string $value |
|
125
|
|
|
* @return $this |
|
126
|
|
|
*/ |
|
127
|
3 |
|
public function setUsername($value) |
|
128
|
|
|
{ |
|
129
|
3 |
|
return $this->setParameter('username', $value); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* @return string |
|
134
|
|
|
*/ |
|
135
|
3 |
|
public function getPassword() |
|
136
|
|
|
{ |
|
137
|
3 |
|
return $this->getParameter('password'); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* @param string $value |
|
142
|
|
|
* @return $this |
|
143
|
|
|
*/ |
|
144
|
3 |
|
public function setPassword($value) |
|
145
|
|
|
{ |
|
146
|
3 |
|
return $this->setParameter('password', $value); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* Get the global default HTTP client |
|
151
|
|
|
* |
|
152
|
|
|
* @return Client |
|
153
|
|
|
*/ |
|
154
|
6 |
|
protected function getDefaultHttpClient() |
|
155
|
|
|
{ |
|
156
|
6 |
|
return new Client([ |
|
157
|
6 |
|
'curl.options' => [CURLOPT_CONNECTTIMEOUT => 60], |
|
158
|
4 |
|
]); |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* Get the global default HTTP request |
|
163
|
|
|
* |
|
164
|
|
|
* @return Request |
|
165
|
|
|
*/ |
|
166
|
6 |
|
protected function getDefaultHttpRequest() |
|
167
|
|
|
{ |
|
168
|
6 |
|
return Request::createFromGlobals(); |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
/** |
|
172
|
|
|
* Create and initialize a request object |
|
173
|
|
|
* |
|
174
|
|
|
* @see \IBM\Watson\Common\Message\AbstractRequest |
|
175
|
|
|
* @param string $class The request class name |
|
176
|
|
|
* @param array $parameters |
|
177
|
|
|
* |
|
178
|
|
|
* @return \IBM\Watson\Common\Message\AbstractRequest |
|
179
|
|
|
*/ |
|
180
|
3 |
|
protected function createRequest($class, array $parameters) |
|
181
|
|
|
{ |
|
182
|
3 |
|
$obj = new $class($this->httpClient, $this->httpRequest); |
|
183
|
|
|
|
|
184
|
3 |
|
return $obj->initialize(array_replace($this->getParameters(), $parameters)); |
|
185
|
|
|
} |
|
186
|
|
|
} |
|
187
|
|
|
|