From d1a72232b359696c2422b2305b8605f7acf0b29d Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Mon, 6 Jun 2022 02:21:31 -0400 Subject: [PATCH] Replace ByJG\WebRequest by GuzzleHttp\ClientInterface --- src/classes/Pollers/Server.php | 37 +++++++++++++++++----------------- 1 file changed, 19 insertions(+), 18 deletions(-) diff --git a/src/classes/Pollers/Server.php b/src/classes/Pollers/Server.php index 3be6131..6da2a89 100644 --- a/src/classes/Pollers/Server.php +++ b/src/classes/Pollers/Server.php @@ -2,7 +2,7 @@ namespace Friendica\Directory\Pollers; -use ByJG\Util\WebRequest; +use GuzzleHttp\Psr7\Uri; use GuzzleHttp\Exception\RequestException; use GuzzleHttp\TransferStats; @@ -402,27 +402,26 @@ class Server function discoverPoco($base_url): void { - $pocoUrl = $base_url . '/poco'; + $uri = Uri::withQueryValues(new Uri($base_url . '/poco'), ['fields' => 'urls', 'count' => 1000]); - $webrequest = new WebRequest($pocoUrl); - $pocoJsonData = $webrequest->get(['fields' => 'urls', 'count' => 1000]); + $response = $this->http->request('GET', $uri); - $this->logger->debug('WebRequest: ' . $webrequest->getLastFetchedUrl() . ' Status: ' . $webrequest->getLastStatus()); + $this->logger->debug('WebRequest: ' . $uri . ' Status: ' . $response->getStatusCode()); - if ($webrequest->getLastStatus() != 200) { - $this->logger->info('Unsuccessful poco request: ' . $webrequest->getLastFetchedUrl()); + if ($response->getStatusCode() != 200) { + $this->logger->info('Unsuccessful poco request: ' . $uri); return; } try { - $pocoFetchData = json_decode($pocoJsonData); + $pocoFetchData = json_decode($response->getBody()->getContents()); } catch (\Throwable $e) { - $this->logger->notice('Invalid JSON string for PoCo URL: ' . $webrequest->getLastFetchedUrl()); + $this->logger->notice('Invalid JSON string for PoCo URL: ' . $uri); return; } if (!isset($pocoFetchData->entry)) { - $this->logger->notice('Invalid JSON structure for PoCo URL: ' . $webrequest->getLastFetchedUrl()); + $this->logger->notice('Invalid JSON structure for PoCo URL: ' . $uri); return; } @@ -444,26 +443,28 @@ class Server public function getSubscribeUrl($base_url, $profile) { - $xrdRequest = new WebRequest($base_url . '/xrd'); - $xrdRequest->addRequestHeader('Accept', 'application/jrd+json'); - $xrdJsonData = $xrdRequest->get(['uri' => $profile]); + $uri = Uri::withQueryValues(new Uri($base_url . '/xrd'), ['uri' => $profile]); - $this->logger->debug('WebRequest: ' . $xrdRequest->getLastFetchedUrl() . ' Status: ' . $xrdRequest->getLastStatus()); + $response = $this->http->request('GET', $uri, ['headers' => ['Accept' => 'application/jrd+json']]); - if ($xrdRequest->getLastStatus() != 200) { - $this->logger->info('Unsuccessful XRD request: ' . $xrdRequest->getLastFetchedUrl()); + $xrdJsonData = $response->getBody()->getContents(); + + $this->logger->debug('WebRequest: ' . $uri . ' Status: ' . $response->getStatusCode()); + + if ($response->getStatusCode() != 200) { + $this->logger->info('Unsuccessful XRD request: ' . $uri); return null; } try { $xrdData = json_decode($xrdJsonData); } catch (\Throwable $e) { - $this->logger->notice('Invalid JSON string for XRD URL: ' . $xrdRequest->getLastFetchedUrl()); + $this->logger->notice('Invalid JSON string for XRD URL: ' . $uri); return null; } if (!isset($xrdData->links)) { - $this->logger->notice('Invalid JSON structure for XRD URL: ' . $xrdRequest->getLastFetchedUrl()); + $this->logger->notice('Invalid JSON structure for XRD URL: ' . $uri); return null; }