From 682e058634ba977ae26165417567ff8107aa010c Mon Sep 17 00:00:00 2001 From: Dmitry Balabka Date: Wed, 10 Jan 2024 21:06:15 +0200 Subject: [PATCH] Add API key into IpInfo --- src/Provider/IpInfo/IpInfo.php | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/src/Provider/IpInfo/IpInfo.php b/src/Provider/IpInfo/IpInfo.php index fcd857f4f..03de569c4 100644 --- a/src/Provider/IpInfo/IpInfo.php +++ b/src/Provider/IpInfo/IpInfo.php @@ -12,14 +12,16 @@ namespace Geocoder\Provider\IpInfo; -use Geocoder\Collection; +use Geocoder\Exception\InvalidCredentials; use Geocoder\Exception\UnsupportedOperation; -use Geocoder\Http\Provider\AbstractHttpProvider; +use Geocoder\Collection; use Geocoder\Model\Address; use Geocoder\Model\AddressCollection; -use Geocoder\Provider\Provider; use Geocoder\Query\GeocodeQuery; use Geocoder\Query\ReverseQuery; +use Geocoder\Http\Provider\AbstractHttpProvider; +use Geocoder\Provider\Provider; +use Psr\Http\Client\ClientInterface; /** * @author Roro Neutron @@ -31,6 +33,21 @@ final class IpInfo extends AbstractHttpProvider implements Provider */ public const ENDPOINT_URL = 'https://ipinfo.io/%s/json'; + /** + * @var string + */ + private $apiKey = null; + + /** + * @param ClientInterface $client an HTTP adapter + * @param string $apiKey an API key + */ + public function __construct(ClientInterface $client, ?string $apiKey = null) + { + $this->apiKey = $apiKey; + parent::__construct($client); + } + public function geocodeQuery(GeocodeQuery $query): Collection { $address = $query->getText(); @@ -43,7 +60,10 @@ public function geocodeQuery(GeocodeQuery $query): Collection return new AddressCollection([$this->getLocationForLocalhost()]); } - return $this->executeQuery(sprintf(self::ENDPOINT_URL, $address)); + return $this->executeQuery( + sprintf(self::ENDPOINT_URL, $address) + . ($this->apiKey ? '?token=' . $this->apiKey : '') + ); } public function reverseQuery(ReverseQuery $query): Collection