1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace VictorAvelar\Phraseapp; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Client; |
6
|
|
|
|
7
|
|
|
class PhraseappHttpClient extends Client |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* API main URL. |
11
|
|
|
* |
12
|
|
|
* @var string |
13
|
|
|
*/ |
14
|
|
|
const BASE_PATH = "https://api.phraseapp.com/v2/"; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Timeout to wait for a response. |
18
|
|
|
* |
19
|
|
|
* @var int |
20
|
|
|
*/ |
21
|
|
|
const CLIENT_TIMEOUT = 10; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* PhraseappHttpClient constructor. |
25
|
|
|
* @param array $configuration |
26
|
|
|
*/ |
27
|
6 |
|
public function __construct(array $configuration = []) |
28
|
|
|
{ |
29
|
6 |
|
$configuration = array_merge_recursive([ |
30
|
6 |
|
'base_uri' => self::BASE_PATH, |
31
|
6 |
|
'timeout' => self::CLIENT_TIMEOUT, |
32
|
|
|
'allow_redirects' => false, |
33
|
|
|
'headers' => [ |
34
|
|
|
'Accept' => 'application/json' |
35
|
|
|
] |
36
|
6 |
|
], $configuration); |
37
|
6 |
|
parent::__construct($configuration); |
38
|
6 |
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Create a new instance using the config defaults and the Oauth2 token auth strategy. |
42
|
|
|
* |
43
|
|
|
* @param string $phraseappToken |
44
|
|
|
* @return PhraseappHttpClient |
45
|
|
|
*/ |
46
|
3 |
|
public static function createWithToken(string $phraseappToken) |
47
|
|
|
{ |
48
|
3 |
|
$config = ['headers' => ['Authorization' => "token $phraseappToken"]]; |
49
|
|
|
|
50
|
3 |
|
return new self($config); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Create a new instance using the config defaults and user:password auth strategy. |
55
|
|
|
* |
56
|
|
|
* @param string $user |
57
|
|
|
* @param string $password |
58
|
|
|
* @return PhraseappHttpClient |
59
|
|
|
*/ |
60
|
3 |
|
public static function createWithBasicAuth(string $user, string $password) |
61
|
|
|
{ |
62
|
3 |
|
$config = ['headers' => ['Authorization' => [$user, $password]]]; |
63
|
|
|
|
64
|
3 |
|
return new self($config); |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|