Files

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

80 lines
2.4 KiB
PHP
Raw Permalink Normal View History

<?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/>.
*
2021-02-09 00:29:04 +01:00
* @link https://www.librenms.org
2021-09-10 20:09:53 +02: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;
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';
/**
* Get latitude and longitude from geocode response
*/
2022-11-02 01:06:15 +01:00
protected function parseLatLng(array $data): array
{
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,
];
}
/**
* Build request option array
*
* @throws \Exception you may throw an Exception if validation fails
*/
2022-11-02 01:06:15 +01:00
protected function buildGeocodingOptions(string $address): array
{
$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
{
2022-01-29 21:09:05 -06:00
return $response->successful() && $data['info']['statuscode'] == 0;
}
}