-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
10 changed files
with
377 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
namespace MaxBeckers\AmazonAlexa\Exception; | ||
|
||
/** | ||
* @author Maximilian Beckers <[email protected]> | ||
*/ | ||
class DeviceApiCallException extends \Exception | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
<?php | ||
|
||
namespace MaxBeckers\AmazonAlexa\Helper; | ||
|
||
use GuzzleHttp\Client; | ||
use MaxBeckers\AmazonAlexa\Exception\DeviceApiCallException; | ||
use MaxBeckers\AmazonAlexa\Exception\MissingRequestDataException; | ||
use MaxBeckers\AmazonAlexa\Request\Device\DeviceAddressInformation; | ||
use MaxBeckers\AmazonAlexa\Request\Request; | ||
|
||
/** | ||
* This helper class can call the amazon api to get address information. | ||
* For more details @see https=>//developer.amazon.com/de/docs/custom-skills/device-address-api.html. | ||
* | ||
* @author Maximilian Beckers <[email protected]> | ||
*/ | ||
class DeviceAddressInformationHelper | ||
{ | ||
/** | ||
* @var Client | ||
*/ | ||
private $client; | ||
|
||
/** | ||
* @param Client|null $client | ||
*/ | ||
public function __construct(Client $client = null) | ||
{ | ||
$this->client = $client ?: new Client(); | ||
} | ||
|
||
/** | ||
* @param Request $request | ||
* | ||
* @throws MissingRequestDataException | ||
* | ||
* @return DeviceAddressInformation | ||
*/ | ||
public function getCountryAndPostalCode(Request $request): DeviceAddressInformation | ||
{ | ||
if (!isset($request->context->system->device->deviceId, $request->context->system->apiAccessToken, $request->context->system->apiEndpoint)) { | ||
throw new MissingRequestDataException(); | ||
} | ||
|
||
$deviceId = $request->context->system->device->deviceId; | ||
$token = $request->context->system->apiAccessToken; | ||
$endpoint = $request->context->system->apiEndpoint; | ||
|
||
$url = sprintf('%s/v1/devices/%s/settings/address/countryAndPostalCode', $endpoint, $deviceId); | ||
|
||
return $this->apiCall($url, $token); | ||
} | ||
|
||
/** | ||
* @param Request $request | ||
* | ||
* @throws MissingRequestDataException | ||
* | ||
* @return DeviceAddressInformation | ||
*/ | ||
public function getAddress(Request $request): DeviceAddressInformation | ||
{ | ||
if (!isset($request->context->system->device->deviceId, $request->context->system->apiAccessToken, $request->context->system->apiEndpoint)) { | ||
throw new MissingRequestDataException(); | ||
} | ||
|
||
$deviceId = $request->context->system->device->deviceId; | ||
$token = $request->context->system->apiAccessToken; | ||
$endpoint = $request->context->system->apiEndpoint; | ||
|
||
$url = sprintf('%s/v1/devices/%s/settings/address', $endpoint, $deviceId); | ||
|
||
return $this->apiCall($url, $token); | ||
} | ||
|
||
/** | ||
* @param string $url | ||
* @param string $token | ||
* | ||
* @throws DeviceApiCallException | ||
* | ||
* @return DeviceAddressInformation | ||
*/ | ||
private function apiCall(string $url, string $token): DeviceAddressInformation | ||
{ | ||
$response = $this->client->request('GET', $url, [ | ||
'headers' => [ | ||
'Authorization' => 'Basic '.$token, | ||
'Accept' => 'application/json', | ||
], | ||
]); | ||
|
||
/* | ||
* Api Call response codes: | ||
* 200 OK Successfully got the address associated with this deviceId. | ||
* 204 No Content The query did not return any results. | ||
* 403 Forbidden The authentication token is invalid or doesn’t have access to the resource. | ||
* 405 Method Not Allowed The method is not supported. | ||
* 429 Too Many Requests The skill has been throttled due to an excessive number of requests. | ||
* 500 Internal Error An unexpected error occurred. | ||
*/ | ||
switch ($response->getStatusCode()) { | ||
case 200: | ||
break; | ||
case 204: | ||
case 403: | ||
case 405: | ||
case 429: | ||
case 500: | ||
default: | ||
throw new DeviceApiCallException(sprintf('Error in api call (status code:"%s")', $response->getStatusCode())); | ||
} | ||
|
||
return DeviceAddressInformation::fromApiResponse(json_decode($response->getBody()->getContents(), true)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
<?php | ||
|
||
namespace MaxBeckers\AmazonAlexa\Request\Device; | ||
|
||
/** | ||
* @author Maximilian Beckers <[email protected]> | ||
*/ | ||
class DeviceAddressInformation | ||
{ | ||
/** | ||
* @var string|null | ||
*/ | ||
public $stateOrRegion; | ||
|
||
/** | ||
* @var string|null | ||
*/ | ||
public $city; | ||
|
||
/** | ||
* @var string|null | ||
*/ | ||
public $countryCode; | ||
|
||
/** | ||
* @var string|null | ||
*/ | ||
public $postalCode; | ||
|
||
/** | ||
* @var string|null | ||
*/ | ||
public $addressLine1; | ||
|
||
/** | ||
* @var string|null | ||
*/ | ||
public $addressLine2; | ||
|
||
/** | ||
* @var string|null | ||
*/ | ||
public $addressLine3; | ||
|
||
/** | ||
* @var string|null | ||
*/ | ||
public $districtOrCounty; | ||
|
||
/** | ||
* @param array $amazonApiResponse | ||
* | ||
* @return DeviceAddressInformation | ||
*/ | ||
public static function fromApiResponse(array $amazonApiResponse): self | ||
{ | ||
$deviceAddressInformation = new self(); | ||
|
||
$deviceAddressInformation->stateOrRegion = isset($amazonApiResponse['stateOrRegion']) ? $amazonApiResponse['stateOrRegion'] : null; | ||
$deviceAddressInformation->city = isset($amazonApiResponse['city']) ? $amazonApiResponse['city'] : null; | ||
$deviceAddressInformation->countryCode = isset($amazonApiResponse['countryCode']) ? $amazonApiResponse['countryCode'] : null; | ||
$deviceAddressInformation->postalCode = isset($amazonApiResponse['postalCode']) ? $amazonApiResponse['postalCode'] : null; | ||
$deviceAddressInformation->addressLine1 = isset($amazonApiResponse['addressLine1']) ? $amazonApiResponse['addressLine1'] : null; | ||
$deviceAddressInformation->addressLine2 = isset($amazonApiResponse['addressLine2']) ? $amazonApiResponse['addressLine2'] : null; | ||
$deviceAddressInformation->addressLine3 = isset($amazonApiResponse['addressLine3']) ? $amazonApiResponse['addressLine3'] : null; | ||
$deviceAddressInformation->districtOrCounty = isset($amazonApiResponse['districtOrCounty']) ? $amazonApiResponse['districtOrCounty'] : null; | ||
|
||
return $deviceAddressInformation; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.