Update Rest architecture

Update composer dependencies
Update README
Update exemple
This commit is contained in:
alphayax 2016-06-03 21:19:23 +02:00
parent 7dfe3790cf
commit 6fb1b7cced
8 changed files with 81 additions and 33 deletions

View File

@ -1,12 +1,14 @@
# Freebox v6 PHP API v3
![license](https://img.shields.io/packagist/l/alphayax/freebox_api_php.svg)
![unstable](https://poser.pugx.org/alphayax/freebox_api_php/v/unstable)
![stable](https://poser.pugx.org/alphayax/freebox_api_php/v/stable)
![unstable](https://poser.pugx.org/alphayax/freebox_api_php/v/unstable)
![pakagist](https://img.shields.io/packagist/v/alphayax/freebox_api_php.svg)
![codacy](https://api.codacy.com/project/badge/Grade/f3569cf671f04b8ab6d699be3fd011e5)
![license](https://img.shields.io/packagist/l/alphayax/freebox_api_php.svg)
Implementation PHP de l'API de la freebox (dans sa version 3).

View File

@ -12,7 +12,7 @@
],
"require" : {
"php": ">=5.5.0",
"alphayax/php_utils" : "^0.1.1",
"alphayax/php_utils" : "^1.0.0",
"monolog/monolog": "^1.9.1"
},
"autoload": {

View File

@ -1,5 +1,5 @@
{
"rss": "https:\/\/www.nyaa.se\/?page=rss&user=175467",
"pattern": "\/One_Piece.*HD.*mp4\/",
"last_date": 1463909172
"last_date": 1464513148
}

View File

@ -5,7 +5,7 @@ use alphayax\utils\cli\IO;
require_once '../../../vendor/autoload.php';
/// Define our application
$App = new \alphayax\freebox\utils\Application( 'com.alphayax.freebox.example', 'Freebox PHP API Example', '0.0.2');
$App = new \alphayax\freebox\utils\Application( 'com.alphayax.freebox.dl_rss', 'Alphayax RSS Downloader', '1.0.0');
$App->authorize();
$App->openSession();

View File

@ -33,10 +33,10 @@ abstract class Service {
* @param string $service
* @param bool $isJson
* @param bool $returnAsArray
* @return \alphayax\freebox\utils\RestAuth
* @return \alphayax\freebox\utils\rest\RestAuth
*/
protected function getAuthService( $service, $isJson = true, $returnAsArray = true){
$rest = new \alphayax\freebox\utils\RestAuth( static::API_HOST . $service, $isJson, $returnAsArray);
$rest = new \alphayax\freebox\utils\rest\RestAuth( static::API_HOST . $service, $isJson, $returnAsArray);
$rest->setSessionToken( $this->application->getSessionToken());
return $rest;
}

View File

@ -78,6 +78,10 @@ class Authorize extends Service {
]);
$response = $rest->getCurlResponse();
if( ! $response['success']){
$this->application->getLogger()->addCritical( 'Freebox Error : '. $response['error_code'] .' - '. $response['msg']);
throw new \Exception( $response['msg'], $response['error_code']);
}
$this->application->getLogger()->addInfo( 'Authorization send to Freebox. Waiting for response...');

View File

@ -1,5 +1,5 @@
<?php
namespace alphayax\freebox\utils;
namespace alphayax\freebox\utils\rest;
use alphayax;
@ -8,10 +8,7 @@ use alphayax;
* @package alphayax\utils
* @author <alphayax@gmail.com>
*/
class RestAuth extends alphayax\utils\Rest {
/** @var string */
protected $session_token = '';
class Rest extends alphayax\utils\Rest {
/**
* @param null $curl_post_data
@ -19,7 +16,6 @@ class RestAuth extends alphayax\utils\Rest {
* @throws \Exception
*/
public function GET( $curl_post_data = null, $checkResponse = true){
$this->add_XFbxAppAuth_Header();
parent::GET( $curl_post_data);
if( $checkResponse){
$this->checkResponse();
@ -30,7 +26,6 @@ class RestAuth extends alphayax\utils\Rest {
* @param $curl_post_data
*/
public function POST( $curl_post_data = null){
$this->add_XFbxAppAuth_Header();
parent::POST( $curl_post_data);
$this->checkResponse();
}
@ -39,7 +34,6 @@ class RestAuth extends alphayax\utils\Rest {
* @param $curl_post_data
*/
public function PUT( $curl_post_data = null){
$this->add_XFbxAppAuth_Header();
parent::PUT( $curl_post_data);
$this->checkResponse();
}
@ -48,36 +42,20 @@ class RestAuth extends alphayax\utils\Rest {
* @param $curl_post_data
*/
public function DELETE( $curl_post_data = null){
$this->add_XFbxAppAuth_Header();
parent::DELETE( $curl_post_data);
$this->checkResponse();
}
/**
* Add the session token in the X-Fbx-App-Auth Header
*/
protected function add_XFbxAppAuth_Header(){
$this->http_headers[ 'X-Fbx-App-Auth'] = $this->session_token;
}
/**
* @throws \Exception
*/
protected function checkResponse(){
$response = $this->getCurlResponse();
if( false === $response['success']){
if( false === $this->getSuccess()){
$response = $this->getCurlResponse();
throw new \Exception( $response['msg'] . ' ('. $response['error_code'] . ')');
}
}
/**
* @param $session_token
*/
public function setSessionToken( $session_token){
$this->session_token = $session_token;
}
/**
* @param string $className
* @return array

View File

@ -0,0 +1,64 @@
<?php
namespace alphayax\freebox\utils\rest;
use alphayax;
/**
* Class Rest
* @package alphayax\utils
* @author <alphayax@gmail.com>
*/
class RestAuth extends Rest {
/** @var string */
protected $session_token = '';
/**
* @param null $curl_post_data
* @param bool $checkResponse
* @throws \Exception
*/
public function GET( $curl_post_data = null, $checkResponse = true){
$this->add_XFbxAppAuth_Header();
parent::GET( $curl_post_data, $checkResponse);
}
/**
* @param $curl_post_data
*/
public function POST( $curl_post_data = null){
$this->add_XFbxAppAuth_Header();
parent::POST( $curl_post_data);
}
/**
* @param $curl_post_data
*/
public function PUT( $curl_post_data = null){
$this->add_XFbxAppAuth_Header();
parent::PUT( $curl_post_data);
}
/**
* @param $curl_post_data
*/
public function DELETE( $curl_post_data = null){
$this->add_XFbxAppAuth_Header();
parent::DELETE( $curl_post_data);
}
/**
* Add the session token in the X-Fbx-App-Auth Header
*/
protected function add_XFbxAppAuth_Header(){
$this->addHeader( 'X-Fbx-App-Auth', $this->session_token);
}
/**
* @param $session_token
*/
public function setSessionToken( $session_token){
$this->session_token = $session_token;
}
}