Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds optional $config parameter to API instance constructor #51

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/Client/Action/ActionApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,21 @@ class ActionApi implements Requester, LoggerAwareInterface {

private LoggerInterface $logger;

private array $config;

/**
* @param string $apiUrl The API Url
* @param AuthMethod|null $auth Auth method to use. null for NoAuth
* @param ClientInterface|null $client Guzzle Client
* @param Tokens|null $tokens Inject a custom tokens object here
* @param array $config ClientInterface compatible configuration array
*/
public function __construct(
string $apiUrl,
AuthMethod $auth = null,
?ClientInterface $client = null,
Tokens $tokens = null
Tokens $tokens = null,
array $config = []
) {
if ( $auth === null ) {
$auth = new NoAuth();
Expand All @@ -61,6 +65,7 @@ public function __construct(
$this->auth = $auth;
$this->client = $client;
$this->tokens = $tokens;
$this->config = $config;

$this->logger = new NullLogger();
}
Expand All @@ -71,7 +76,7 @@ public function getApiUrl(): string {

private function getClient(): ClientInterface {
if ( !$this->client instanceof ClientInterface ) {
$clientFactory = new ClientFactory();
$clientFactory = new ClientFactory( $this->config );
$clientFactory->setLogger( $this->logger );
$this->client = $clientFactory->getClient();
}
Expand Down
19 changes: 12 additions & 7 deletions src/Client/MediaWiki.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,25 @@ class MediaWiki {

private RestApi $rest;

public function __construct( string $baseUrl, AuthMethod $auth = null ) {
private array $config;

public function __construct( string $baseUrl, AuthMethod $auth = null, array $config = [] ) {
if ( $auth === null ) {
$auth = new NoAuth();
}

$this->baseUrl = $baseUrl;
$this->auth = $auth;
$this->config = $config;
}

/**
* @param string $anApiEndpoint Either the REST or Action API endpoint e.g. https://en.wikipedia.org/w/api.php
* @param AuthMethod|null $auth
* @param array $config ClientInterface compatible configuration array
*/
public static function newFromEndpoint( string $anApiEndpoint, AuthMethod $auth = null ): self {
return new self( self::pruneActionOrRestPhp( $anApiEndpoint ), $auth );
public static function newFromEndpoint( string $anApiEndpoint, AuthMethod $auth = null, array $config = [] ): self {
return new self( self::pruneActionOrRestPhp( $anApiEndpoint ), $auth, $config );
}

private static function pruneActionOrRestPhp( string $url ): string {
Expand All @@ -56,14 +60,15 @@ private static function pruneActionOrRestPhp( string $url ): string {
/**
* @param string $anApiEndpoint A page on a MediaWiki site e.g. https://en.wikipedia.org/wiki/Main_Page
* @param AuthMethod|null $auth
* @param array $config ClientInterface compatible configuration array
*/
public static function newFromPage( string $pageUrl, AuthMethod $auth = null ): self {
return new self( ReallySimpleDiscovery::baseFromPage( $pageUrl ), $auth );
public static function newFromPage( string $pageUrl, AuthMethod $auth = null, array $config = [] ): self {
return new self( ReallySimpleDiscovery::baseFromPage( $pageUrl ), $auth, $config );
}

public function action(): ActionApi {
if ( !isset( $this->action ) ) {
$this->action = new ActionApi( $this->baseUrl . self::ACTION_PHP, $this->auth );
$this->action = new ActionApi( $this->baseUrl . self::ACTION_PHP, $this->auth, null, null, $this->config );
}

return $this->action;
Expand All @@ -72,7 +77,7 @@ public function action(): ActionApi {
public function rest(): RestApi {
if ( !isset( $this->rest ) ) {
// TODO perhaps use the same Tokens object between the 2 APIs
$this->rest = new RestApi( $this->baseUrl . self::REST_PHP, $this->auth, null, new Tokens( $this->action() ) );
$this->rest = new RestApi( $this->baseUrl . self::REST_PHP, $this->auth, null, new Tokens( $this->action() ), $this->config );
}

return $this->rest;
Expand Down
4 changes: 3 additions & 1 deletion src/Guzzle/ClientFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ private function newClient(): Client {
];
$this->setUaHeaderFromConfigOrDefault();
$this->setDefaultHandlerIfNotInConfigAlready();
$this->setMiddlewareFromConfigWithDefaultRetry();
if( !array_key_exists( 'noretry', $this->config ) ) {
$this->setMiddlewareFromConfigWithDefaultRetry();
}
return new Client( $this->config );
}

Expand Down