33namespace Gitlab \HttpClient ;
44
55use Http \Client \Common \HttpMethodsClient ;
6+ use Http \Client \Common \HttpMethodsClientInterface ;
67use Http \Client \Common \Plugin ;
78use Http \Client \Common \PluginClient ;
9+ use Http \Client \Common \Plugin \Cache \Generator \HeaderCacheKeyGenerator ;
10+ use Http \Client \Common \Plugin \CachePlugin ;
811use Http \Client \Common \PluginClientFactory ;
912use Http \Discovery \Psr17FactoryDiscovery ;
1013use Http \Discovery \Psr18ClientDiscovery ;
14+ use Psr \Cache \CacheItemPoolInterface ;
1115use Psr \Http \Client \ClientInterface ;
1216use Psr \Http \Message \RequestFactoryInterface ;
1317use Psr \Http \Message \StreamFactoryInterface ;
1418
1519/**
1620 * A builder that builds the API client.
21+ *
1722 * This will allow you to fluently add and remove plugins.
1823 *
1924 * @author Tobias Nyholm <[email protected] > 25+ * @author Graham Campbell <[email protected] > 2026 */
2127class Builder
2228{
@@ -30,16 +36,20 @@ class Builder
3036 /**
3137 * A HTTP client with all our plugins.
3238 *
33- * @var HttpMethodsClient
39+ * @var HttpMethodsClientInterface
3440 */
3541 private $ pluginClient ;
3642
3743 /**
44+ * The HTTP request factory.
45+ *
3846 * @var RequestFactoryInterface
3947 */
4048 private $ requestFactory ;
4149
4250 /**
51+ * The HTTP stream factory.
52+ *
4353 * @var StreamFactoryInterface
4454 */
4555 private $ streamFactory ;
@@ -52,11 +62,24 @@ class Builder
5262 private $ httpClientModified = true ;
5363
5464 /**
65+ * The currently registered plugins.
66+ *
5567 * @var Plugin[]
5668 */
5769 private $ plugins = [];
5870
5971 /**
72+ * The cache plugin to use.
73+ *
74+ * This plugin is specially treated because it has to be the very last plugin.
75+ *
76+ * @var CachePlugin|null
77+ */
78+ private $ cachePlugin ;
79+
80+ /**
81+ * Create a new http client builder instance.
82+ *
6083 * @param ClientInterface|null $httpClient
6184 * @param RequestFactoryInterface|null $requestFactory
6285 * @param StreamFactoryInterface|null $streamFactory
@@ -66,21 +89,26 @@ public function __construct(
6689 RequestFactoryInterface $ requestFactory = null ,
6790 StreamFactoryInterface $ streamFactory = null
6891 ) {
69- $ this ->httpClient = $ httpClient ?: Psr18ClientDiscovery::find ();
70- $ this ->requestFactory = $ requestFactory ?: Psr17FactoryDiscovery::findRequestFactory ();
71- $ this ->streamFactory = $ streamFactory ?: Psr17FactoryDiscovery::findStreamFactory ();
92+ $ this ->httpClient = $ httpClient ?? Psr18ClientDiscovery::find ();
93+ $ this ->requestFactory = $ requestFactory ?? Psr17FactoryDiscovery::findRequestFactory ();
94+ $ this ->streamFactory = $ streamFactory ?? Psr17FactoryDiscovery::findStreamFactory ();
7295 }
7396
7497 /**
75- * @return HttpMethodsClient
98+ * @return HttpMethodsClientInterface
7699 */
77100 public function getHttpClient ()
78101 {
79102 if ($ this ->httpClientModified ) {
80103 $ this ->httpClientModified = false ;
81104
105+ $ plugins = $ this ->plugins ;
106+ if ($ this ->cachePlugin !== null ) {
107+ $ plugins [] = $ this ->cachePlugin ;
108+ }
109+
82110 $ this ->pluginClient = new HttpMethodsClient (
83- (new PluginClientFactory ())->createClient ($ this ->httpClient , $ this -> plugins ),
111+ (new PluginClientFactory ())->createClient ($ this ->httpClient , $ plugins ),
84112 $ this ->requestFactory
85113 );
86114 }
@@ -92,6 +120,8 @@ public function getHttpClient()
92120 * Add a new plugin to the end of the plugin chain.
93121 *
94122 * @param Plugin $plugin
123+ *
124+ * @return void
95125 */
96126 public function addPlugin (Plugin $ plugin )
97127 {
@@ -103,6 +133,8 @@ public function addPlugin(Plugin $plugin)
103133 * Remove a plugin by its fully qualified class name (FQCN).
104134 *
105135 * @param string $fqcn
136+ *
137+ * @return void
106138 */
107139 public function removePlugin ($ fqcn )
108140 {
@@ -113,4 +145,33 @@ public function removePlugin($fqcn)
113145 }
114146 }
115147 }
148+
149+ /**
150+ * Add a cache plugin to cache responses locally.
151+ *
152+ * @param CacheItemPoolInterface $cachePool
153+ * @param array $config
154+ *
155+ * @return void
156+ */
157+ public function addCache (CacheItemPoolInterface $ cachePool , array $ config = [])
158+ {
159+ if (!isset ($ config ['cache_key_generator ' ])) {
160+ $ config ['cache_key_generator ' ] = new HeaderCacheKeyGenerator (['Authorization ' , 'Cookie ' , 'Accept ' , 'Content-type ' ]);
161+ }
162+
163+ $ this ->cachePlugin = CachePlugin::clientCache ($ cachePool , $ this ->streamFactory , $ config );
164+ $ this ->httpClientModified = true ;
165+ }
166+
167+ /**
168+ * Remove the cache plugin.
169+ *
170+ * @return void
171+ */
172+ public function removeCache ()
173+ {
174+ $ this ->cachePlugin = null ;
175+ $ this ->httpClientModified = true ;
176+ }
116177}
0 commit comments