1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Licensed to CRATE Technology GmbH("Crate") under one or more contributor |
4
|
|
|
* license agreements. See the NOTICE file distributed with this work for |
5
|
|
|
* additional information regarding copyright ownership. Crate licenses |
6
|
|
|
* this file to you under the Apache License, Version 2.0 (the "License"); |
7
|
|
|
* you may not use this file except in compliance with the License. You may |
8
|
|
|
* obtain a copy of the License at |
9
|
|
|
* |
10
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
11
|
|
|
* |
12
|
|
|
* Unless required by applicable law or agreed to in writing, software |
13
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
14
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
15
|
|
|
* License for the specific language governing permissions and limitations |
16
|
|
|
* under the License. |
17
|
|
|
* |
18
|
|
|
* However, if you have executed another commercial license agreement |
19
|
|
|
* with Crate these terms will supersede the license and you may use the |
20
|
|
|
* software solely pursuant to the terms of the relevant commercial agreement. |
21
|
|
|
*/ |
22
|
|
|
|
23
|
|
|
namespace Crate\PDO\Http; |
24
|
|
|
|
25
|
|
|
use Crate\PDO\Exception\UnsupportedException; |
26
|
|
|
use GuzzleHttp\Client as HttpClient; |
27
|
|
|
use GuzzleHttp\Exception\RequestException; |
28
|
|
|
use GuzzleHttp\Message\ResponseInterface; |
29
|
|
|
|
30
|
|
|
class Server implements InternalClientInterface |
31
|
|
|
{ |
32
|
|
|
|
33
|
|
|
const PROTOCOL = 'http'; |
34
|
|
|
const SQL_PATH = '/_sql'; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var HttpClient |
38
|
|
|
*/ |
39
|
|
|
private $client; |
40
|
|
|
|
41
|
|
|
private $opts = [ |
42
|
|
|
'headers' => [] |
43
|
|
|
]; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param string $uri |
47
|
|
|
* @param array $options |
48
|
|
|
*/ |
49
|
|
|
public function __construct($uri, array $options) |
|
|
|
|
50
|
|
|
{ |
51
|
|
|
$uri = self::computeURI($uri); |
52
|
|
|
$this->client = new HttpClient([ |
53
|
|
|
'base_uri' => $uri |
54
|
|
|
]); |
55
|
|
|
} |
56
|
|
|
|
57
|
1 |
|
public function setTimeout($timeout) |
58
|
|
|
{ |
59
|
1 |
|
$this->opts['timeout'] = (float)$timeout; |
60
|
1 |
|
} |
61
|
|
|
|
62
|
|
|
public function setHttpBasicAuth($username, $passwd) |
63
|
|
|
{ |
64
|
|
|
$this->opts['auth'] = [$username, $passwd]; |
65
|
|
|
} |
66
|
|
|
|
67
|
1 |
|
public function setHttpHeader($name, $value) |
68
|
|
|
{ |
69
|
1 |
|
$this->opts['headers'][$name] = $value; |
70
|
1 |
|
} |
71
|
|
|
|
72
|
1 |
|
public function getServerInfo() |
73
|
|
|
{ |
74
|
|
|
// TODO: Implement getServerInfo() method. |
75
|
1 |
|
throw new UnsupportedException('Not yet implemented'); |
76
|
|
|
} |
77
|
|
|
|
78
|
1 |
|
public function getServerVersion() |
79
|
|
|
{ |
80
|
|
|
// TODO: Implement getServerVersion() method. |
81
|
1 |
|
throw new UnsupportedException('Not yet implemented'); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Execute a HTTP/1.1 POST request with JSON body |
86
|
|
|
* |
87
|
|
|
* @param array $body |
88
|
|
|
|
89
|
|
|
* @return ResponseInterface |
90
|
|
|
* @throws RequestException When an error is encountered |
91
|
|
|
*/ |
92
|
|
|
public function doRequest(array $body) |
93
|
|
|
{ |
94
|
|
|
$args = ['json' => $body] + $this->opts; |
95
|
|
|
return $this->client->post(null, $args); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Compute a URI for usage with the HTTP client |
100
|
|
|
* |
101
|
|
|
* @param string $server A host:port string |
102
|
|
|
* |
103
|
|
|
* @return string An URI which can be used by the HTTP client |
104
|
|
|
*/ |
105
|
4 |
|
private static function computeURI($server) |
106
|
|
|
{ |
107
|
4 |
|
return self::PROTOCOL .'://' . $server . self::SQL_PATH; |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.