Use Guzzle's RequestOptions constants
authorTim Düsterhus <duesterhus@woltlab.com>
Thu, 11 Feb 2021 14:30:04 +0000 (15:30 +0100)
committerTim Düsterhus <duesterhus@woltlab.com>
Thu, 11 Feb 2021 14:30:04 +0000 (15:30 +0100)
These allow one to easily view the option's description within the IDE.

wcfsetup/install/files/lib/action/AbstractOauth2Action.class.php
wcfsetup/install/files/lib/action/ImageProxyAction.class.php
wcfsetup/install/files/lib/system/background/job/DownloadRankImageJob.class.php
wcfsetup/install/files/lib/system/io/HttpFactory.class.php
wcfsetup/install/files/lib/system/style/FontManager.class.php
wcfsetup/install/files/lib/util/HTTPRequest.class.php

index 19764d66c92da04e6b4a08c063729d6dca4a69b1..7bee94bdd449ea942b839eef182eb669c3364471 100644 (file)
@@ -4,6 +4,7 @@ namespace wcf\action;
 
 use GuzzleHttp\ClientInterface;
 use GuzzleHttp\Psr7\Request;
+use GuzzleHttp\RequestOptions;
 use ParagonIE\ConstantTime\Hex;
 use Psr\Http\Client\ClientExceptionInterface;
 use wcf\system\exception\NamedUserException;
@@ -52,7 +53,7 @@ abstract class AbstractOauth2Action extends AbstractAction
     {
         if (!$this->httpClient) {
             $this->httpClient = HttpFactory::makeClient([
-                'timeout' => 5,
+                RequestOptions::TIMEOUT => 5,
             ]);
         }
 
index c5b945a5e82c40eb0031996740320bdb7d40930c..d67272342231fc73aa8dbaa123bb64049c6a2a94 100644 (file)
@@ -4,6 +4,7 @@ namespace wcf\action;
 
 use GuzzleHttp\Exception\TransferException;
 use GuzzleHttp\Psr7\Request;
+use GuzzleHttp\RequestOptions;
 use wcf\system\exception\IllegalLinkException;
 use wcf\system\exception\SystemException;
 use wcf\system\io\File;
@@ -105,7 +106,7 @@ class ImageProxyAction extends AbstractAction
                     // download image
                     try {
                         $client = HttpFactory::makeClient([
-                            'timeout' => 10,
+                            RequestOptions::TIMEOUT => 10,
                         ]);
                         $request = new Request('GET', $url, [
                             'via' => '1.1 wsc',
index 31cba42af6f90c5af1a522f6db7cb57c6932e8ec..485957403ac0490f036965d8badb9c01c49e0d97 100644 (file)
@@ -3,6 +3,7 @@
 namespace wcf\system\background\job;
 
 use GuzzleHttp\Psr7\Request;
+use GuzzleHttp\RequestOptions;
 use wcf\data\user\rank\UserRank;
 use wcf\data\user\rank\UserRankEditor;
 use wcf\system\io\HttpFactory;
@@ -64,7 +65,7 @@ class DownloadRankImageJob extends AbstractBackgroundJob
         $extension = \pathinfo(Url::parse($rank->rankImage)['path'], \PATHINFO_EXTENSION);
         if (\in_array($extension, ['gif', 'png', 'jpg', 'jpeg', 'svg', 'webp'])) {
             $http = HttpFactory::makeClient([
-                'timeout' => 10,
+                RequestOptions::TIMEOUT => 10,
             ]);
 
             $imageDest = WCF_DIR . UserRank::RANK_IMAGE_DIR . $rank->rankID . '-rankImage.' . $extension;
index bb481058a25345666d6dddf5aaaab788b981cba8..205c50e8f7da05abdaec63eba7f92afcbbe08bd3 100644 (file)
@@ -4,6 +4,7 @@ namespace wcf\system\io;
 
 use GuzzleHttp\Client;
 use GuzzleHttp\ClientInterface;
+use GuzzleHttp\RequestOptions;
 
 /**
  * Factory for HTTP Clients.
@@ -61,9 +62,9 @@ final class HttpFactory
     public static function makeClient(array $options = [])
     {
         $defaults = [
-            'proxy' => PROXY_SERVER_HTTP,
-            'headers' => [],
-            'timeout' => 60,
+            RequestOptions::PROXY => PROXY_SERVER_HTTP,
+            RequestOptions::HEADERS => [],
+            RequestOptions::TIMEOUT => 60,
         ];
 
         foreach ($defaults as $key => $value) {
@@ -73,14 +74,14 @@ final class HttpFactory
         }
 
         $foundUserAgent = false;
-        foreach ($options['headers'] as $headerName => $value) {
+        foreach ($options[RequestOptions::HEADERS] as $headerName => $value) {
             if (\strtolower($headerName) === 'user-agent') {
                 $foundUserAgent = true;
                 break;
             }
         }
         if (!$foundUserAgent) {
-            $options['headers']['user-agent'] = self::getDefaultUserAgent();
+            $options[RequestOptions::HEADERS]['user-agent'] = self::getDefaultUserAgent();
         }
 
         return new Client($options);
index 307032e47a2812f61a2abc79a6fa424ed8f7d55f..d573067b0232704f2a4b843414eaff95f49f50a9 100644 (file)
@@ -6,6 +6,7 @@ use GuzzleHttp\ClientInterface;
 use GuzzleHttp\Exception\ClientException;
 use GuzzleHttp\Exception\RequestException;
 use GuzzleHttp\Psr7\Request;
+use GuzzleHttp\RequestOptions;
 use wcf\system\io\AtomicWriter;
 use wcf\system\io\HttpFactory;
 use wcf\system\SingletonFactory;
@@ -36,7 +37,7 @@ class FontManager extends SingletonFactory
     {
         $this->http = HttpFactory::makeClient([
             'base_uri' => 'https://fonts.woltlab.com/',
-            'timeout' => 10,
+            RequestOptions::TIMEOUT => 10,
         ]);
     }
 
index 3a5cf274679a2fbb496547b2615421407b107080..c34e21f8738754f48730c968aef4230ed66c1901 100644 (file)
@@ -6,6 +6,7 @@ use GuzzleHttp\Exception\BadResponseException;
 use GuzzleHttp\Exception\TooManyRedirectsException;
 use GuzzleHttp\Exception\TransferException;
 use GuzzleHttp\Psr7\Request;
+use GuzzleHttp\RequestOptions;
 use ParagonIE\ConstantTime\Hex;
 use Psr\Http\Message\RequestInterface;
 use Psr\Http\Message\ResponseInterface;
@@ -188,20 +189,20 @@ final class HTTPRequest
 
         $options = [
             // No overall timeout
-            'timeout' => 0,
-            'connect_timeout' => $this->options['timeout'],
-            'read_timeout' => $this->options['timeout'],
-            'allow_redirects' => [
+            RequestOptions::TIMEOUT => 0,
+            RequestOptions::CONNECT_TIMEOUT => $this->options['timeout'],
+            RequestOptions::READ_TIMEOUT => $this->options['timeout'],
+            RequestOptions::ALLOW_REDIRECTS => [
                 'max' => $this->options['maxDepth'],
                 'track_redirects' => true,
                 'on_redirect' => $redirectHandler,
             ],
         ];
         if (isset($this->options['maxLength'])) {
-            $options['stream'] = true;
+            $options[RequestOptions::STREAM] = true;
         }
         if (isset($this->options['auth'])) {
-            $options['auth'] = [
+            $options[RequestOptions::AUTH] = [
                 $this->options['auth']['username'],
                 $this->options['auth']['password'],
             ];