Différences entre versions de « Comment utiliser l'API d’Ikoula »

De Ikoula Wiki
Jump to navigation Jump to search
 
(2 versions intermédiaires par le même utilisateur non affichées)
Ligne 46 : Ligne 46 :
  
 
<syntaxhighlight lang="php">
 
<syntaxhighlight lang="php">
<?php  
+
<?php
  
 
// #################################################
 
// #################################################
Ligne 55 : Ligne 55 :
 
class IkoulaAPI {
 
class IkoulaAPI {
  
  /**
+
    /**
    * Email of Ikoula account
+
    * Email of Ikoula account
    * @var string Email account
+
    * @var string Email account
    */
+
    */
 
     private static $email = "EMAIL_ACCOUNT_IKOULA";
 
     private static $email = "EMAIL_ACCOUNT_IKOULA";
  
  /**
+
    /**
    * Password of Ikoula account
+
    * Password of Ikoula account
    * @var string Password account
+
    * @var string Password account
    */
+
    */
 
     private static $password = "PASSWORD_ACCOUNT_IKOULA";
 
     private static $password = "PASSWORD_ACCOUNT_IKOULA";
  
  /**
+
    /**
    * Ikoula API URI
+
    * Ikoula API URI
    * @var string Password account
+
    * @var string Password account
    */  
+
    */  
 
     private static $urlApi = "https://api.ikoula.com/";
 
     private static $urlApi = "https://api.ikoula.com/";
  
  /** Public key path for encrypt data
+
    /** Public key path for encrypt data
    * @var string Path of public key
+
    * @var string Path of public key
    * @see https://api.ikoula.com/downloads/Ikoula.API.RSAKeyPub.pem
+
    * @see https://api.ikoula.com/downloads/Ikoula.API.RSAKeyPub.pem
    */  
+
    */  
 
     private static $publicKeyPath = "/path/to/ikoula/public/key/Ikoula.API.RSAKeyPub.pem";
 
     private static $publicKeyPath = "/path/to/ikoula/public/key/Ikoula.API.RSAKeyPub.pem";
+
   
 
     /** Fonction for request Ikoula API
 
     /** Fonction for request Ikoula API
 
     * @param string $webservice Webservice for data
 
     * @param string $webservice Webservice for data
Ligne 84 : Ligne 84 :
 
     * @param string $type HTTP Type (GET/POST)
 
     * @param string $type HTTP Type (GET/POST)
 
     * @param array $params Params to add for request
 
     * @param array $params Params to add for request
 +
* @param array|null $body
 +
* @param bool $jsonBody
 
     */
 
     */
    public static function requestApi($webservice, $format, $type, $params = [])
+
    public static function requestApi($webservice, $format, $method, $params = [], $body = null, $jsonBody = false)
    {
+
{
 
// Add connexion information
 
// Add connexion information
 
$params['login'] = self::$email;
 
$params['login'] = self::$email;
Ligne 92 : Ligne 94 :
 
$params['format'] = $format;
 
$params['format'] = $format;
  
// Generate signature
+
// Fix params to lowercase for generate signature correctly
$signature = self::createSignature($params);
+
$params = array_change_key_case($params);
 
 
// Add signature for call
 
$params['signature'] = $signature;
 
  
 
// Curl init
 
// Curl init
Ligne 102 : Ligne 101 :
  
 
if($ch)
 
if($ch)
{
+
  {
 
// Create API URI
 
// Create API URI
 
$url = self::$urlApi.$webservice;
 
$url = self::$urlApi.$webservice;
 +
$url .= (str_contains($url, '?') ? '&' : '?') . http_build_query($params);
 
 
 
// Add type request
 
// Add type request
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $type);
+
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
 
 
// If parameters, we encode his
 
if(is_array($params) && count($params) > 0)
 
$params_str = http_build_query($params);
 
  
 
// If we use post, fix params
 
// If we use post, fix params
if(strcasecmp("POST", $type) == 0)
+
if(in_array($method, ['POST', 'PUT', 'DELETE'], true))
 
{
 
{
// Fix POST data
+
if($body !== null)
curl_setopt($ch,CURLOPT_POST, true);
+
{
curl_setopt($ch,CURLOPT_POSTFIELDS, $params_str);
+
if(!$jsonBody)
 +
{
 +
curl_setopt($ch, CURLOPT_POST, true);
 +
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($body));
 +
}
 +
else
 +
{
 +
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-type: application/json']);
 +
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($body));
 +
}
 +
}
 
}
 
}
else
+
 
$url .= (strpos($url,'?') === false ? '?' : '&').$params_str;
 
 
 
// Create curl info
 
// Create curl info
 
curl_setopt($ch, CURLOPT_URL, $url);
 
curl_setopt($ch, CURLOPT_URL, $url);
Ligne 138 : Ligne 142 :
 
 
 
// Return response
 
// Return response
return($data);
+
return $data;
}
+
  }
  
return null;
+
  return null;
    }
+
  }
 
+
   
     /** Create signature with params
+
     /** Fonction for crypt Ikoula password
     * @param array $params Params to add for request
+
     * @param string $password Ikoula account password
    * @return string Signature encoded
+
    * @return mixed Ikoula password encrypted, null if error
    */
+
    */
     private static function createSignature($params = [])
+
     private static function opensslEncryptPublic($password)
 
     {
 
     {
        // Signature to send
+
         // Verify if key file exist
        $signature = null;
+
         if(file_exists(self::$publicKeyPath))
 
 
         // Verify parameters
 
         if (count($params) > 0)
 
 
         {
 
         {
            // Sort params
+
             // Verify if password is not empty
            ksort($params);
+
             if(!empty($password))
           
 
            // Encode params
 
            $query = http_build_query($params);
 
 
 
            // Encode "plus "+"
 
            $query = str_replace("+", "%20", $query);
 
 
 
            // Transform in lowercase
 
            $query = strtolower($query);
 
 
 
            $public_key = "";
 
 
 
             // Verify if key file is present
 
             if (file_exists(self::$publicKeyPath))
 
 
             {
 
             {
// Get public key
+
                // Get file content
                 $public_key = trim(
+
                 $publicKey = openssl_pkey_get_public('file://'.realpath(self::$publicKeyPath));
                        str_replace(
+
   
                                array("\n", '-----BEGIN PUBLIC KEY-----','-----END PUBLIC KEY-----'),
+
                // If we get file content without error
                                array('', '', ''),
+
                if ($publicKey !== FALSE)    
                                file_get_contents(self::$publicKeyPath)
+
                {
                            )
+
                    // Encrypt password
                         );
+
                    if(openssl_public_encrypt($password, $crypted, $publicKey) === TRUE)
 +
                        return $crypted;
 +
                    else
 +
                         return NULL;
 +
                }
 +
                else
 +
                    return NULL;
 
             }
 
             }
 
+
             else
  // SHA1 hash
+
                return NULL;
             $hash = hash_hmac("SHA1", $query, $public_key, true);
 
 
 
    // base64 encode
 
            $signature = base64_encode($hash);
 
 
         }
 
         }
 
+
        else
    return $signature;
+
            return NULL;
}
+
    }
 
/** Fonction for crypt Ikoula password
 
* @param string $password Ikoula account password
 
* @return mixed Ikoula password encrypted, null if error
 
*/
 
private static function opensslEncryptPublic($password)
 
{
 
// Verify if key file exist
 
if(file_exists(self::$publicKeyPath))
 
{
 
// Verify if password is not empty
 
if(!empty($password))
 
{
 
// Get file content
 
$publicKey = openssl_pkey_get_public('file://'.realpath(self::$publicKeyPath));
 
 
// If we get file content without error
 
if ($publicKey !== FALSE)     
 
{
 
// Encrypt password
 
if(openssl_public_encrypt($password, $crypted, $publicKey) === TRUE)
 
return $crypted;
 
else
 
return NULL;
 
}
 
else
 
return NULL;
 
}
 
else
 
return NULL;
 
}
 
else
 
return NULL;
 
}
 
 
}
 
}
 
</syntaxhighlight>
 
</syntaxhighlight>
Ligne 261 : Ligne 216 :
  
 
En cas de demande de fonctionnalité, contactez le support technique.
 
En cas de demande de fonctionnalité, contactez le support technique.
 +
 +
[[Category:API]]

Version actuelle datée du 17 avril 2024 à 15:34

en:How to use the Ikoula API es:Cómo utilizar la API de Ikoula nl:Hoe de Ikoula API te gebruiken? zh:如何使用Ikoula的API it:Come utilizzare l'API Ikoula

Introduction

Ikoula dispose d'une API permettant de réaliser des actions sur les produits associés à votre compte client. Voici l'URL de l'API: https://api.ikoula.com

La documentation est disponible directement pour chaque produit.

Explications

Pour des raisons évidentes de sécurité, l'API Ikoula exige une authentification. Celle-ci est basée sur un identifiant, un mot de passe et une signature :

  • L'identifiant est l'adresse e-mail utilisée pour la connexion à votre compte Ikoula ou à l'Extranet. Le nom du paramètre pour le transmettre est toujours login ;
  • Le mot de passe quant à lui doit être chiffré via une fonction spécifique utilisant une clé publique fournie par Ikoula (paramètre crypted_password) et base64_encode ;
  • La signature est générée en fonction des paramètres fournis lors de l'appel à l'API.
  • Ces paramètres doivent toujours être passés en GET à l'API !


ATTENTION :

Pour vos tests de l'API, vous pouvez, par exemple, utiliser un utilisateur temporaire dédié à ces tests. L'utilisation du chiffrement de mot de passe avec la clé publique Ikoula est indispensable dans tout contexte de production ou non-court terme.
Si les appels API sont voués à être utilisés via un script ou un programme, nous vous recommandons la création d'un utilisateur dédié à cet effet plutôt que d'utiliser votre utilisateur extranet classique.
Deux possibilités s'offrent à vous :

Attention à ne pas oublier de lui mettre les droits sur les prestations souhaitées.

Clé de chiffrement

La clé publique de chiffrement du mot de passe est disponible à l'adresse suivante: https://api.ikoula.com/downloads/Ikoula.API.RSAKeyPub.pem

Wrapper PHP

<?php

// #################################################
// ####    ..:: Ikoula Hosting Services ::..     ###
// ####	   Wrapper for https://api.ikoula.com	 ###
// #################################################

class IkoulaAPI {

    /**
     * Email of Ikoula account
     * @var string Email account
     */
    private static $email = "EMAIL_ACCOUNT_IKOULA";

    /**
     * Password of Ikoula account
     * @var string Password account
     */
    private static $password = "PASSWORD_ACCOUNT_IKOULA";

    /**
     * Ikoula API URI
     * @var string Password account
     */ 
    private static $urlApi = "https://api.ikoula.com/";

    /** Public key path for encrypt data
     * @var string Path of public key
     * @see https://api.ikoula.com/downloads/Ikoula.API.RSAKeyPub.pem
     */ 
    private static $publicKeyPath = "/path/to/ikoula/public/key/Ikoula.API.RSAKeyPub.pem";
    
    /** Fonction for request Ikoula API
     * @param string $webservice Webservice for data
     * @param string $format JSON or XML
     * @param string $type HTTP Type (GET/POST)
     * @param array $params Params to add for request
	 * @param array|null $body
	 * @param bool $jsonBody
     */
    public static function requestApi($webservice, $format, $method, $params = [], $body = null, $jsonBody = false)
	{
		// Add connexion information
		$params['login'] = self::$email;
		$params['crypted_password'] = base64_encode(self::opensslEncryptPublic(self::$password));
		$params['format'] = $format;

		// Fix params to lowercase for generate signature correctly
		$params = array_change_key_case($params);

		// Curl init
		$ch = curl_init();

		if($ch)
	   	{
			// Create API URI
			$url = self::$urlApi.$webservice;
			$url .= (str_contains($url, '?') ? '&' : '?') . http_build_query($params);
	
			// Add type request
			curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);

			// If we use post, fix params
			if(in_array($method, ['POST', 'PUT', 'DELETE'], true))
			{
				if($body !== null)
				{
					if(!$jsonBody)
					{
						curl_setopt($ch, CURLOPT_POST, true);
						curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($body));
					}
					else
					{
						curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-type: application/json']);
						curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($body));
					}
				}
			}
   
			// Create curl info
			curl_setopt($ch, CURLOPT_URL, $url);
			curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
			curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
			curl_setopt($ch, CURLOPT_HEADER, 1);
			curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
			curl_setopt($ch, CURLOPT_HEADER, false);
	
			// Exec request
			$data = curl_exec($ch);
	
			// Close curl object
			curl_close($ch);
	
			// Return response
			return $data;
	   }

	   return null;
   }
    
    /** Fonction for crypt Ikoula password
     * @param string $password Ikoula account password
    * @return mixed Ikoula password encrypted, null if error
    */
    private static function opensslEncryptPublic($password)
    {
        // Verify if key file exist
        if(file_exists(self::$publicKeyPath))
        {
            // Verify if password is not empty
            if(!empty($password))
            {
                // Get file content
                $publicKey = openssl_pkey_get_public('file://'.realpath(self::$publicKeyPath));
    
                // If we get file content without error
                if ($publicKey !== FALSE)      
                {
                    // Encrypt password
                    if(openssl_public_encrypt($password, $crypted, $publicKey) === TRUE)
                        return $crypted;
                    else
                        return NULL;
                }
                else
                    return NULL;
            }
            else
                return NULL;
        }
        else
            return NULL;
    }
}

Exemples d'utilisation

// Fix JSON header
header("Content-type: application/json");

// Get Exch schema for prestation
echo IkoulaAPI::requestApi("wsexch/schema-subscr", "json", "GET", ['subscr_id' => 999999999]);

// Get Flex VM for Ikoula Account
echo IkoulaAPI::requestApi("wsflex/list", "json", "GET", []);

// Get Flex VM for Ikoula Account
echo IkoulaAPI::requestApi("wsflex/list", "json", "GET", []);

// Get Platform list
echo IkoulaAPI::requestApi("wsplatform/list", "json", "GET", []);

// Get Platform dossiers
echo IkoulaAPI::requestApi("wsplatform/dossiers", "json", "GET", ['platform_id' => 999999999]);

// Get Billing conso for CloudStack prestation
echo IkoulaAPI::requestApi("wscs/conso-for-billing", "json", "GET", ['subscr_id' => '999999999', 'billing_id' => '1']);

// Reboot server
echo IkoulaAPI::requestApi("wsds/reboot-apc-request", "json", "GET", ['server_ip' => 'XXX.XXX.XXX.XXX', 'tempo' => '3']);

Ajout de fonctionnalités

En cas de demande de fonctionnalité, contactez le support technique.