Skip to content

Commit f5a7bcf

Browse files
committed
Initial commit
0 parents  commit f5a7bcf

File tree

8 files changed

+435
-0
lines changed

8 files changed

+435
-0
lines changed

.gitignore

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
.DS_Store
2+
.idea
3+
4+
/vendor
5+
composer.lock
6+
7+
*.sublime-project
8+
*.sublime-workspace
9+
*.phar
10+
*.log

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2016 Ivan Boldyrev
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
Api.ai PHP sdk
2+
==============
3+
4+
This is an unofficial php sdk for [Api.ai][1] and it's still in progress...
5+
6+
```
7+
Api.ai: Build brand-unique, natural language interactions for bots, applications and devices.
8+
```
9+
10+
## Install:
11+
12+
Via composer:
13+
14+
```
15+
$ composer require iboldurev/api-ai-php
16+
```
17+
18+
## Usage:
19+
20+
Using the low level `Client`:
21+
22+
```php
23+
require_once __DIR__.'/vendor/autoload.php';
24+
25+
use Api\Client;
26+
27+
$client = new Client('access_token');
28+
29+
$query = $client->get('query', [
30+
'query' => 'Hello',
31+
]);
32+
33+
$response = json_decode((string) $query->getBody(), true);
34+
```
35+
36+
[1]: https://api.ai

composer.json

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"name": "iboldurev/api-ai-php",
3+
"description": "API.ai php sdk",
4+
"minimum-stability": "stable",
5+
"license": "MIT",
6+
"authors": [
7+
{
8+
"name": "Ivan Boldyrev",
9+
"email": "iboldurev@gmail.com",
10+
"homepage": "https://github.com/iboldurev"
11+
}
12+
],
13+
"keywords": [
14+
"api.ai",
15+
"api.ai sdk",
16+
"bot"
17+
],
18+
"autoload": {
19+
"psr-4": {
20+
"Api\\": "src/"
21+
}
22+
},
23+
"require": {
24+
"php": ">=5.5.0",
25+
"guzzlehttp/guzzle": "^6.2"
26+
}
27+
}

src/Client.php

Lines changed: 221 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,221 @@
1+
<?php
2+
3+
namespace Api;
4+
5+
use Api\HttpClient\HttpClient;
6+
use Api\HttpClient\GuzzleHttpClient;
7+
use Api\Exception\BadResponseException;
8+
use Psr\Http\Message\ResponseInterface;
9+
10+
/**
11+
* Class Client
12+
*
13+
* @package Api
14+
*/
15+
class Client
16+
{
17+
/**
18+
* API Base url
19+
*/
20+
const API_BASE_URI = 'https://api.api.ai/';
21+
22+
/**
23+
* API Version
24+
*/
25+
const DEFAULT_API_VERSION = '20150910';
26+
27+
/**
28+
* API Endpoint
29+
*/
30+
const DEFAULT_API_ENDPOINT = 'v1/';
31+
32+
/**
33+
* API Default Language
34+
*/
35+
const DEFAULT_API_LANGUAGE = 'en';
36+
37+
/**
38+
* API Default Source
39+
*/
40+
const DEFAULT_API_SOURCE = 'php';
41+
42+
/**
43+
* Request default timeout
44+
*/
45+
const DEFAULT_TIMEOUT = 5;
46+
47+
/**
48+
* @var array
49+
*/
50+
public static $allowedMethod = ['GET', 'POST'];
51+
52+
/**
53+
* @var string Api.ai token
54+
*/
55+
private $accessToken;
56+
57+
/**
58+
* @var string
59+
*/
60+
private $apiLanguage;
61+
62+
/**
63+
* @var string
64+
*/
65+
private $apiVersion;
66+
67+
/**
68+
* @var HttpClient client
69+
*/
70+
private $client;
71+
72+
/**
73+
* @var ResponseInterface|null
74+
*/
75+
private $lastResponse;
76+
77+
/**
78+
* Client constructor.
79+
*
80+
* @param string $accessToken
81+
* @param HttpClient|null $httpClient
82+
* @param string $apiLanguage
83+
* @param string $apiVersion
84+
*/
85+
public function __construct($accessToken, HttpClient $httpClient = null, $apiLanguage = self::DEFAULT_API_LANGUAGE, $apiVersion = self::DEFAULT_API_VERSION)
86+
{
87+
$this->accessToken = $accessToken;
88+
$this->apiLanguage = $apiLanguage;
89+
$this->apiVersion = $apiVersion;
90+
$this->client = $httpClient ?: $this->defaultHttpClient();
91+
}
92+
93+
/**
94+
* @return GuzzleHttpClient
95+
*/
96+
private function defaultHttpClient()
97+
{
98+
return new GuzzleHttpClient();
99+
}
100+
101+
/**
102+
* @return string
103+
*/
104+
public function getAccessToken()
105+
{
106+
return $this->accessToken;
107+
}
108+
109+
/**
110+
* @return GuzzleHttpClient|HttpClient
111+
*/
112+
public function getHttpClient()
113+
{
114+
return $this->client;
115+
}
116+
117+
/**
118+
* @return null|ResponseInterface
119+
*/
120+
public function getLastResponse()
121+
{
122+
return $this->lastResponse;
123+
}
124+
125+
/**
126+
* @param string $uri
127+
* @param array $params
128+
*
129+
* @return ResponseInterface
130+
*/
131+
public function get($uri, array $params = [])
132+
{
133+
return $this->send('GET', $uri, null, $params);
134+
}
135+
136+
/**
137+
* @param string $uri
138+
* @param array $params
139+
*
140+
* @return ResponseInterface
141+
*/
142+
public function post($uri, array $params = [])
143+
{
144+
return $this->send('POST', $uri, $params);
145+
}
146+
147+
/**
148+
* @param string $method
149+
* @param string $uri
150+
* @param mixed $body
151+
* @param array $query
152+
* @param array $headers
153+
* @param array $options
154+
*
155+
* @return ResponseInterface
156+
*/
157+
public function send($method, $uri, $body = null, array $query = [], array $headers = [], array $options = [])
158+
{
159+
$this->validateMethod($method);
160+
161+
$query = array_merge($this->getDefaultQuery(), $query);
162+
$headers = array_merge($this->getDefaultHeaders(), $headers);
163+
164+
$this->lastResponse = $this->client->send($method, $uri, $body, $query, $headers, $options);
165+
166+
$this->validateResponse($this->lastResponse);
167+
168+
return $this->lastResponse;
169+
}
170+
171+
/**
172+
* @param string $method
173+
* @throw \InvalidArgumentException
174+
*/
175+
private function validateMethod($method)
176+
{
177+
if (!in_array(strtoupper($method), self::$allowedMethod)) {
178+
throw new \InvalidArgumentException(sprintf('"%s" is not in the allowed methods "%s"', $method, implode(', ', self::$allowedMethod)));
179+
}
180+
}
181+
182+
/**
183+
* @param ResponseInterface $response
184+
* @throws BadResponseException
185+
*/
186+
private function validateResponse(ResponseInterface $response)
187+
{
188+
if ($response->getStatusCode() !== 200) {
189+
$message = empty($response->getReasonPhrase()) ? 'Bad response status code' : $response->getReasonPhrase();
190+
throw new BadResponseException($message, $response);
191+
}
192+
}
193+
194+
/**
195+
* Get the defaults query
196+
*
197+
* @return array
198+
*/
199+
private function getDefaultQuery()
200+
{
201+
return [
202+
'lang' => $this->apiLanguage,
203+
'v' => $this->apiVersion,
204+
];
205+
}
206+
207+
/**
208+
* Get the defaults headers
209+
*
210+
* @return array
211+
*/
212+
private function getDefaultHeaders()
213+
{
214+
return [
215+
'Content-Type' => 'application/json; charset=utf-8',
216+
'Authorization' => 'Bearer ' . $this->accessToken,
217+
'api-request-source' => self::DEFAULT_API_SOURCE,
218+
];
219+
}
220+
221+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
namespace Api\Exception;
4+
5+
use Psr\Http\Message\ResponseInterface;
6+
7+
/**
8+
* Class BadResponseException
9+
*
10+
* @package Api\Exception
11+
*/
12+
class BadResponseException extends \RuntimeException
13+
{
14+
/**
15+
* @var ResponseInterface
16+
*/
17+
private $response;
18+
19+
/**
20+
* BadResponseException constructor.
21+
*
22+
* @param string $message
23+
* @param ResponseInterface $response
24+
*/
25+
public function __construct($message, ResponseInterface $response)
26+
{
27+
$code = $response ? $response->getStatusCode() : 0;
28+
29+
parent::__construct($message, $code);
30+
$this->response = $response;
31+
}
32+
33+
/**
34+
* @return ResponseInterface
35+
*/
36+
public function getResponse()
37+
{
38+
return $this->response;
39+
}
40+
}

0 commit comments

Comments
 (0)