Skip to content

Commit ba10c88

Browse files
authored
Add "path" and "userInfo" as HTTP Client Builder plugins
Also mark some properties as "protected" for convenience
1 parent 3b9f34a commit ba10c88

File tree

1 file changed

+32
-4
lines changed

1 file changed

+32
-4
lines changed

src/ConfluenceClient.php

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,25 @@
88
use CloudPlayDev\ConfluenceClient\HttpClient\Plugin\TokenAuthentication;
99
use Http\Client\Common\HttpMethodsClientInterface;
1010
use Http\Client\Common\Plugin\AddHostPlugin;
11+
use Http\Client\Common\Plugin\AddPathPlugin;
12+
use Http\Client\Common\Plugin\AuthenticationPlugin;
1113
use Http\Client\Common\Plugin\HeaderDefaultsPlugin;
14+
use Http\Message\Authentication\BasicAuth;
1215

1316
class 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

Comments
 (0)