Skip to content

Commit

Permalink
Make ClientFactory configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
addshore committed Dec 29, 2015
1 parent 0c2959d commit f0bd199
Showing 1 changed file with 23 additions and 2 deletions.
25 changes: 23 additions & 2 deletions src/Guzzle/ClientFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,18 @@ class ClientFactory implements LoggerAwareInterface {

private $client;
private $logger;
private $config;

public function __construct() {
/**
* @since 2.1.0
*
* @param array $config with possible keys:
* middleware => array of extra middleware to pass to guzzle
* user-agent => string default user agent to use for requests
*/
public function __construct( array $config = array() ) {
$this->logger = new NullLogger();
$this->config = $config;
}

/**
Expand All @@ -45,10 +54,22 @@ private function newClient() {
$handlerStack = HandlerStack::create( new CurlHandler() );
$handlerStack->push( $middlewareFactory->retry() );

if( array_key_exists( 'user-agent', $this->config ) ) {
$ua = $this->config['user-agent'];
} else {
$ua = 'Addwiki - mediawiki-api-base';
}

if( array_key_exists( 'middleware', $this->config ) ) {
foreach( $this->config['middleware'] as $middleware ) {
$handlerStack->push( $middleware );
}
}

return new Client( array(
'cookies' => true,
'handler' => $handlerStack,
'headers' => array( 'User-Agent' => 'Addwiki - mediawiki-api-base' ),
'headers' => array( 'User-Agent' => $ua ),
) );
}

Expand Down

0 comments on commit f0bd199

Please sign in to comment.