diff --git a/.gitignore b/.gitignore index 0e342a2..494f9c4 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,7 @@ /.idea/ # Composer -/composer.phar \ No newline at end of file +/composer.phar + +# Project files +/app_token \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..d0a0e9a --- /dev/null +++ b/README.md @@ -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); +``` \ No newline at end of file diff --git a/composer.json b/composer.json index 05a527c..3129ae8 100644 --- a/composer.json +++ b/composer.json @@ -16,7 +16,7 @@ } ], "require" : { - "alphayax/php_utils" : "*" + "alphayax/php_utils" : "dev-master" }, "autoload": { "psr-4": {"alphayax\\": "./"} diff --git a/exemple.php b/exemple.php new file mode 100644 index 0000000..ad6b33a --- /dev/null +++ b/exemple.php @@ -0,0 +1,17 @@ +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); diff --git a/freebox/api/v3/Model.php b/freebox/api/v3/Model.php index bd2fb24..19dc14a 100644 --- a/freebox/api/v3/Model.php +++ b/freebox/api/v3/Model.php @@ -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(){ + + } + } \ No newline at end of file diff --git a/freebox/api/v3/services/login/Authorize.php b/freebox/api/v3/services/login/Authorize.php index a4a8a2d..35db33a 100644 --- a/freebox/api/v3/services/login/Authorize.php +++ b/freebox/api/v3/services/login/Authorize.php @@ -45,13 +45,14 @@ class Authorize extends Service { if( ! $this->application->haveAppToken()){ $this->ask_authorization(); - while( $this->status == self::STATUS_PENDING){ + while( in_array( $this->status, [self::STATUS_UNKNOWN, self::STATUS_PENDING])){ $this->get_authorization_status(); if( $this->status == self::STATUS_GRANTED){ + $this->application->setAppToken( $this->app_token); $this->application->saveAppToken(); break; } - sleep( 10); + sleep( 5); } /// For verbose diff --git a/freebox/utils/Application.php b/freebox/utils/Application.php index c4f04d1..931d85e 100644 --- a/freebox/utils/Application.php +++ b/freebox/utils/Application.php @@ -116,7 +116,9 @@ class Application { * Write the app token in a file for later use */ 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'); + } } }