Add exemple files
Fix first authorisation demand Add README.md
This commit is contained in:
parent
f23cff48b1
commit
db5b1ee687
|
@ -4,3 +4,6 @@
|
||||||
|
|
||||||
# Composer
|
# Composer
|
||||||
/composer.phar
|
/composer.phar
|
||||||
|
|
||||||
|
# Project files
|
||||||
|
/app_token
|
|
@ -0,0 +1,38 @@
|
||||||
|
|
||||||
|
# Freebox v6 PHP API v3
|
||||||
|
|
||||||
|
Implementation PHP de l'API de la freebox.
|
||||||
|
|
||||||
|
## Prérequis
|
||||||
|
|
||||||
|
Ce projet est basé sur composer. Pensez a installer les dependences :)
|
||||||
|
|
||||||
|
## Utilisation
|
||||||
|
|
||||||
|
### Application
|
||||||
|
|
||||||
|
La premiere étape est de créer une application.
|
||||||
|
La seconde est de demander l'autorisation de connexion a la freebox (cf: cadrant led du Freebox Server)
|
||||||
|
La derniere est de récuperer une session pour utiliser les divers services de l'API
|
||||||
|
|
||||||
|
```php
|
||||||
|
$App = new \alphayax\freebox\utils\Application( 'com.alphayax.freebox.example', 'Freebox PHP API Exemple', '0.0.1');
|
||||||
|
$App->authorize();
|
||||||
|
$App->openSession();
|
||||||
|
```
|
||||||
|
|
||||||
|
### Services
|
||||||
|
Les appels aux services de l'API se font de la maniere suivante :
|
||||||
|
Voiuci un exemple d'utilisation de l'API System.
|
||||||
|
1. Nous créons un nouveau service "System"
|
||||||
|
2. Nous demandons de récuperer la configuration actuelle
|
||||||
|
3. Nous utilisons le modele retourné pour acceder a la donnée `uptime`
|
||||||
|
|
||||||
|
```php
|
||||||
|
$System = new \alphayax\freebox\api\v3\services\config\System( $App);
|
||||||
|
|
||||||
|
/** @var \alphayax\freebox\api\v3\models\SystemConfig $SystemConfig */
|
||||||
|
$SystemConfig = $System->getConfiguration();
|
||||||
|
|
||||||
|
\alphayax\utils\cli\IO::stdout( 'Uptime : '. $SystemConfig->uptime);
|
||||||
|
```
|
|
@ -16,7 +16,7 @@
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"require" : {
|
"require" : {
|
||||||
"alphayax/php_utils" : "*"
|
"alphayax/php_utils" : "dev-master"
|
||||||
},
|
},
|
||||||
"autoload": {
|
"autoload": {
|
||||||
"psr-4": {"alphayax\\": "./"}
|
"psr-4": {"alphayax\\": "./"}
|
||||||
|
|
|
@ -0,0 +1,17 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
/// Require Composer AutoLoader
|
||||||
|
require_once 'vendor/autoload.php';
|
||||||
|
|
||||||
|
/// Define our application
|
||||||
|
$App = new \alphayax\freebox\utils\Application( 'com.alphayax.freebox.example', 'Freebox PHP API Exemple', '0.0.1');
|
||||||
|
$App->authorize();
|
||||||
|
$App->openSession();
|
||||||
|
|
||||||
|
|
||||||
|
$System = new \alphayax\freebox\api\v3\services\config\System( $App);
|
||||||
|
|
||||||
|
/** @var \alphayax\freebox\api\v3\models\SystemConfig $SystemConfig */
|
||||||
|
$SystemConfig = $System->getConfiguration();
|
||||||
|
|
||||||
|
\alphayax\utils\cli\IO::stdout( 'Uptime : '. $SystemConfig->uptime);
|
|
@ -19,4 +19,31 @@ abstract class Model {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Magic getter
|
||||||
|
* @param $name
|
||||||
|
* @return null
|
||||||
|
*/
|
||||||
|
function __get( $name){
|
||||||
|
if( property_exists( static::class, $name)){
|
||||||
|
return $this->$name;
|
||||||
|
}
|
||||||
|
return null; // TODO : maybe throw exception ?
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Magic setter
|
||||||
|
* @param $name
|
||||||
|
* @param $value
|
||||||
|
*/
|
||||||
|
public function __set( $name, $value){
|
||||||
|
if( property_exists( static::class, $name)){
|
||||||
|
$this->$name = $value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function toArray(){
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
|
@ -45,13 +45,14 @@ class Authorize extends Service {
|
||||||
|
|
||||||
if( ! $this->application->haveAppToken()){
|
if( ! $this->application->haveAppToken()){
|
||||||
$this->ask_authorization();
|
$this->ask_authorization();
|
||||||
while( $this->status == self::STATUS_PENDING){
|
while( in_array( $this->status, [self::STATUS_UNKNOWN, self::STATUS_PENDING])){
|
||||||
$this->get_authorization_status();
|
$this->get_authorization_status();
|
||||||
if( $this->status == self::STATUS_GRANTED){
|
if( $this->status == self::STATUS_GRANTED){
|
||||||
|
$this->application->setAppToken( $this->app_token);
|
||||||
$this->application->saveAppToken();
|
$this->application->saveAppToken();
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
sleep( 10);
|
sleep( 5);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// For verbose
|
/// For verbose
|
||||||
|
|
|
@ -116,7 +116,9 @@ class Application {
|
||||||
* Write the app token in a file for later use
|
* Write the app token in a file for later use
|
||||||
*/
|
*/
|
||||||
public function saveAppToken(){
|
public function saveAppToken(){
|
||||||
file_put_contents( 'app_token', $this->app_token);
|
if( ! file_put_contents( 'app_token', $this->app_token)){
|
||||||
|
throw new \Exception('Unable to save app token in file');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue