Skip to content

Instantly share code, notes, and snippets.

@clnt
Last active March 8, 2020 04:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save clnt/0d74367effadeac3cd34f6c7ac8a06e6 to your computer and use it in GitHub Desktop.
Save clnt/0d74367effadeac3cd34f6c7ac8a06e6 to your computer and use it in GitHub Desktop.
HERE Token Request OAuth Header
<?php
namespace App\Admin\Geocoding\Adapters;
use CraftMap\Admin\Geocoding\Adapters\Transformers\Transformer;
use CraftMap\Admin\Geocoding\Headers\OAuth1Header;
use Illuminate\Http\Client\Response;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;
class HereAdapter extends Adapter
{
/**
* @var string|null
*/
protected $apiId;
/**
* @var string|null
*/
protected $apiAccessId;
/**
* @var string|null
*/
protected $apiAccessSecret;
/**
* @var string
*/
protected $tokenUrl = 'https://account.api.here.com/oauth2/token';
/**
* Default token expiry time is 24 hours.
*
* @var int
*/
protected $tokenExpiry = 87000;
/**
* @var string
*/
protected $apiUrl = 'https://discover.search.hereapi.com/v1/geocode?q=';
/**
* @var string
*/
protected $cacheKey = 'services.here.access_token';
/**
* Expire cached token at 23.5 hours.
*
* @var int
*/
protected $cacheExpiry = 84600;
public function __construct(
Transformer $transformer,
?string $apiId,
?string $apiAccessId,
?string $apiAccessSecret
) {
parent::__construct($transformer);
$this->apiId = $apiId;
$this->apiAccessId = $apiAccessId;
$this->apiAccessSecret = $apiAccessSecret;
}
public function geocode( // phpcs:ignore
string $query,
array $options = []
): ?Response {
$response = Http::withHeaders($this->getRequestHeaders())
->get($this->getRequestUrl($query));
if ($response->successful()) {
return $response;
}
if ($response->clientError()) {
// Handle client error 400, auth or validation typically.
return null;
}
if ($response->serverError()) {
Log::channel('geocoding')->info($response->body());
return null;
}
return null;
}
protected function getRequestUrl(string $query): string
{
return $this->apiUrl . urlencode($query);
}
protected function getRequestHeaders(array $headers = []): array
{
return array_merge([
'Authorization' => 'Bearer ' . $this->getAccessToken(),
], $headers);
}
protected function getAccessToken(): string
{
if (config('services.geocoding.here.override') === true) {
return config('services.geocoding.here.access_token');
}
if (cache()->has($this->cacheKey)) {
return cache($this->cacheKey);
}
$response = Http::withHeaders(
$this->getTokenHeaders($this->getTokenBody())
)->post($this->tokenUrl, $this->getTokenBody());
return $response->body();
}
private function getTokenBody(): array
{
return [
'grant_type' => 'client_credentials',
'client_id' => $this->apiAccessId,
'client_secret' => $this->apiAccessSecret,
'expires_in' => $this->tokenExpiry,
];
}
protected function getTokenHeaders(array $body): array
{
return [
'Content-Type' => 'application/x-www-form-urlencoded',
'Authorization' => $this->createOAuthHeader($body),
];
}
protected function createOAuthHeader(array $body): string
{
return OAuth1Header::make(
$this->tokenUrl,
[
'consumer_key' => $this->apiAccessId,
'consumer_secret' => $this->apiAccessSecret,
'request_method' => OAuth1Header::REQUEST_METHOD_HEADER,
'signature_method' => OAuth1Header::SIGNATURE_METHOD_HMAC_256,
],
'POST',
$body
)->build();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment