|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Bobbyshaw\WatsonVisualRecognition\Message; |
|
4
|
|
|
|
|
5
|
|
|
use Bobbyshaw\WatsonVisualRecognition\Exceptions\AuthException; |
|
6
|
|
|
use GuzzleHttp\Exception\ClientException; |
|
7
|
|
|
use GuzzleHttp\Psr7\MultipartStream; |
|
8
|
|
|
use GuzzleHttp\Psr7\Request; |
|
9
|
|
|
use InvalidArgumentException; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Class CreateClassifierRequest |
|
13
|
|
|
* @package Bobbyshaw\WatsonVisualRecognition\Message |
|
14
|
|
|
*/ |
|
15
|
|
|
class CreateClassifierRequest extends AbstractRequest |
|
16
|
|
|
{ |
|
17
|
|
|
const CLASSIFIERS_PATH = 'classifiers/'; |
|
18
|
|
|
const ALLOWED_FILE_TYPES = ['.zip']; |
|
19
|
|
|
|
|
20
|
|
|
|
|
21
|
5 |
|
public function getData() |
|
22
|
|
|
{ |
|
23
|
5 |
|
$data = parent::getData(); |
|
24
|
|
|
|
|
25
|
5 |
|
$data['positive_examples'] = $this->getPositiveExamples(); |
|
26
|
5 |
|
$data['negative_examples'] = $this->getNegativeExamples(); |
|
27
|
5 |
|
$data['name'] = $this->getName(); |
|
28
|
|
|
|
|
29
|
5 |
|
return $data; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Send HTTP request to create a new classifier |
|
34
|
|
|
* |
|
35
|
|
|
* @param array $data |
|
36
|
|
|
* @return ClassifierResponse |
|
37
|
|
|
* @throws AuthException |
|
38
|
|
|
* @throws \Exception |
|
39
|
|
|
*/ |
|
40
|
4 |
|
public function sendData($data) |
|
41
|
|
|
{ |
|
42
|
4 |
|
$this->validateData($data); |
|
43
|
|
|
|
|
44
|
|
|
$params = [ |
|
45
|
|
|
[ |
|
46
|
3 |
|
'name' => 'positive_examples', |
|
47
|
3 |
|
'contents' => fopen($data['positive_examples'], 'r'), |
|
48
|
3 |
|
'filename' => $data['positive_examples'] |
|
49
|
3 |
|
], |
|
50
|
|
|
[ |
|
51
|
3 |
|
'name' => 'negative_examples', |
|
52
|
3 |
|
'contents' => fopen($data['negative_examples'], 'r'), |
|
53
|
3 |
|
'filename' => $data['negative_examples'] |
|
54
|
3 |
|
], |
|
55
|
|
|
[ |
|
56
|
3 |
|
'name' => 'name', |
|
57
|
3 |
|
'contents' => $data['name'] |
|
58
|
3 |
|
] |
|
59
|
3 |
|
]; |
|
60
|
|
|
|
|
61
|
3 |
|
$multipartStream = new MultipartStream($params); |
|
62
|
|
|
|
|
63
|
3 |
|
$request = new Request( |
|
64
|
3 |
|
'POST', |
|
65
|
3 |
|
$this->getApiUrl(static::CLASSIFIERS_PATH) . '?' . http_build_query(['version' => $data['version']]), |
|
66
|
3 |
|
['Authorization' => 'Basic ' . base64_encode($data['username'] . ':' . $data['password'])], |
|
67
|
|
|
$multipartStream |
|
68
|
3 |
|
); |
|
69
|
|
|
|
|
70
|
|
|
try { |
|
71
|
3 |
|
$response = $this->httpClient->send($request); |
|
72
|
|
|
|
|
73
|
2 |
|
if ($response->getStatusCode() != 200) { |
|
74
|
|
|
throw new \Exception($response->getBody()->getContents(), $response->getStatusCode()); |
|
75
|
|
|
} |
|
76
|
3 |
|
} catch (ClientException $e) { |
|
77
|
1 |
|
if ($e->getCode() == 401) { |
|
78
|
1 |
|
throw new AuthException('Invalid credentials provided'); |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
2 |
|
return $this->response = new ClassifierResponse($this, $response->getBody()); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Validate Data |
|
88
|
|
|
* |
|
89
|
|
|
* @param $data |
|
90
|
|
|
* @return bool |
|
91
|
|
|
* @throws InvalidArgumentException |
|
92
|
|
|
*/ |
|
93
|
4 |
|
protected function validateData($data) |
|
94
|
|
|
{ |
|
95
|
4 |
|
if (!in_array(substr($data['positive_examples'], -4), static::ALLOWED_FILE_TYPES) |
|
96
|
4 |
|
|| !in_array(substr($data['negative_examples'], -4), static::ALLOWED_FILE_TYPES)) { |
|
97
|
1 |
|
throw new InvalidArgumentException( |
|
98
|
1 |
|
'Example files needs to be one of the following types: ' . implode(', ', static::ALLOWED_FILE_TYPES) |
|
99
|
1 |
|
); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
3 |
|
return true; |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|