2018-11-28 16:49:18 -06:00
|
|
|
<?php
|
|
|
|
|
/**
|
|
|
|
|
* MapquestGeocodeApi.php
|
|
|
|
|
*
|
|
|
|
|
* -Description-
|
|
|
|
|
*
|
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2021-02-09 00:29:04 +01:00
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2018-11-28 16:49:18 -06:00
|
|
|
*
|
2021-02-09 00:29:04 +01:00
|
|
|
* @link https://www.librenms.org
|
2021-09-10 20:09:53 +02:00
|
|
|
*
|
2018-11-28 16:49:18 -06:00
|
|
|
* @copyright 2018 Tony Murray
|
|
|
|
|
* @author Tony Murray <murraytony@gmail.com>
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
namespace App\ApiClients;
|
|
|
|
|
|
|
|
|
|
use Exception;
|
2022-01-29 21:09:05 -06:00
|
|
|
use Illuminate\Http\Client\Response;
|
2018-11-28 16:49:18 -06:00
|
|
|
use LibreNMS\Config;
|
|
|
|
|
use LibreNMS\Interfaces\Geocoder;
|
|
|
|
|
|
|
|
|
|
class MapquestApi extends BaseApi implements Geocoder
|
|
|
|
|
{
|
|
|
|
|
use GeocodingHelper;
|
|
|
|
|
|
2022-11-02 01:06:15 +01:00
|
|
|
protected string $base_uri = 'https://open.mapquestapi.com';
|
|
|
|
|
protected string $geocoding_uri = '/geocoding/v1/address';
|
2018-11-28 16:49:18 -06:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get latitude and longitude from geocode response
|
|
|
|
|
*/
|
2022-11-02 01:06:15 +01:00
|
|
|
protected function parseLatLng(array $data): array
|
2018-11-28 16:49:18 -06:00
|
|
|
{
|
|
|
|
|
return [
|
|
|
|
|
'lat' => isset($data['results'][0]['locations'][0]['latLng']['lat']) ? $data['results'][0]['locations'][0]['latLng']['lat'] : 0,
|
|
|
|
|
'lng' => isset($data['results'][0]['locations'][0]['latLng']['lng']) ? $data['results'][0]['locations'][0]['latLng']['lng'] : 0,
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2023-05-23 09:25:17 -05:00
|
|
|
* Build request option array
|
2018-11-28 16:49:18 -06:00
|
|
|
*
|
|
|
|
|
* @throws \Exception you may throw an Exception if validation fails
|
|
|
|
|
*/
|
2022-11-02 01:06:15 +01:00
|
|
|
protected function buildGeocodingOptions(string $address): array
|
2018-11-28 16:49:18 -06:00
|
|
|
{
|
|
|
|
|
$api_key = Config::get('geoloc.api_key');
|
|
|
|
|
if (! $api_key) {
|
|
|
|
|
throw new Exception('MapQuest API key missing, set geoloc.api_key');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return [
|
|
|
|
|
'query' => [
|
|
|
|
|
'key' => $api_key,
|
|
|
|
|
'location' => $address,
|
|
|
|
|
'thumbMaps' => 'false',
|
|
|
|
|
],
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Checks if the request was a success
|
|
|
|
|
*/
|
2022-01-29 21:09:05 -06:00
|
|
|
protected function checkResponse(Response $response, array $data): bool
|
2018-11-28 16:49:18 -06:00
|
|
|
{
|
2022-01-29 21:09:05 -06:00
|
|
|
return $response->successful() && $data['info']['statuscode'] == 0;
|
2018-11-28 16:49:18 -06:00
|
|
|
}
|
|
|
|
|
}
|