88use CloudPlayDev \ConfluenceClient \HttpClient \Plugin \TokenAuthentication ;
99use Http \Client \Common \HttpMethodsClientInterface ;
1010use Http \Client \Common \Plugin \AddHostPlugin ;
11+ use Http \Client \Common \Plugin \AddPathPlugin ;
12+ use Http \Client \Common \Plugin \AuthenticationPlugin ;
1113use Http \Client \Common \Plugin \HeaderDefaultsPlugin ;
14+ use Http \Message \Authentication \BasicAuth ;
1215
1316class ConfluenceClient
1417{
1518 /**
1619 * Default User-Agent send with every request
20+ * Use protected visibility to facilitate extension
21+ * @see http://fabien.potencier.org/pragmatism-over-theory-protected-vs-private.html
1722 */
18- private const USER_AGENT = 'cloudplaydev-confluence-php-client/0.1.0 ' ;
19- private Builder $ httpClientBuilder ;
23+ protected const USER_AGENT = 'cloudplaydev-confluence-php-client/0.1.0 ' ;
24+
25+ /**
26+ * Use protected visibility to facilitate extension
27+ * @see http://fabien.potencier.org/pragmatism-over-theory-protected-vs-private.html
28+ */
29+ protected Builder $ httpClientBuilder ;
2030
2131 public function __construct (string $ confluenceHostUrl , Builder $ httpClientBuilder = null )
2232 {
@@ -51,13 +61,31 @@ public function getHttpClient(): HttpMethodsClientInterface
5161 return $ this ->httpClientBuilder ->getHttpClient ();
5262 }
5363
64+ /**
65+ * Register basic properties about original URI in order to
66+ * restore them on every request
67+ *
68+ * @param string $url
69+ * @return void
70+ */
5471 public function setUrl (string $ url ): void
5572 {
5673 $ uri = $ this ->httpClientBuilder ->getUriFactory ()->createUri ($ url );
5774
5875 $ this ->httpClientBuilder ->removePlugin (AddHostPlugin::class);
5976 $ this ->httpClientBuilder ->addPlugin (new AddHostPlugin ($ uri ));
60- }
6177
78+ // Prepend path if any
79+ if ( $ uri ->getPath () ) {
80+ $ this ->httpClientBuilder ->removePlugin (AddPathPlugin::class);
81+ $ this ->httpClientBuilder ->addPlugin (new AddPathPlugin ($ uri ));
82+ }
6283
63- }
84+ // Report userInfo as Basic authentication
85+ if ( $ uri ->getUserInfo () ) {
86+ list ( $ user , $ token ) = explode ( ': ' , $ uri ->getUserInfo () , 2 );
87+ $ this ->httpClientBuilder ->removePlugin (AuthenticationPlugin::class);
88+ $ this ->httpClientBuilder ->addPlugin (new AuthenticationPlugin ( new BasicAuth ( $ user , $ token ) ) );
89+ }
90+ }
91+ }
0 commit comments