• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

prooph / event-store-client / 9555551525

17 Jun 2024 10:16PM UTC coverage: 70.262% (-1.1%) from 71.395%
9555551525

push

github

prolic
update coveralls repo token

3466 of 4933 relevant lines covered (70.26%)

67.7 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

94.03
/src/Transport/Http/HttpClient.php
1
<?php
2

3
/**
4
 * This file is part of `prooph/event-store-client`.
5
 * (c) 2018-2024 Alexander Miertsch <kontakt@codeliner.ws>
6
 * (c) 2018-2024 Sascha-Oliver Prolic <saschaprolic@googlemail.com>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11

12
declare(strict_types=1);
13

14
namespace Prooph\EventStoreClient\Transport\Http;
15

16
use Amp\Http\Client\Connection\DefaultConnectionFactory;
17
use Amp\Http\Client\Connection\UnlimitedConnectionPool;
18
use Amp\Http\Client\HttpClient as AmpHttpClient;
19
use Amp\Http\Client\HttpClientBuilder;
20
use Amp\Http\Client\Interceptor\SetRequestTimeout;
21
use Amp\Http\Client\Request;
22
use Amp\Socket\ClientTlsContext;
23
use Amp\Socket\ConnectContext;
24
use Closure;
25
use Prooph\EventStore\Transport\Http\HttpMethod;
26
use Prooph\EventStore\UserCredentials;
27
use Throwable;
28

29
/** @internal  */
30
class HttpClient
31
{
32
    private readonly AmpHttpClient $httpClient;
33

34
    public function __construct(int $operationTimeout, bool $verifyPeer)
35
    {
36
        $builder = new HttpClientBuilder();
190✔
37

38
        $tlsContext = new ClientTlsContext('');
190✔
39
        if (! $verifyPeer) {
190✔
40
            $tlsContext = $tlsContext->withoutPeerVerification();
145✔
41
        }
42
        $connectContext = (new ConnectContext())
190✔
43
            ->withTlsContext($tlsContext);
190✔
44
        $this->httpClient = $builder
190✔
45
            ->intercept(new SetRequestTimeout($operationTimeout, $operationTimeout, $operationTimeout))
190✔
46
            ->usingPool(new UnlimitedConnectionPool(new DefaultConnectionFactory(null, $connectContext)))
190✔
47
            ->build();
190✔
48
    }
49

50
    public function get(
51
        string $url,
52
        ?UserCredentials $userCredentials,
53
        Closure $onSuccess,
54
        Closure $onException
55
    ): void {
56
        $this->receive(
29✔
57
            HttpMethod::Get,
29✔
58
            $url,
29✔
59
            $userCredentials,
29✔
60
            $onSuccess,
29✔
61
            $onException
29✔
62
        );
29✔
63
    }
64

65
    public function post(
66
        string $url,
67
        string $body,
68
        string $contentType,
69
        ?UserCredentials $userCredentials,
70
        Closure $onSuccess,
71
        Closure $onException
72
    ): void {
73
        $this->send(
171✔
74
            HttpMethod::Post,
171✔
75
            $url,
171✔
76
            $body,
171✔
77
            $contentType,
171✔
78
            $userCredentials,
171✔
79
            $onSuccess,
171✔
80
            $onException
171✔
81
        );
171✔
82
    }
83

84
    public function delete(
85
        string $url,
86
        ?UserCredentials $userCredentials,
87
        Closure $onSuccess,
88
        Closure $onException
89
    ): void {
90
        $this->receive(
138✔
91
            HttpMethod::Delete,
138✔
92
            $url,
138✔
93
            $userCredentials,
138✔
94
            $onSuccess,
138✔
95
            $onException
138✔
96
        );
138✔
97
    }
98

99
    public function put(
100
        string $url,
101
        string $body,
102
        string $contentType,
103
        ?UserCredentials $userCredentials,
104
        Closure $onSuccess,
105
        Closure $onException
106
    ): void {
107
        $this->send(
3✔
108
            HttpMethod::Put,
3✔
109
            $url,
3✔
110
            $body,
3✔
111
            $contentType,
3✔
112
            $userCredentials,
3✔
113
            $onSuccess,
3✔
114
            $onException
3✔
115
        );
3✔
116
    }
117

118
    private function receive(
119
        string $method,
120
        string $url,
121
        ?UserCredentials $userCredentials,
122
        Closure $onSuccess,
123
        Closure $onException,
124
        string $hostHeader = ''
125
    ): void {
126
        $request = new Request($url, $method);
166✔
127

128
        if (null !== $userCredentials) {
166✔
129
            $this->addAuthenticationHeader($request, $userCredentials);
166✔
130
        }
131

132
        if ('' !== $hostHeader) {
166✔
133
            $request->setHeader('Host', $hostHeader);
×
134
        }
135

136
        $this->handleRequest($request, $onSuccess, $onException);
166✔
137
    }
138

139
    private function send(
140
        string $method,
141
        string $url,
142
        string $body,
143
        string $contentType,
144
        ?UserCredentials $userCredentials,
145
        Closure $onSuccess,
146
        Closure $onException
147
    ): void {
148
        $request = new Request($url, $method);
172✔
149

150
        if (null !== $userCredentials) {
172✔
151
            $this->addAuthenticationHeader($request, $userCredentials);
172✔
152
        }
153

154
        $request->setHeader('Content-Type', $contentType);
172✔
155
        $request->setHeader('Content-Length', (string) \strlen($body));
172✔
156
        $request->setBody($body);
172✔
157

158
        $this->handleRequest($request, $onSuccess, $onException);
172✔
159
    }
160

161
    private function addAuthenticationHeader(
162
        Request $request,
163
        UserCredentials $userCredentials
164
    ): void {
165
        $httpAuthentication = \sprintf(
179✔
166
            '%s:%s',
179✔
167
            $userCredentials->username(),
179✔
168
            $userCredentials->password()
179✔
169
        );
179✔
170

171
        $encodedCredentials = \base64_encode($httpAuthentication);
179✔
172

173
        $request->setHeader('Authorization', 'Basic ' . $encodedCredentials);
179✔
174
    }
175

176
    private function handleRequest(Request $request, Closure $onSuccess, Closure $onException): void
177
    {
178
        try {
179
            $response = $this->httpClient->request($request);
179✔
180
        } catch (Throwable $e) {
×
181
            $onException($e);
×
182

183
            return;
×
184
        }
185

186
        $onSuccess($response);
179✔
187
    }
188
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc