From e5a6784c1697b1700450132575082e7a362d598b Mon Sep 17 00:00:00 2001 From: Vedmaka Date: Wed, 9 Nov 2022 20:03:38 +0400 Subject: [PATCH 1/5] Adds `$config` param for MediaWiki class constructor --- src/Client/Action/ActionApi.php | 9 +++++++-- src/Client/MediaWiki.php | 19 ++++++++++++------- 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/src/Client/Action/ActionApi.php b/src/Client/Action/ActionApi.php index c67de96..523967c 100644 --- a/src/Client/Action/ActionApi.php +++ b/src/Client/Action/ActionApi.php @@ -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(); @@ -61,6 +65,7 @@ public function __construct( $this->auth = $auth; $this->client = $client; $this->tokens = $tokens; + $this->config = $config; $this->logger = new NullLogger(); } @@ -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(); } diff --git a/src/Client/MediaWiki.php b/src/Client/MediaWiki.php index 3255155..a9f9a6d 100644 --- a/src/Client/MediaWiki.php +++ b/src/Client/MediaWiki.php @@ -32,38 +32,43 @@ 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 { - return str_replace( 'rest.php', '', str_replace( self::ACTION_PHP, '', $url ) ); + return str_replace( 'rest.php', '', str_replace( self::ACTION_PHP, '', $url ) ); } /** * @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, $this->config ); } return $this->action; From b0f2036edbba690de614424a8d2cd6199311ca61 Mon Sep 17 00:00:00 2001 From: Vedmaka Date: Wed, 9 Nov 2022 20:07:07 +0400 Subject: [PATCH 2/5] Fixes up indents --- src/Client/Action/ActionApi.php | 2 +- src/Client/MediaWiki.php | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/Client/Action/ActionApi.php b/src/Client/Action/ActionApi.php index 523967c..6b9cc49 100644 --- a/src/Client/Action/ActionApi.php +++ b/src/Client/Action/ActionApi.php @@ -44,7 +44,7 @@ class ActionApi implements Requester, LoggerAwareInterface { * @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 + * @param array $config ClientInterface compatible configuration array */ public function __construct( string $apiUrl, diff --git a/src/Client/MediaWiki.php b/src/Client/MediaWiki.php index a9f9a6d..4352ef0 100644 --- a/src/Client/MediaWiki.php +++ b/src/Client/MediaWiki.php @@ -49,12 +49,12 @@ public function __construct( string $baseUrl, AuthMethod $auth = null, array $co * @param AuthMethod|null $auth * @param array $config ClientInterface compatible configuration array */ - public static function newFromEndpoint( string $anApiEndpoint, AuthMethod $auth = null, array $config = [] ): self { - return new self( self::pruneActionOrRestPhp( $anApiEndpoint ), $auth, $config ); + 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 { - return str_replace( 'rest.php', '', str_replace( self::ACTION_PHP, '', $url ) ); + return str_replace( 'rest.php', '', str_replace( self::ACTION_PHP, '', $url ) ); } /** @@ -62,8 +62,8 @@ private static function pruneActionOrRestPhp( string $url ): string { * @param AuthMethod|null $auth * @param array $config ClientInterface compatible configuration array */ - public static function newFromPage( string $pageUrl, AuthMethod $auth = null, array $config = [] ): self { - return new self( ReallySimpleDiscovery::baseFromPage( $pageUrl ), $auth, $config ); + public static function newFromPage( string $pageUrl, AuthMethod $auth = null, array $config = [] ): self { + return new self( ReallySimpleDiscovery::baseFromPage( $pageUrl ), $auth, $config ); } public function action(): ActionApi { From 11d9da0a79266a19de24e1feea4dd1697d1bb4e9 Mon Sep 17 00:00:00 2001 From: Vedmaka Date: Wed, 9 Nov 2022 20:08:48 +0400 Subject: [PATCH 3/5] Fixes up indentation --- src/Client/Action/ActionApi.php | 6 +++--- src/Client/MediaWiki.php | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/Client/Action/ActionApi.php b/src/Client/Action/ActionApi.php index 6b9cc49..474cd57 100644 --- a/src/Client/Action/ActionApi.php +++ b/src/Client/Action/ActionApi.php @@ -51,7 +51,7 @@ public function __construct( AuthMethod $auth = null, ?ClientInterface $client = null, Tokens $tokens = null, - array $config = [] + array $config = [] ) { if ( $auth === null ) { $auth = new NoAuth(); @@ -65,7 +65,7 @@ public function __construct( $this->auth = $auth; $this->client = $client; $this->tokens = $tokens; - $this->config = $config; + $this->config = $config; $this->logger = new NullLogger(); } @@ -76,7 +76,7 @@ public function getApiUrl(): string { private function getClient(): ClientInterface { if ( !$this->client instanceof ClientInterface ) { - $clientFactory = new ClientFactory( $this->config ); + $clientFactory = new ClientFactory( $this->config ); $clientFactory->setLogger( $this->logger ); $this->client = $clientFactory->getClient(); } diff --git a/src/Client/MediaWiki.php b/src/Client/MediaWiki.php index 4352ef0..5a7d349 100644 --- a/src/Client/MediaWiki.php +++ b/src/Client/MediaWiki.php @@ -32,16 +32,16 @@ class MediaWiki { private RestApi $rest; - private array $config; + private array $config; - public function __construct( string $baseUrl, AuthMethod $auth = null, 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; + $this->config = $config; } /** From 78b2f099b848b3d4aa820e2181273c8e808f1a2f Mon Sep 17 00:00:00 2001 From: Vedmaka Date: Wed, 9 Nov 2022 20:14:23 +0400 Subject: [PATCH 4/5] Adds $config setting to ActionApi and RestApi calls --- src/Client/MediaWiki.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Client/MediaWiki.php b/src/Client/MediaWiki.php index 5a7d349..e626f28 100644 --- a/src/Client/MediaWiki.php +++ b/src/Client/MediaWiki.php @@ -68,7 +68,7 @@ public static function newFromPage( string $pageUrl, AuthMethod $auth = null, ar public function action(): ActionApi { if ( !isset( $this->action ) ) { - $this->action = new ActionApi( $this->baseUrl . self::ACTION_PHP, $this->auth, $this->config ); + $this->action = new ActionApi( $this->baseUrl . self::ACTION_PHP, $this->auth, null, null, $this->config ); } return $this->action; @@ -77,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; From 8c14df4e91ba05a793b54863fbcedbde922e2c58 Mon Sep 17 00:00:00 2001 From: Vedmaka Date: Mon, 21 Nov 2022 01:08:07 +0400 Subject: [PATCH 5/5] Adds extra `noretry` config param to prevent retry middleware from being added --- src/Guzzle/ClientFactory.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Guzzle/ClientFactory.php b/src/Guzzle/ClientFactory.php index 435c7be..3216c89 100644 --- a/src/Guzzle/ClientFactory.php +++ b/src/Guzzle/ClientFactory.php @@ -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 ); }