9use InvalidArgumentException;
15use Psr\Log\LoggerInterface;
37 private readonly LoggerInterface $logger,
40 $options->assertRequiredOptions( self::CONSTRUCTOR_OPTIONS );
77 public function create(
$url, array $options = [], $caller = __METHOD__ ) {
78 if ( !isset( $options[
'logger'] ) ) {
79 $options[
'logger'] = $this->logger;
81 $options[
'timeout'] = $this->normalizeTimeout(
82 $options[
'timeout'] ??
null,
83 $options[
'maxTimeout'] ??
null,
87 $options[
'connectTimeout'] = $this->normalizeTimeout(
88 $options[
'connectTimeout'] ??
null,
89 $options[
'maxConnectTimeout'] ??
null,
94 if ( $this->telemetry ) {
95 $client->addTelemetry( $this->telemetry );
110 private function normalizeTimeout( $parameter, $maxParameter, $default, $maxConfigured ) {
111 if ( $parameter ===
'default' || $parameter ===
null ) {
112 if ( !is_numeric( $default ) ) {
113 throw new InvalidArgumentException(
114 '$wgHTTPTimeout and $wgHTTPConnectTimeout must be set to a number' );
120 $max = $maxParameter ?? $maxConfigured;
121 if ( $max && $value > $max ) {
134 return function_exists(
'curl_init' ) ||
wfIniGetBool(
'allow_url_fopen' );
148 public function request( $method,
$url, array $options = [], $caller = __METHOD__ ) {
149 $logger = LoggerFactory::getInstance(
'http' );
150 $logger->debug(
"$method: $url" );
152 $options[
'method'] = strtoupper( $method );
154 $req = $this->
create(
$url, $options, $caller );
155 $status = $req->execute();
157 if ( $status->isOK() ) {
158 return $req->getContent();
160 $errors = array_map(
static fn ( $msg ) => $msg->getKey(), $status->getMessages(
'error' ) );
161 $logger->warning( Status::wrap( $status )->getWikiText(
false,
false,
'en' ),
162 [
'error' => $errors,
'caller' => $caller,
'content' => $req->getContent() ] );
176 public function get(
$url, array $options = [], $caller = __METHOD__ ) {
177 return $this->
request(
'GET',
$url, $options, $caller );
189 public function post(
$url, array $options = [], $caller = __METHOD__ ) {
190 return $this->
request(
'POST',
$url, $options, $caller );
213 $options[
'reqTimeout'] = $this->normalizeTimeout(
214 $options[
'reqTimeout'] ?? $options[
'timeout'] ??
null,
215 $options[
'maxReqTimeout'] ?? $options[
'maxTimeout'] ??
null,
219 $options[
'connTimeout'] = $this->normalizeTimeout(
220 $options[
'connTimeout'] ?? $options[
'connectTimeout'] ??
null,
221 $options[
'maxConnTimeout'] ?? $options[
'maxConnectTimeout'] ??
null,
230 'logger' => $this->logger,
252 $config[
'timeout'] = $this->normalizeTimeout(
253 $config[
'timeout'] ?? null,
254 $config[
'maxTimeout'] ?? null,
259 $config[
'connect_timeout'] = $this->normalizeTimeout(
260 $config[
'connect_timeout'] ??
null,
261 $config[
'maxConnectTimeout'] ??
null,
266 if ( !isset( $config[
'headers'][
'User-Agent'] ) ) {
267 $config[
'headers'][
'User-Agent'] = $this->
getUserAgent();
269 if ( $this->telemetry ) {
270 $config[
'headers'] = array_merge(
271 $this->telemetry->getRequestHeaders(), $config[
'headers']
275 return new Client( $config );
const MW_VERSION
The running version of MediaWiki.
wfIniGetBool( $setting)
Safety wrapper around ini_get() for boolean settings.
A class containing constants representing the names of configuration variables.
const HTTPConnectTimeout
Name constant for the HTTPConnectTimeout setting, for use with Config::get()
const HTTPTimeout
Name constant for the HTTPTimeout setting, for use with Config::get()
const LocalVirtualHosts
Name constant for the LocalVirtualHosts setting, for use with Config::get()
const HTTPMaxConnectTimeout
Name constant for the HTTPMaxConnectTimeout setting, for use with Config::get()
const HTTPMaxTimeout
Name constant for the HTTPMaxTimeout setting, for use with Config::get()
const LocalHTTPProxy
Name constant for the LocalHTTPProxy setting, for use with Config::get()