<?php

/*************************************************************************************************/
/*****************************  DEFINITION D'UNE CLASSE POUR L'API  ******************************/
/*************************************************************************************************/

class DIAZ_API
{
    protected 
$server '';
    protected 
$endPoint '';

    private 
$authTokens null;

    public function 
__construct($endPoint$server)
    {
      
$this->server $server;
      
$this->endPoint $endPoint;
    }

    protected function 
diaz_exec($func$method 'GET'$data null)
    {
        
$url $this->endPoint $func;
        if (
$method == 'GET' && is_array($data) && count($data)) {
            
$url .= '?' http_build_query($data);
        }
        
$ch curl_init($url);
        
curl_setopt($chCURLOPT_CUSTOMREQUEST$method);
        if (
$method == 'POST') {
            
curl_setopt($chCURLOPT_POSTFIELDSjson_encode($data));
        }
        
curl_setopt($chCURLOPT_RETURNTRANSFERtrue);
        
$headers = array(
            
'Content-Type: application/json'
        
);
        if (isset(
$this->authTokens->access_token)) {
            
$headers[] = 'Authorization: Bearer ' $this->authTokens->access_token;
        }
        
curl_setopt($chCURLOPT_HTTPHEADER$headers);
        
$response curl_exec($ch);
        if (
curl_errno($ch)) {
            
curl_close($ch);
            throw new 
Exception('Erreur cURL : ' curl_error($ch));
        } else {
            
$httpCode curl_getinfo($chCURLINFO_HTTP_CODE);
        
curl_close($ch);
        if (
$httpCode>=200 && $httpCode<=299) {
              
$decoded_response json_decode($responsefalse);
        } else {
          
$decoded_response null;
        }
            if (
$decoded_response !== null) {
                return 
$decoded_response;
            } else {
                throw new 
Exception('Erreur : '.$httpCode.' (' $this->endPoint $url ")\n".var_export($responsetrue));
            }
        }
    }

    public function 
login($username$password)
    {
        
$loginData = array('username' => $username,
                           
'password' => $password,
                           
'server' => $this->server
                     
);
        try {
            
$this->authTokens $this->diaz_exec('auth/login''POST'$loginData);
        } catch (
Exception $e) {
            die(
$e->getMessage());
        }
    }

    public function 
logout()
    {
        
// TODO: Appeler logout sur l'api.
        
$this->authTokens null;
    }
 
    public function 
getListeFichier($fichier$params null) {
        try {
            return 
$this->diaz_exec($fichier'GET'$params);
        } catch (
Exception $e) {
            die(
$e->getMessage());
        }
    }

    public function 
getSearchFichier($fichier$params) {
        try {
            return 
$this->diaz_exec($fichier.'/search''POST'$params);
        } catch (
Exception $e) {
            die(
$e->getMessage());
        }
    }
}


/*************************************************************************************************/
/*************************************************************************************************/
/*************************************************************************************************/
/*************************************************************************************************/