Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
* develop:
  cs fix
  update changelog
  fix tests
  fixed #20
  • Loading branch information
maxbeckers committed Feb 15, 2018
2 parents 48f98ae + 747b359 commit 31e9c21
Show file tree
Hide file tree
Showing 10 changed files with 377 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .php_cs.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@
'php_unit_construct' => true,
'php_unit_dedicate_assert' => true,
'php_unit_fqcn_annotation' => true,
'php_unit_strict' => true,
'php_unit_strict' => false,
'phpdoc_add_missing_param_annotation' => true,
'phpdoc_align' => true,
'phpdoc_annotation_without_dot' => true,
Expand Down
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Change Log

## [1.1.0](https://github.com/maxbeckers/amazon-alexa-php/tree/1.1.0) (2018-02-15)
[Full Changelog](https://github.com/maxbeckers/amazon-alexa-php/compare/1.0.0...1.1.0)

**Closed issues:**

- Service for device address information [\#20](https://github.com/maxbeckers/amazon-alexa-php/issues/20)

## [1.0.16](https://github.com/maxbeckers/amazon-alexa-php/tree/1.0.16) (2018-02-06)
[Full Changelog](https://github.com/maxbeckers/amazon-alexa-php/compare/1.0.15...1.0.16)

Expand Down
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,13 @@ public function handleRequest(Request $request): Response
return $this->responseHelper->respond('Success :)');
}
```
## Check device address information
To get either "Full Address" or "Country & Postal Code" from the customer you need the permissions for user api call. More informations for the call see [device-address-api](https://developer.amazon.com/de/docs/custom-skills/device-address-api.html).
```php
$helper = new DeviceAddressInformationHelper();
$fullAddress = $helper->getAddress($request);
$countryAndPostalCode = $helper->getCountryAndPostalCode($request);
```
## Generate SSML
For SSML output you can use the `SsmlGenerator`. With the helper will generate valid SSML for alexa. All types of alexa known SSML tags have a function in the `SsmlGeneator`.
You can add all SSML you need to the generator and call `getSsml` to get the full string.
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@
],
"minimum-stability": "dev",
"require": {
"php": ">=7.0"
"php": ">=7.0",
"guzzlehttp/guzzle": ">=6.0"
},
"require-dev": {
"friendsofphp/php-cs-fixer": ">=2.8",
Expand Down
10 changes: 10 additions & 0 deletions src/Exception/DeviceApiCallException.php
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
{
}
116 changes: 116 additions & 0 deletions src/Helper/DeviceAddressInformationHelper.php
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));
}
}
70 changes: 70 additions & 0 deletions src/Request/Device/DeviceAddressInformation.php
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;
}
}
14 changes: 10 additions & 4 deletions src/Request/System.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ class System
*/
public $device;

/**
* @var string|null
*/
public $apiAccessToken;

/**
* @var string|null
*/
Expand All @@ -36,10 +41,11 @@ public static function fromAmazonRequest(array $amazonRequest): self
{
$system = new self();

$system->application = isset($amazonRequest['application']) ? Application::fromAmazonRequest($amazonRequest['application']) : null;
$system->user = isset($amazonRequest['user']) ? User::fromAmazonRequest($amazonRequest['user']) : null;
$system->device = isset($amazonRequest['device']) ? Device::fromAmazonRequest($amazonRequest['device']) : null;
$system->apiEndpoint = isset($amazonRequest['apiEndpoint']) ? $amazonRequest['apiEndpoint'] : null;
$system->application = isset($amazonRequest['application']) ? Application::fromAmazonRequest($amazonRequest['application']) : null;
$system->user = isset($amazonRequest['user']) ? User::fromAmazonRequest($amazonRequest['user']) : null;
$system->device = isset($amazonRequest['device']) ? Device::fromAmazonRequest($amazonRequest['device']) : null;
$system->apiAccessToken = isset($amazonRequest['apiAccessToken']) ? $amazonRequest['apiAccessToken'] : null;
$system->apiEndpoint = isset($amazonRequest['apiEndpoint']) ? $amazonRequest['apiEndpoint'] : null;

return $system;
}
Expand Down
Loading

0 comments on commit 31e9c21

Please sign in to comment.