-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRetrofitBuilder.php
81 lines (67 loc) · 2.06 KB
/
RetrofitBuilder.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<?php
declare(strict_types=1);
namespace Retrofit\Core;
use GuzzleHttp\Psr7\Uri;
use LogicException;
use PhpParser\BuilderFactory;
use PhpParser\PrettyPrinter\Standard;
use Psr\Http\Message\UriInterface;
use Retrofit\Core\Converter\ConverterFactory;
use Retrofit\Core\Internal\BuiltInConverterFactory;
use Retrofit\Core\Internal\ConverterProvider;
use Retrofit\Core\Internal\Proxy\DefaultProxyFactory;
/**
* Build a new {@link Retrofit}.
*
* @api
*/
class RetrofitBuilder
{
private ?HttpClient $httpClient = null;
private ?UriInterface $baseUrl = null;
/** @var list<ConverterFactory> */
private array $converterFactories = [];
/**
* The HTTP Client used for requests.
*/
public function client(HttpClient $httpClient): static
{
$this->httpClient = $httpClient;
return $this;
}
/**
* Set the API base URL.
*/
public function baseUrl(UriInterface|string $baseUrl): static
{
if (is_string($baseUrl)) {
$baseUrl = new Uri($baseUrl);
}
$this->baseUrl = $baseUrl;
return $this;
}
/**
* Add converter factory for serialization and deserialization of objects.
*/
public function addConverterFactory(ConverterFactory $converterFactory): static
{
$this->converterFactories[] = $converterFactory;
return $this;
}
/**
* Create the {@link Retrofit} instance using the configured values.
*/
public function build(): Retrofit
{
if (is_null($this->httpClient)) {
throw new LogicException('Must set HttpClient object to make requests.');
}
if (is_null($this->baseUrl)) {
throw new LogicException('Base URL required.');
}
$this->converterFactories[] = new BuiltInConverterFactory();
$proxyFactory = new DefaultProxyFactory(new BuilderFactory(), new Standard());
$converterProvider = new ConverterProvider($this->converterFactories);
return new Retrofit($this->httpClient, $this->baseUrl, $converterProvider, $proxyFactory);
}
}