Skip to content
This repository was archived by the owner on Aug 12, 2022. It is now read-only.

Commit c392df2

Browse files
authored
Merge pull request #2 from sjaune/refactor-namespace
Refactor namespace
2 parents 277cf94 + db645fe commit c392df2

25 files changed

+992
-938
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ composer.phar
44
.DS_Store
55
node_modules
66
file.wav
7-
test.php
7+
test.php

composer.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66
{
77
"name": "Bruno",
88
"email": "bruno.gantelmi@recast.ai"
9+
},
10+
{
11+
"name": "Sylvain Jaune",
12+
"email": "sylvain@hellocare.com"
913
}
1014
],
1115
"minimum-stability": "dev",
@@ -19,8 +23,8 @@
1923
},
2024
"autoload": {
2125
"psr-4": {
22-
"SDK-PHP\\": "src/",
23-
"client\\": "tests/"
26+
"RecastAI\\": "src/",
27+
"Tests\\RecastAI\\": "tests/"
2428
}
2529
}
2630
}

package.json

Lines changed: 0 additions & 31 deletions
This file was deleted.

src/Client.php

Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
<?php
2+
3+
namespace RecastAI;
4+
5+
/**
6+
* Class Client
7+
* @package RecastAI
8+
*/
9+
class Client
10+
{
11+
const API_ENDPOINT = 'https://api.recast.ai/v2/request';
12+
13+
/**
14+
* Client constructor.
15+
* @param null $token
16+
* @param null $language
17+
*/
18+
public function __construct($token = null, $language = null)
19+
{
20+
$this->token = $token;
21+
$this->language = $language;
22+
}
23+
24+
/**
25+
* Sends a text request to Recast and returns the response.
26+
*
27+
* @param string $text
28+
*
29+
* @return \RecastAI\Response
30+
*
31+
* @throws Token is missing
32+
*/
33+
public function textRequest($text, $options = null)
34+
{
35+
if ($options === null) {
36+
$token = $this->token;
37+
} else if (array_key_exists('token', $options)) {
38+
$token = $options['token'];
39+
}
40+
41+
if ($this->language) {
42+
$params = array('text' => $text, 'language' => $this->language);
43+
} else {
44+
$params = array('text' => $text);
45+
}
46+
47+
if (!$token) {
48+
return ('Token is missing');
49+
} else {
50+
$headers = array('Content-Type' => 'application/json', 'Authorization' => "Token " . $token);
51+
52+
$res = $this->requestPrivate(self::API_ENDPOINT, $headers, $params);
53+
return (new Response($res));
54+
}
55+
}
56+
57+
/**
58+
* Sends a request to Recast and returns the response.
59+
*
60+
* @param string $url
61+
* @param array $headers
62+
* @param array $params
63+
*
64+
* @return Response $res
65+
*/
66+
protected function requestPrivate($url, $headers, $params)
67+
{
68+
$res = \Requests::post($url, $headers, json_encode($params));
69+
70+
return ($res);
71+
}
72+
73+
/**
74+
* Sends a request to Recast and returns the response.
75+
*
76+
* @param string $url
77+
* @param array $params
78+
*
79+
* @return Response $res
80+
*/
81+
protected function requestFilePrivate($url, $params)
82+
{
83+
$client = new \GuzzleHttp\Client();
84+
$res = $client->request('POST', $url, $params);
85+
86+
return ($res);
87+
}
88+
89+
/**
90+
* Sends a file request to Recast and returns the response.
91+
*
92+
* @param string $file
93+
*
94+
* @return Response
95+
*
96+
* @throws Token is missing
97+
*/
98+
public function fileRequest($file, $options = null)
99+
{
100+
if ($options === null) {
101+
$token = $this->token;
102+
} else if (array_key_exists('token', $options)) {
103+
$token = $options['token'];
104+
}
105+
106+
if (!$token) {
107+
return ('Token is missing');
108+
} else {
109+
$url = self::API_ENDPOINT;
110+
111+
if (!$this->language) {
112+
$params = [
113+
'headers' => [
114+
'Authorization' => "Token " . $token
115+
],
116+
'multipart' => [
117+
[
118+
'Content-Type' => 'multipart/form-data',
119+
'name' => 'voice',
120+
'contents' => fopen($file, 'r')
121+
],
122+
]
123+
];
124+
} else {
125+
$params = [
126+
'headers' => [
127+
'Authorization' => "Token " . $token
128+
],
129+
'multipart' => [
130+
[
131+
'Content-Type' => 'multipart/form-data',
132+
'name' => 'voice',
133+
'contents' => fopen($file, 'r')
134+
],
135+
[
136+
'name' => 'language',
137+
'contents' => $this->language
138+
],
139+
]
140+
];
141+
}
142+
$res = $this->requestFilePrivate($url, $params);
143+
return (new Response($res));
144+
}
145+
}
146+
147+
/**
148+
* @param $text
149+
* @param null $conversation_token
150+
* @param null $options
151+
* @return Conversation|string
152+
*/
153+
public function textConverse($text, $conversation_token = null, $options = null)
154+
{
155+
if ($options === null) {
156+
$token = $this->token;
157+
} else if ($options['token']) {
158+
$token = $options['token'];
159+
}
160+
161+
if ($this->language) {
162+
$params = array('text' => $text, 'language' => $this->language, 'conversation_token' => $conversation_token);
163+
} else {
164+
$params = array('text' => $text, 'conversation_token' => $conversation_token);
165+
}
166+
167+
if (!$token) {
168+
return ('Token is missing');
169+
} else {
170+
$headers = array('Content-Type' => 'application/json', 'Authorization' => "Token " . $token);
171+
$res = $this->requestPrivate(Conversation::API_ENDPOINT_CONVERSATION, $headers, $params);
172+
173+
return (new Conversation(($res)));
174+
}
175+
}
176+
}

0 commit comments

Comments
 (0)