« Général:FAQ API MAJ » : différence entre les versions

De WikiFr_dbSQWare
Aller à la navigation Aller à la recherche
Page créée avec « ==Généralités== ===Limites de cette section=== Cette section n’a pas la prétention de traiter tous les cas possibles d'utilisation de l'API de mise à jour du réf... »
 
 
(7 versions intermédiaires par 2 utilisateurs non affichées)
Ligne 4 : Ligne 4 :
Cette section n’a pas la prétention de traiter tous les cas possibles d'utilisation de l'API de mise à jour du référentiel.<br>
Cette section n’a pas la prétention de traiter tous les cas possibles d'utilisation de l'API de mise à jour du référentiel.<br>
Vous devez être à l'aise avec le concept des API et la gestion de token, si ce n'est pas le cas, faites appel au support.<br>
Vous devez être à l'aise avec le concept des API et la gestion de token, si ce n'est pas le cas, faites appel au support.<br>
Attention, vous allez mettre à jour la table de référentiel, insert et/ou update, vous devez savoir ce que vous faites !<br>
Attention, vous allez mettre à jour la table du référentiel général, insert et/ou update, vous devez savoir ce que vous faites !<br>


===Paramétrage préalable===
===Paramétrage préalable===
Ligne 15 : Ligne 15 :


===Python===
===Python===
import requests
<syntaxhighlight lang="python" line>
import json
import requests
import json
v_bearer_token = ""
 
bearerToken = ""
"""
 
f_api_request does API request
"""
Does API request.
:param v_method : request method
 
:param v_url     : website url
:param method : Request method
:param t_headers : JSON object headers  
:param url     : Website url
:param t_data   : JSON object data
:param headers : JSON object headers  
:return         : API request return
:param data   : JSON object data
"""
:return       : API request return
def f_api_request(v_method, v_url, t_headers={}, t_data={}):
"""
    v_response = requests.request(v_method, v_url, headers=t_headers, data=t_data) # API request
def apiRequest(method, url, headers = {}, data = {}):
    return v_response  
    response = requests.request(method, url, headers=headers, data=data)
    return response  
"""
 
f_get_bearer_token recovers bearer token
"""
Get bearer token.
:param v_username : account username
 
:param v_password : account password
:param username : Account username
:return           : bearer token
:param password : Account password
"""
:return         : Bearer token
def f_get_bearer_token(v_username, v_password):
"""
    v_method = "POST"
def getBearerToken(username, password):
    v_url     = "http://my_sqwareweb_url/lib/apiUpdate_login.php"
    # Init request
    t_headers = {'Content-Type': 'application/json'}
    method = 'POST'
    t_data   = json.dumps({"username":v_username,"password":v_password}) # Crendentials for connection
    url     = "http://admin-webdba.dbsqware.com/lib/apiUpdate_login.php"
    headers = {'Content-Type': 'application/json'}
    v_response = f_api_request(v_method, v_url, t_headers=t_headers, t_data=t_data) # API request
    data   = json.dumps({'username': username, 'password': password})
    v_json_response = json.loads(v_response.text)
 
    if "error_message" not in v_json_response:
    # API request
        global v_bearer_token
    response = apiRequest(method, url, headers=headers, data=data)
        v_bearer_token = v_json_response['results'][0]['token'] # Get token response
    if response == '':
        print("Bearer token : "+v_bearer_token)
        print('Failure of the API request !')
        return 1
        return 0
    else:
 
        v_error_message = v_json_response['error_message']
    response = json.loads(response.text)
        print("Error message ["+v_error_message+"]")
    if 'error_message' in response:
        return 0
        print(f"Error message [{response['error_message']}]")
        return 0
"""
 
f_get_instance recovers instance object
    # Get token response
    global bearerToken
:param v_dbalias      : dbalias name
    bearerToken = response['results']['token']
:return               : instance object
    print(f"Bearer token : {bearerToken}")
"""
    return 1
def f_get_instance(v_dbalias):
 
    v_method = "GET"
"""
    v_url     = "http://my_sqwareweb_url/lib/apiUpdate.php?dbalias="+v_dbalias
Get instance.
    t_headers = {'Authorization':'Bearer '+v_bearer_token, 'Content-Type': 'application/json'}
 
:param dbalias : Dbalias name
    v_response = f_api_request(v_method, v_url, t_headers=t_headers) # API request
:return       : Instance
    v_json_response = json.loads(v_response.text)
"""
    if "error_message" not in v_json_response:
def getInstance(dbalias):
        v_instance = v_json_response['results'][0] # Get instance
    # Init request
        print(v_instance)
    method = 'GET'
        return 1
    url     = f"http://admin-webdba.dbsqware.com/lib/apiUpdate.php?dbalias={dbalias}"
    else:
    headers = {
        v_error_message = v_json_response['error_message']
        'Authorization': f"Bearer {bearerToken}",
        print("Error message ["+v_error_message+"]")
        'Content-Type': 'application/json'
        return 0
    }
 
"""
    # API request
f_get_instances recovers instances objects
    response = apiRequest(method, url, headers=headers)
    if response == '':
:return               : instances objects
        print('Failure of the API request !')
"""
        return 0
def f_get_instances():
 
    v_method = "GET"
    response = json.loads(response.text)
    v_url     = "http://my_sqwareweb_url/lib/apiUpdate.php"
    if 'error_message' in response:
    t_headers = {'Authorization':'Bearer '+v_bearer_token, 'Content-Type': 'application/json'}
        print(f"Error message [{response['error_message']}]")
        return 0
    v_response = f_api_request(v_method, v_url, t_headers=t_headers) # API request
 
    v_json_response = json.loads(v_response.text)
    # Get instance
    if "error_message" not in v_json_response:
    instance = response['results'][0]
        v_instances = v_json_response['results'] # Get instances
    print(instance)
        print(v_instances)
    return 1
        return 1
 
    else:
"""
        v_error_message = v_json_response['error_message']
Get instances.
        print("Error message ["+v_error_message+"]")
 
        return 0
:return : Instances
"""
"""
def getInstances():
f_post_instance create new instance object
    # Init request
    method = 'GET'
:param t_data        : JSON object data
    url     = "http://admin-webdba.dbsqware.com/lib/apiUpdate.php"
:return               : request response
    headers = {
"""
        'Authorization': f"Bearer {bearerToken}",
def f_post_instance(t_data):
        'Content-Type': 'application/json'
    v_method  = "POST"
    }
    v_url     = "http://my_sqwareweb_url/lib/apiUpdate.php"
 
    t_headers = {'Authorization':'Bearer '+v_bearer_token, 'Content-Type': 'application/json'}
    # API request
    t_data   = json.dumps(t_data)
    response = apiRequest(method, url, headers=headers)
    if response == '':
    v_response = f_api_request(v_method, v_url, t_headers=t_headers, t_data=t_data) # API request
        print('Failure of the API request !')
    v_json_response = json.loads(v_response.text)
        return 0
    if "error_message" not in v_json_response:
 
        print("Insert with success !")
    response = json.loads(response.text)
        return 1
    if 'error_message' in response:
    else:
        print(f"Error message [{response['error_message']}]")
        v_error_message = v_json_response['error_message']
        return 0
        print("Error message ["+v_error_message+"]")
 
        return 0
    # Get instances
    instances = response['results']
"""
    print(instances)
f_put_instance update instance object
    return 1
 
:param v_dbalias      : dbalias name
"""
:param t_data        : JSON object data
Create new instance.
:return               : request response
 
"""
:param data : JSON object data
def f_put_instance(v_dbalias, t_data):
:return    : Request response
    v_method = "PUT"
"""
    v_url     = "http://my_sqwareweb_url/lib/apiUpdate.php?dbalias="+v_dbalias
def postInstance(data):
    t_headers = {'Authorization':'Bearer '+v_bearer_token, 'Content-Type': 'application/json'}
    # Init request
    t_data   = json.dumps(t_data)
    method  = 'POST'
    url     = "http://admin-webdba.dbsqware.com/lib/apiUpdate.php"
    v_response = f_api_request(v_method, v_url, t_headers=t_headers, t_data=t_data) # API request
    headers = {
    v_json_response = json.loads(v_response.text)
        'Authorization': f"Bearer {bearerToken}",
    if "error_message" not in v_json_response:
        'Content-Type': 'application/json'
        print("Update with success !")
    }
        return 1
    data   = json.dumps(data)
    else:
 
        v_error_message = v_json_response['error_message']
    # API request
        print("Error message ["+v_error_message+"]")
    response = apiRequest(method, url, headers=headers, data=data)
        return 0
    if response == '':
        print('Failure of the API request !')
def main():
        return 0
    # Variables
 
    v_username = "toto"
    response = json.loads(response.text)
    v_password = "toto"
    if 'error_message' in response:
    v_dbalias = "toto"
        print(f"Error message [{response['error_message']}]")
        return 0
    # Get bearer token
 
    f_get_bearer_token(v_username, v_password)
    print('Insert with success !')
    return 1
    # Post new instance
 
    t_data = {
"""
                "dbalias"      :v_dbalias,
Update instance.
                "rdbmsname"    :"cassandra",
 
                "virt_host_name":"AA-TEST-API",
:param dbalias : Dbalias name
                "host_name"    :"NPW",
:param data    : JSON object data
                "username"      :"system",
:return       : Request response
                "port"          :"1430",
"""
                "comments"      :"desc IPS_A",
def putInstance(dbalias, data):
                "contact"      :"OAY .L",
    # Init request
                "status"        :"ON",
    method = 'PUT'
                "client"        :"CUST2",
    url     = f"http://admin-webdba.dbsqware.com/lib/apiUpdate.php?dbalias={dbalias}"
                "env"          :"PRD3",
    headers = {
                "globalhost"    :"A",
        'Authorization': f"Bearer {bearerToken}",
                "custom1"      :"B",
        'Content-Type': 'application/json'
                "custom2"      :"C",
    }
                "dbaname"      :v_username,
    data   = json.dumps(data)
                "comments_upd"  :"insert new instance by API"
 
            }
    # API request
    response = apiRequest(method, url, headers=headers, data=data)
    f_post_instance(t_data)
    if response == '':
        print('Failure of the API request !')
    # Get new instance
        return 0
    f_get_instance(v_dbalias)
 
    response = json.loads(response.text)
    # Put new instance
    if 'error_message' in response:
    t_data = {
        print(f"Error message [{response['error_message']}]")
                "rdbmsname"    :"mysql",
        return 0
                "virt_host_name":"AA-TEST-API",
 
                "host_name"    :"NPW",
    print("Update with success !")
                "username"      :"system",
    return 1
                "port"          :"1430",
 
                "comments"      :"desc IPS_A",
def main():
                "contact"      :"OAY .L",
    # Variables
                "status"        :"OFF",
    username = "toto"
                "client"        :"CUST2",
    password = "toto"
                "env"          :"PRD3",
    dbalias = "toto"
                "globalhost"    :"A",
 
                "custom1"      :"B",
    # Get bearer token
                "custom2"      :"C",
    getBearerToken(username, password)
                "comments_upd"  : "update instance status by API"
 
            }
    # Post new instance
    data = {
    f_put_instance(v_dbalias, t_data)
        "dbalias"      : dbalias,
        "rdbmsname"    : "cassandra",
    # Get all instances
        "virt_host_name": "AA-TEST-API",
    f_get_instances()
        "host_name"    : "NPW",
        "username"      : "system",
if __name__ == '__main__':
        "port"          : "1430",
    main()
        "comments"      : "desc IPS_A",
        "contact"      : "OAY .L",
        "status"        : "ON",
        "client"        : "CUST2",
        "env"          : "PRD3",
        "globalhost"    : "A",
        "custom1"      : "B",
        "custom2"      : "C",
        "comments_upd"  : "insert new instance by API"
    }
 
    postInstance(data)
 
    # Get new instance
    getInstance(dbalias)
 
    # Put new instance
    data = {
        "rdbmsname"    : "mysql",
        "virt_host_name": "AA-TEST-API",
        "host_name"    : "NPW",
        "username"      : "system",
        "port"          : "1430",
        "comments"      : "desc IPS_A",
        "contact"      : "OAY .L",
        "status"        : "OFF",
        "client"        : "CUST2",
        "env"          : "PRD3",
        "globalhost"    : "A",
        "custom1"      : "B",
        "custom2"      : "C",
        "comments_upd"  : "update instance status by API"
    }
 
    putInstance(dbalias, data)
 
    # Get all instances
    getInstances()
 
if __name__ == '__main__':
    main()
</syntaxhighlight>


===PHP===
===PHP===
<?php
<syntaxhighlight lang="php" line>
<?php
$v_bearer_token = "";
 
$bearerToken = "";
/**
 
  * f_api_request does API request
/**
  *  
* Does API request.
  * @param String $v_method : request method
*  
  * @param String $v_url   : website url
* @param string  $method : Request method
  * @param Array $t_headers : JSON object headers  
* @param string  $url   : Website url
  * @param Array $t_data    : JSON object data
* @param array  $headers : JSON object headers  
  * @return $v_response    : API request return
* @param string  $data  : JSON object data
  */
* @return string
function f_api_request($v_method, $v_url, $t_headers=[], $t_data='{}'){
*/
    $v_curl = curl_init();
function apiRequest(string $method, string $url, array $headers = [], $data = '{}'): string
{
    curl_setopt_array($v_curl, array(
    $curl = curl_init();
        CURLOPT_URL            => $v_url,
 
        CURLOPT_RETURNTRANSFER => true,
    curl_setopt_array($curl, [
        CURLOPT_ENCODING      => '',
        CURLOPT_URL            => $url,
        CURLOPT_MAXREDIRS      => 10,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_TIMEOUT        => 0,
        CURLOPT_ENCODING      => '',
        CURLOPT_FOLLOWLOCATION => true,
        CURLOPT_MAXREDIRS      => 10,
        CURLOPT_HTTP_VERSION  => CURL_HTTP_VERSION_1_1,
        CURLOPT_TIMEOUT        => 0,
        CURLOPT_CUSTOMREQUEST  => $v_method,
        CURLOPT_FOLLOWLOCATION => true,
        CURLOPT_POSTFIELDS    => $t_data,
        CURLOPT_HTTP_VERSION  => CURL_HTTP_VERSION_1_1,
        CURLOPT_HTTPHEADER    => $t_headers,
        CURLOPT_SSL_VERIFYHOST => 0,
    ));
        CURLOPT_SSL_VERIFYPEER => 0,
        CURLOPT_CUSTOMREQUEST  => $method,
    $v_response = curl_exec($v_curl);
        CURLOPT_POSTFIELDS    => $data,
    return $v_response;
        CURLOPT_HTTPHEADER    => $headers,
}
    ]);
 
/**
    $response = curl_exec($curl);
  * f_get_bearer_token recovers bearer token
    curl_close($curl);
  *  
 
  * @param String $v_username : account username
    return $response;
  * @param String $v_password : account password
}
  * @return $v_bearer_token  : bearer token
 
  */
/**
function f_get_bearer_token($v_username, $v_password){
* Get bearer token.
    $v_method  = "POST";
*  
    $v_url     = "http://my_sqwareweb_url/lib/apiUpdate_login.php";
* @param string  $username : Account username
    $t_headers = array('Content-Type: application/json');
* @param string  $password : Account password
    $t_data   = json_encode(array("username" => $v_username, "password" => $v_password), JSON_FORCE_OBJECT); // Crendentials for connection
* @return bool
*/
    $v_response = f_api_request($v_method, $v_url, $t_headers=$t_headers, $t_data=$t_data); // API request
function getBearerToken(string $username, string $password): bool
    $v_json_response = json_decode($v_response, true);
{
    if(!array_key_exists("error_message", $v_json_response)){
    // Init request
        global $v_bearer_token;
    $method  = 'POST';
        $v_bearer_token = $v_json_response['results'][0]['token']; // Get token response
    $url     = "http://admin-webdba.dbsqware.com/lib/apiUpdate_login.php";
        echo("Bearer token : $v_bearer_token");
    $headers = ['Content-Type: application/json'];
        return 0;
    $data   = json_encode(['username' => $username, 'password' => $password], JSON_FORCE_OBJECT);
    }
 
    else{
    // API request
        $v_error_message = $v_json_response['error_message'];
    $response = apiRequest($method, $url, $headers=$headers, $data=$data);
        echo("Error message [$v_error_message]");
    if (empty($response)) {
        return 1;
        echo('Failure of the API request !');
    }
        return 0;
}
    }
 
/**
    $response = json_decode($response, true);
  * f_get_instance recovers instance object
    if (array_key_exists('error_message', $response)) {
  *
        echo("Error message [{$response['error_message']}]");
  * @param String $v_dbalias      : dbalias name
        return 0;
  * @return $v_instance          : instance object
    }
  */
 
  function f_get_instance($v_dbalias){
    // Get token response
    global $v_bearer_token;
    global $bearerToken;
    $v_method = "GET";
    $bearerToken = $response['results']['token'];
    $v_url    = "http://my_sqwareweb_url/lib/apiUpdate.php?dbalias=".$v_dbalias;
    echo("Bearer token : {$bearerToken}");
    $t_headers = array('Authorization: Bearer '.$v_bearer_token.'', 'Content-Type: application/json');
    return 0;
}
    $v_response = f_api_request($v_method, $v_url, $t_headers=$t_headers); // API request
 
    $v_json_response = json_decode($v_response, true);
/**
    if(!array_key_exists("error_message", $v_json_response)){
  * Get instance.
        $v_instance = $v_json_response['results'][0]; // Get instance
*
        var_dump($v_instance);
* @param  string  $dbalias : Dbalias name
        return 0;
* @return bool
    }
  */
    else{
function getInstance(string $dbalias): bool
        $v_error_message = $v_json_response['error_message'];
{
        echo("Error message [$v_error_message]");
    global $bearerToken;
        return 1;
 
    }
    // Init request
}
    $method  = "GET";
    $url    = "http://admin-webdba.dbsqware.com/lib/apiUpdate.php?dbalias={$dbalias}";
/**
    $headers = [
  * f_get_instances recovers instances objects
        "Authorization: Bearer {$bearerToken}",
  *
        'Content-Type: application/json'
  * @return $v_instances          : instances objects
    ];
  */
 
function f_get_instances(){
    // API request
    global $v_bearer_token;
    $response = apiRequest($method, $url, $headers=$headers);
    $v_method  = "GET";
    if (empty($response)) {
    $v_url    = "http://my_sqwareweb_url/lib/apiUpdate.php";
        echo('Failure of the API request !');
    $t_headers = array('Authorization: Bearer '.$v_bearer_token.'', 'Content-Type: application/json');
        return 0;
    }
    $v_response = f_api_request($v_method, $v_url, $t_headers=$t_headers); # API request
 
    $v_json_response = json_decode($v_response, true);
    $response = json_decode($response, true);
    if(!array_key_exists("error_message", $v_json_response)){
    if (array_key_exists('error_message', $response)) {
        $v_instance = $v_json_response['results']; // Get instances
        echo("Error message [{$response['error_message']}]");
        var_dump($v_instance);
        return 0;
        return 0;
    }
    }
 
    else{
    // Get instance
        $v_error_message = $v_json_response['error_message'];
    $instance = $response['results'][0];
        echo("Error message [$v_error_message]");
    var_dump($instance);
        return 1;
    return 0;
    }
}
}
 
/**
/**
* Get instances.
  * f_post_instance create new instance object
*
  *
* @return bool
  * @param Array $t_data          : JSON object data
*/
  * @return Array mixed[]        : request response
function getInstances(): bool
  */
{
function f_post_instance($t_data){
    global $bearerToken;
    global $v_bearer_token;
 
    $v_method  = "POST";
    // Init request
    $v_url     = "http://my_sqwareweb_url/lib/apiUpdate.php";
    $method  = 'GET';
    $t_headers = array('Authorization: Bearer '.$v_bearer_token.'', 'Content-Type: application/json');
    $url    = "http://admin-webdba.dbsqware.com/lib/apiUpdate.php";
    $t_data    = json_encode($t_data, JSON_FORCE_OBJECT);
    $headers = [
        "Authorization: Bearer {$bearerToken}",
    $v_response = f_api_request($v_method, $v_url, $t_headers=$t_headers, $t_data=$t_data); // API request
        'Content-Type: application/json'
    $v_json_response = json_decode($v_response, true);
    ];
    if(!array_key_exists("error_message", $v_json_response)){
 
        echo("Insert with success !");
    // API request
        return 0;
    $response = apiRequest($method, $url, $headers=$headers);
    }
    if (empty($response)) {
    else{
        echo('Failure of the API request !');
        $v_error_message = $v_json_response['error_message'];
        return 0;
        echo("Error message [$v_error_message]");
    }
        return 1;
 
    }
    $response = json_decode($response, true);
}  
    if (array_key_exists('error_message', $response)) {
        echo("Error message [{$response['error_message']}]");
/**
        return 0;
  * f_put_instance update instance object
     }
  *
 
  * @param String $v_dbalias      : dbalias name
    // Get instances
  * @param Array $t_data          : JSON object data
    $instances = $response['results'];
  * @return Array mixed[]        : request response
    var_dump($instances);
  */
    return 0;
function f_put_instance($v_dbalias, $t_data){
}
    global $v_bearer_token;
 
    $v_method  = "PUT";
/**
    $v_url      = "http://my_sqwareweb_url/lib/apiUpdate.php?dbalias=".$v_dbalias;
* Create new instance.
    $t_headers = array('Authorization: Bearer '.$v_bearer_token.'', 'Content-Type: application/json');
*
    $t_data    = json_encode($t_data, JSON_FORCE_OBJECT);
* @param  array  $data : JSON object data
* @return
    $v_response = f_api_request($v_method, $v_url, $t_headers=$t_headers, $t_data=$t_data); // API request
*/
    $v_json_response = json_decode($v_response, true);
function postInstance(array $data): bool
    if(!array_key_exists("error_message", $v_json_response)){
{
        echo("Update with success !");
    global $bearerToken;
        return 0;
 
    }
    // Init request
    else{
    $method  = 'POST';
        $v_error_message = $v_json_response['error_message'];
    $url    = "http://admin-webdba.dbsqware.com/lib/apiUpdate.php";
        echo("Error message [$v_error_message]");
    $headers = [
        return 1;
        "Authorization: Bearer {$bearerToken}",
    }
        'Content-Type: application/json'
  }
    ];
   
    $data    = json_encode($data, JSON_FORCE_OBJECT);
  function main(){
 
    // Variables
    // API request
    $v_username = "toto";
    $response = apiRequest($method, $url, $headers=$headers, $data=$data);
    $v_password = "toto";
    if (empty($response)) {
    $v_dbalias  = "toto";
        echo('Failure of the API request !');
        return 0;
    // Get bearer token
    }
    f_get_bearer_token($v_username, $v_password);
 
    $response = json_decode($response, true);
    // Post new instance
    if (array_key_exists('error_message', $response)) {
    $t_data = array(
        echo("Error message [{$response['error_message']}]");
                        "dbalias"       => $v_dbalias,
        return 0;
                        "rdbmsname"      => "cassandra",
    }
                        "virt_host_name" => "AA-TEST-API",
 
                        "host_name"      => "NPW",
    echo('Insert with success !');
                        "username"      => "system",
    return 1;
                        "port"          => "1430",
}  
                        "comments"      => "desc IPS_A",
 
                        "contact"        => "OAY .L",
/**
                        "status"        => "ON",
* Update instance.
                        "client"         => "CUST2",
*
                        "env"            => "PRD3",
* @param  string  $dbalias : Dbalias name
                        "globalhost"     => "A",
  * @param  array $data    : JSON object data
                        "custom1"        => "B",
  * @return bool
                        "custom2"       => "C",
*/
                        "dbaname"        => $v_username,
function putInstance(string $dbalias, array $data): bool
                        "comments_upd"  => "insert new instance by API"
{
                    );
    global $bearerToken;
 
    f_post_instance($t_data);
    // Init request
    $method  = 'PUT';
    // Get new instance
    $url    = "http://admin-webdba.dbsqware.com/lib/apiUpdate.php?dbalias={$dbalias}";
    f_get_instance($v_dbalias);
    $headers = [
        "Authorization: Bearer {$bearerToken}",
    // Put new instance
        'Content-Type: application/json'
    $t_data = array(
    ];
                        "dbalias"        => $v_dbalias,
    $data    = json_encode($data, JSON_FORCE_OBJECT);
                        "rdbmsname"      => "mysql",
 
                        "virt_host_name" => "AA-TEST-API",
    // API request
                        "host_name"      => "NPW",
    $response = apiRequest($method, $url, $headers=$headers, $data=$data);
                        "username"      => "system",
    if (empty($response)) {
                        "port"          => "1430",
        echo('Failure of the API request !');
                        "comments"      => "desc IPS_A",
        return 0;
                        "contact"        => "OAY .L",
    }
                        "status"        => "OFF",
 
                        "client"        => "CUST2",
    $response = json_decode($response, true);
                        "env"            => "PRD3",
    if (array_key_exists('error_message', $response)) {
                        "globalhost"    => "A",
         echo("Error message [{$response['error_message']}]");
                        "custom1"        => "B",
        return 0;
                        "custom2"        => "C",
    }
                        "comments_upd"  => "update instance status by API"
 
                    );
     echo("Update with success !");
    return 0;
    f_put_instance($v_dbalias, $t_data);
}
 
    // Get all instances
function main(): void
    f_get_instances();
{
}
    // Variables
    $username = "toto";
main();
    $password = "toto";
    $dbalias  = "toto";
?>
 
    // Get bearer token
    getBearerToken($username, $password);
 
    // Post new instance
    $data = [
        "dbalias"        => $dbalias,
        "rdbmsname"      => "cassandra",
        "virt_host_name" => "AA-TEST-API",
        "host_name"      => "NPW",
        "username"      => "system",
        "port"          => "1430",
        "comments"      => "desc IPS_A",
        "contact"        => "OAY .L",
        "status"        => "ON",
        "client"        => "CUST2",
        "env"            => "PRD3",
        "globalhost"    => "A",
        "custom1"        => "B",
        "custom2"        => "C",
        "dbaname"        => $username,
        "comments_upd"  => "insert new instance by API"
    ];
 
    postInstance($data);
 
    // Get new instance
    getInstance($dbalias);
 
    // Put new instance
    $data = [
        "rdbmsname"      => "mysql",
        "virt_host_name" => "AA-TEST-API",
        "host_name"      => "NPW",
        "username"      => "system",
        "port"          => "1430",
        "comments"      => "desc IPS_A",
        "contact"        => "OAY .L",
        "status"        => "OFF",
        "client"        => "CUST2",
        "env"            => "PRD3",
        "globalhost"    => "A",
        "custom1"        => "B",
        "custom2"        => "C",
        "comments_upd"  => "update instance status by API"
    ];
 
    putInstance($dbalias, $data);
 
    // Get all instances
    getInstances();
}
 
main();
 
?>
</syntaxhighlight>


===Shell===
===Shell===
#!/bin/sh
<syntaxhighlight lang="sh" line>
#!/bin/sh
v_bearer_token=""
 
v_tempfile=api_dbSQWare.tmp
bearerToken=""
tempfile=api_dbSQWare.tmp
<<COMMENTS
 
    f_get_bearer_token recovers bearer token
<<COMMENTS
    Get bearer token.
    :param v_username : account username
 
    :param v_password : account password
    :param username : Account username
    :param v_tempfile : temporary file
    :param password : Account password
    :return           : bearer token
    :param tempfile : Temporary file
COMMENTS
    :return         : Bearer token
f_get_bearer_token(){
COMMENTS
    v_username="$1"
getBearerToken() {
    v_password="$2"
    username="$1"
    v_tempfile="$3"
    password="$2"
    tempfile="$3"
    curl --location --request POST "http://my_sqwareweb_url/lib/apiUpdate_login.php" -H "Content-Type:application/json" -d '{"username":"'$v_username'","password":"'$v_password'"}' > $v_tempfile 2>$v_tempfile.err
 
if [ $(grep -c '"error_message"' $v_tempfile) -eq 0 ]
    curl --location --request POST "http://admin-webdba.dbsqware.com/lib/apiUpdate_login.php" -H "Content-Type:application/json" -d '{"username":"'$username'","password":"'$password'"}' > $tempfile 2>$tempfile.err
then
if [ ! -s "$tempfile" ]; then
        # Get bearer token
        echo 'Failure of the API request !'
v_bearer_token=`cat $v_tempfile|grep '\"token\":\"' | cut -d'"' -f4`
        return 0
        echo "Bearer token : $v_bearer_token"
    fi
return 0
 
else
    if grep -q '"error_message"' "$tempfile"; then
v_error_message=`cat $v_tempfile|grep '\"error_message\": \"' | cut -d'"' -f4`
        errorMessage=$(grep '"error_message": "' "$tempfile" | cut -d'"' -f4)
        echo "Error message [$v_error_message]"
        echo "Error message [$errorMessage]"
return 1
        return 0
fi
    fi
}
 
    # Get bearer token
<<COMMENTS
    bearerToken=`cat $tempfile|grep '\"token\":\"' | cut -d'"' -f4`
    f_get_instance recovers instance object
    echo "Bearer token : $bearerToken"
    return 0
    :param v_dbalias : dbalias name
}
    :return         : instance object
 
COMMENTS
<<COMMENTS
f_get_instance(){
    Get instance.
    v_dbalias="$1"
 
    :param dbalias : Dbalias name
    curl --location --request GET "http://my_sqwareweb_url/lib/apiUpdate.php?dbalias=$v_dbalias" -H "Authorization: Bearer $v_bearer_token" -H "Content-Type:application/json" > $v_tempfile 2>$v_tempfile.err
    :return       : Instance
if [ $(grep -c '"error_message"' $v_tempfile) -eq 0 ]
COMMENTS
then
getInstance() {
        # Get instance
    dbalias="$1"
        v_instance=`cat $v_tempfile|grep -A 1 '\"results\":'`
 
        echo "Instance : $v_instance"
    curl --location --request GET "http://admin-webdba.dbsqware.com/lib/apiUpdate.php?dbalias=$dbalias" -H "Authorization: Bearer $bearerToken" -H "Content-Type:application/json" > $tempfile 2>$tempfile.err
return 0
if [ ! -s "$tempfile" ]; then
else
        echo 'Failure of the API request !'
v_error_message=`cat $v_tempfile|grep '\"error_message\": \"' | cut -d'"' -f4`
        return 0
        echo "Error message [$v_error_message]"
    fi
return 1
 
fi
    if grep -q '"error_message"' "$tempfile"; then
}
        errorMessage=$(grep '"error_message": "' "$tempfile" | cut -d'"' -f4)
        echo "Error message [$errorMessage]"
<<COMMENTS
        return 0
    f_get_instances recovers instances objects
    fi
 
    :return : instances objects
    # Get instance
COMMENTS
    instance=`cat $tempfile|grep -A 1 '\"results\":'`
f_get_instances(){
    echo "Instance : $instance"
    curl --location --request GET "http://my_sqwareweb_url/lib/apiUpdate.php" -H "Authorization: Bearer $v_bearer_token" -H "Content-Type:application/json" > $v_tempfile 2>$v_tempfile.err
    return 0
if [ $(grep -c '"error_message"' $v_tempfile) -eq 0 ]
}
then
 
# Get instances
<<COMMENTS
v_instances=`cat $v_tempfile|grep -A 1 '\"results\":'`
    Get instances.
        echo "Instances : $v_instances"
 
return 0
    :return : Instances
else
COMMENTS
        v_error_message=`cat $v_tempfile|grep '\"error_message\": \"' | cut -d'"' -f4`
getInstances() {
        echo "Error message [$v_error_message]"
    curl --location --request GET "http://admin-webdba.dbsqware.com/lib/apiUpdate.php" -H "Authorization: Bearer $bearerToken" -H "Content-Type:application/json" > $tempfile 2>$tempfile.err
return 1
if [ ! -s "$tempfile" ]; then
fi
        echo 'Failure of the API request !'
}
        return 0
    fi
<<COMMENTS
 
    f_post_instance create new instance object
    if grep -q '"error_message"' "$tempfile"; then
        errorMessage=$(grep '"error_message": "' "$tempfile" | cut -d'"' -f4)
    :param t_data : JSON object data
        echo "Error message [$errorMessage]"
    :return       : request response
        return 0
COMMENTS
    fi
f_post_instance(){
 
    t_data="$1"
    # Get instances
    instances=`cat $tempfile|grep -A 1 '\"results\":'`
    curl --location --request POST "http://my_sqwareweb_url/lib/apiUpdate.php" -H "Authorization: Bearer $v_bearer_token" -H "Content-Type:application/json" -d "$t_data" > $v_tempfile 2>$v_tempfile.err
    echo "Instances : $instances"
if [ $(grep -c '"error_message"' $v_tempfile) -eq 0 ]
    return 0
then
}
echo "Insert with success !"
 
return 0
<<COMMENTS
else
    Create new instance.
v_error_message=`cat $v_tempfile|grep '\"error_message\": \"' | cut -d'"' -f4`
 
        echo "Error message [$v_error_message]"
    :param data : JSON object data
return 1
    :return     : Request response
fi
COMMENTS
}
postInstance() {
    data="$1"
<<COMMENTS
 
    f_put_instance update instance object
    curl --location --request POST "http://admin-webdba.dbsqware.com/lib/apiUpdate.php" -H "Authorization: Bearer $bearerToken" -H "Content-Type:application/json" -d "$data" > $tempfile 2>$tempfile.err
if [ ! -s "$tempfile" ]; then
    :param v_dbalias : dbalias name
        echo 'Failure of the API request !'
    :param t_data   : JSON object data
        return 0
    :return         : request response
    fi
COMMENTS
 
f_put_instance(){
    if grep -q '"error_message"' "$tempfile"; then
    v_dbalias="$1"
        errorMessage=$(grep '"error_message": "' "$tempfile" | cut -d'"' -f4)
    t_data="$2"
        echo "Error message [$errorMessage]"
        return 0
    echo $t_data > test.tmp
    fi
    curl --location --request PUT "http://my_sqwareweb_url/lib/apiUpdate.php?dbalias=$v_dbalias" -H "Authorization: Bearer $v_bearer_token" -H "Content-Type:application/json" -d "$t_data" > $v_tempfile 2>$v_tempfile.err
 
if [ $(grep -c '"error_message"' $v_tempfile) -eq 0 ]
    echo 'Insert with success !'
then
    return 1
echo "Update with success !"
}
return 0
 
else
<<COMMENTS
v_error_message=`cat $v_tempfile|grep '\"error_message\": \"' | cut -d'"' -f4`
    Update instance.
        echo "Error message [$v_error_message]"
 
return 1
    :param dbalias : Dbalias name
fi
    :param data   : JSON object data
}
    :return       : Request response
COMMENTS
main(){
putInstance() {
    # Variables
    dbalias="$1"
    v_username="toto";
    data="$2"
    v_password="toto";
 
    v_dbalias="toto";
    echo $data > test.tmp
    curl --location --request PUT "http://admin-webdba.dbsqware.com/lib/apiUpdate.php?dbalias=$dbalias" -H "Authorization: Bearer $bearerToken" -H "Content-Type:application/json" -d "$data" > $tempfile 2>$tempfile.err
    # Get bearer token
if [ ! -s "$tempfile" ]; then
    f_get_bearer_token $v_username $v_password $v_tempfile
        echo 'Failure of the API request !'
        return 0
    # Post new instance
    fi
    t_data='{"dbalias":"'$v_dbalias'","rdbmsname":"cassandra","virt_host_name":"AA-TEST-API","host_name":"NPW","username":"system","port":"1430","comments":"desc IPS_A","contact":"OAY .L","status":"ON","client":"CUST2","env":"PRD3","globalhost":"A","custom1":"B","custom2":"C","dbaname":"'$v_username'","comments_upd":"insert new instance by API"}'
 
    f_post_instance "$t_data"
    if grep -q '"error_message"' "$tempfile"; then
        errorMessage=$(grep '"error_message": "' "$tempfile" | cut -d'"' -f4)
    # Get new instance
        echo "Error message [$errorMessage]"
    f_get_instance $v_dbalias
        return 0
    fi
    # Put new instance
 
    t_data='{"rdbmsname":"mysql","virt_host_name":"AA-TEST-API","host_name":"NPW","username":"system","port":"1430","comments":"desc IPS_A","contact":"OAY .L","status":"OFF","client":"CUST2","env":"PRD3","globalhost":"A","custom1":"B","custom2":"C","comments_upd":"update instance status by API"}'
    echo 'Update with success !'
    f_put_instance $v_dbalias "$t_data"
    return 1
}
    # Get all instances
 
    f_get_instances
main() {
}
    # Variables
    username="toto";
main
    password="toto";
    dbalias="toto";
 
    # Get bearer token
    getBearerToken $username $password $tempfile
 
    # Post new instance
    data='{"dbalias":"'$dbalias'","rdbmsname":"cassandra","virhosname":"AA-TEST-API","hosname":"NPW","username":"system","port":"1430","comments":"desc IPS_A","contact":"OAY .L","status":"ON","client":"CUST2","env":"PRD3","globalhost":"A","custom1":"B","custom2":"C","comments_upd":"insert new instance by API"}'
    postInstance "$data"
 
    # Get new instance
    getInstance $dbalias
 
    # Put new instance
    data='{"rdbmsname":"mysql","virhosname":"AA-TEST-API","hosname":"NPW","username":"system","port":"1430","comments":"desc IPS_A","contact":"OAY .L","status":"OFF","client":"CUST2","env":"PRD3","globalhost":"A","custom1":"B","custom2":"C","comments_upd":"update instance status by API"}'
    putInstance $dbalias "$data"
 
    # Get all instances
    getInstances
}
 
main
</syntaxhighlight>

Dernière version du 25 juin 2025 à 17:16

Généralités

Limites de cette section

Cette section n’a pas la prétention de traiter tous les cas possibles d'utilisation de l'API de mise à jour du référentiel.
Vous devez être à l'aise avec le concept des API et la gestion de token, si ce n'est pas le cas, faites appel au support.
Attention, vous allez mettre à jour la table du référentiel général, insert et/ou update, vous devez savoir ce que vous faites !

Paramétrage préalable

Afin de pouvoir utiliser l'API "update", vous devez au préalable avoir paramétré un login avec le type d'authentification "internal_api_update".

Exemples d'utilisation

Vous devez adapter les exemples suivants avec vos propres données (URL, user, password, ...)
Ceci représente une base pour du Python, PHP et shell.
Vous pouvez bien évidemment utiliser le langage de votre choix.

Python

import requests
import json

bearerToken = ""

"""
Does API request.

:param method  : Request method
:param url     : Website url
:param headers : JSON object headers 
:param data    : JSON object data
:return        : API request return
"""
def apiRequest(method, url, headers = {}, data = {}):
    response = requests.request(method, url, headers=headers, data=data)
    return response  

"""
Get bearer token.

:param username : Account username
:param password : Account password
:return         : Bearer token
"""
def getBearerToken(username, password):
    # Init request
    method  = 'POST'
    url     = "http://admin-webdba.dbsqware.com/lib/apiUpdate_login.php"
    headers = {'Content-Type': 'application/json'}
    data    = json.dumps({'username': username, 'password': password})

    # API request
    response = apiRequest(method, url, headers=headers, data=data)
    if response == '':
        print('Failure of the API request !')
        return 0

    response = json.loads(response.text)
    if 'error_message' in response:
        print(f"Error message [{response['error_message']}]")
        return 0

    # Get token response
    global bearerToken
    bearerToken = response['results']['token']
    print(f"Bearer token : {bearerToken}")
    return 1

"""
Get instance.

:param dbalias : Dbalias name
:return        : Instance
"""
def getInstance(dbalias):
    # Init request
    method  = 'GET'
    url     = f"http://admin-webdba.dbsqware.com/lib/apiUpdate.php?dbalias={dbalias}"
    headers = {
        'Authorization': f"Bearer {bearerToken}",
        'Content-Type': 'application/json'
    }

    # API request
    response = apiRequest(method, url, headers=headers)
    if response == '':
        print('Failure of the API request !')
        return 0

    response = json.loads(response.text)
    if 'error_message' in response:
        print(f"Error message [{response['error_message']}]")
        return 0

    # Get instance
    instance = response['results'][0]
    print(instance)
    return 1

"""
Get instances.

:return : Instances
"""
def getInstances():
    # Init request
    method  = 'GET'
    url     = "http://admin-webdba.dbsqware.com/lib/apiUpdate.php"
    headers = {
        'Authorization': f"Bearer {bearerToken}",
        'Content-Type': 'application/json'
    }

    # API request
    response = apiRequest(method, url, headers=headers)
    if response == '':
        print('Failure of the API request !')
        return 0

    response = json.loads(response.text)
    if 'error_message' in response:
        print(f"Error message [{response['error_message']}]")
        return 0

    # Get instances
    instances = response['results']
    print(instances)
    return 1

"""
Create new instance.

:param data : JSON object data
:return     : Request response
"""
def postInstance(data):
    # Init request
    method  = 'POST'
    url     = "http://admin-webdba.dbsqware.com/lib/apiUpdate.php"
    headers = {
        'Authorization': f"Bearer {bearerToken}",
        'Content-Type': 'application/json'
    }
    data    = json.dumps(data)

    # API request
    response = apiRequest(method, url, headers=headers, data=data)
    if response == '':
        print('Failure of the API request !')
        return 0

    response = json.loads(response.text)
    if 'error_message' in response:
        print(f"Error message [{response['error_message']}]")
        return 0

    print('Insert with success !')
    return 1

"""
Update instance.

:param dbalias : Dbalias name
:param data    : JSON object data
:return        : Request response
"""
def putInstance(dbalias, data):
    # Init request
    method  = 'PUT'
    url     = f"http://admin-webdba.dbsqware.com/lib/apiUpdate.php?dbalias={dbalias}"
    headers = {
        'Authorization': f"Bearer {bearerToken}",
        'Content-Type': 'application/json'
    }
    data    = json.dumps(data)

    # API request
    response = apiRequest(method, url, headers=headers, data=data)
    if response == '':
        print('Failure of the API request !')
        return 0

    response = json.loads(response.text)
    if 'error_message' in response:
        print(f"Error message [{response['error_message']}]")
        return 0

    print("Update with success !")
    return 1

def main():
    # Variables
    username = "toto"
    password = "toto"
    dbalias  = "toto"

    # Get bearer token
    getBearerToken(username, password)

    # Post new instance
    data = {
        "dbalias"       : dbalias,
        "rdbmsname"     : "cassandra",
        "virt_host_name": "AA-TEST-API",
        "host_name"     : "NPW",
        "username"      : "system",
        "port"          : "1430",
        "comments"      : "desc IPS_A",
        "contact"       : "OAY .L",
        "status"        : "ON",
        "client"        : "CUST2",
        "env"           : "PRD3",
        "globalhost"    : "A",
        "custom1"       : "B",
        "custom2"       : "C",
        "comments_upd"  : "insert new instance by API"
    }

    postInstance(data)

    # Get new instance
    getInstance(dbalias)

    # Put new instance
    data = {
        "rdbmsname"     : "mysql",
        "virt_host_name": "AA-TEST-API",
        "host_name"     : "NPW",
        "username"      : "system",
        "port"          : "1430",
        "comments"      : "desc IPS_A",
        "contact"       : "OAY .L",
        "status"        : "OFF",
        "client"        : "CUST2",
        "env"           : "PRD3",
        "globalhost"    : "A",
        "custom1"       : "B",
        "custom2"       : "C",
        "comments_upd"  : "update instance status by API"
    }

    putInstance(dbalias, data)

    # Get all instances
    getInstances()

if __name__ == '__main__':
    main()

PHP

<?php

$bearerToken = "";

/**
 * Does API request.
 * 
 * @param  string  $method : Request method
 * @param  string  $url    : Website url
 * @param  array  $headers : JSON object headers 
 * @param  string  $data   : JSON object data
 * @return string
 */
function apiRequest(string $method, string $url, array $headers = [], $data = '{}'): string
{
    $curl = curl_init();

    curl_setopt_array($curl, [
        CURLOPT_URL            => $url,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_ENCODING       => '',
        CURLOPT_MAXREDIRS      => 10,
        CURLOPT_TIMEOUT        => 0,
        CURLOPT_FOLLOWLOCATION => true,
        CURLOPT_HTTP_VERSION   => CURL_HTTP_VERSION_1_1,
        CURLOPT_SSL_VERIFYHOST => 0,
        CURLOPT_SSL_VERIFYPEER => 0,
        CURLOPT_CUSTOMREQUEST  => $method,
        CURLOPT_POSTFIELDS     => $data,
        CURLOPT_HTTPHEADER     => $headers,
    ]);

    $response = curl_exec($curl);
    curl_close($curl);

    return $response;
}

/**
 * Get bearer token.
 * 
 * @param  string  $username : Account username
 * @param  string  $password : Account password
 * @return bool
 */
function getBearerToken(string $username, string $password): bool
{
    // Init request
    $method  = 'POST';
    $url     = "http://admin-webdba.dbsqware.com/lib/apiUpdate_login.php";
    $headers = ['Content-Type: application/json'];
    $data    = json_encode(['username' => $username, 'password' => $password], JSON_FORCE_OBJECT);

    // API request
    $response = apiRequest($method, $url, $headers=$headers, $data=$data);
    if (empty($response)) {
        echo('Failure of the API request !');
        return 0;
    }

    $response = json_decode($response, true);
    if (array_key_exists('error_message', $response)) {
        echo("Error message [{$response['error_message']}]");
        return 0;
    }

    // Get token response
    global $bearerToken;
    $bearerToken = $response['results']['token'];
    echo("Bearer token : {$bearerToken}");
    return 0;
}

/**
 * Get instance.
 * 
 * @param  string  $dbalias : Dbalias name
 * @return bool
 */
function getInstance(string $dbalias): bool
{
    global $bearerToken;

    // Init request
    $method  = "GET";
    $url     = "http://admin-webdba.dbsqware.com/lib/apiUpdate.php?dbalias={$dbalias}";
    $headers = [
        "Authorization: Bearer {$bearerToken}",
        'Content-Type: application/json'
    ];

    // API request
    $response = apiRequest($method, $url, $headers=$headers);
    if (empty($response)) {
        echo('Failure of the API request !');
        return 0;
    }

    $response = json_decode($response, true);
    if (array_key_exists('error_message', $response)) {
        echo("Error message [{$response['error_message']}]");
        return 0;
    }

    // Get instance
    $instance = $response['results'][0];
    var_dump($instance);
    return 0;
}

/**
 * Get instances.
 * 
 * @return bool
 */
function getInstances(): bool
{
    global $bearerToken;

    // Init request
    $method  = 'GET';
    $url     = "http://admin-webdba.dbsqware.com/lib/apiUpdate.php";
    $headers = [
        "Authorization: Bearer {$bearerToken}",
        'Content-Type: application/json'
    ];

    // API request
    $response = apiRequest($method, $url, $headers=$headers);
    if (empty($response)) {
        echo('Failure of the API request !');
        return 0;
    }

    $response = json_decode($response, true);
    if (array_key_exists('error_message', $response)) {
        echo("Error message [{$response['error_message']}]");
        return 0;
    }

    // Get instances
    $instances = $response['results'];
    var_dump($instances);
    return 0;
}

/**
 * Create new instance.
 * 
 * @param  array  $data : JSON object data
 * @return
 */
function postInstance(array $data): bool
{
    global $bearerToken;

    // Init request
    $method  = 'POST';
    $url     = "http://admin-webdba.dbsqware.com/lib/apiUpdate.php";
    $headers = [
        "Authorization: Bearer {$bearerToken}",
        'Content-Type: application/json'
    ];
    $data    = json_encode($data, JSON_FORCE_OBJECT);

    // API request
    $response = apiRequest($method, $url, $headers=$headers, $data=$data);
    if (empty($response)) {
        echo('Failure of the API request !');
        return 0;
    }

    $response = json_decode($response, true);
    if (array_key_exists('error_message', $response)) {
        echo("Error message [{$response['error_message']}]");
        return 0;
    }

    echo('Insert with success !');
    return 1;
} 

/**
 * Update instance.
 * 
 * @param  string  $dbalias : Dbalias name
 * @param  array  $data     : JSON object data
 * @return bool
 */
function putInstance(string $dbalias, array $data): bool
{
    global $bearerToken;

    // Init request
    $method  = 'PUT';
    $url     = "http://admin-webdba.dbsqware.com/lib/apiUpdate.php?dbalias={$dbalias}";
    $headers = [
        "Authorization: Bearer {$bearerToken}",
        'Content-Type: application/json'
    ];
    $data    = json_encode($data, JSON_FORCE_OBJECT);

    // API request
    $response = apiRequest($method, $url, $headers=$headers, $data=$data);
    if (empty($response)) {
        echo('Failure of the API request !');
        return 0;
    }

    $response = json_decode($response, true);
    if (array_key_exists('error_message', $response)) {
        echo("Error message [{$response['error_message']}]");
        return 0;
    }

    echo("Update with success !");
    return 0;
}

function main(): void
{
    // Variables
    $username = "toto";
    $password = "toto";
    $dbalias  = "toto";

    // Get bearer token
    getBearerToken($username, $password);

    // Post new instance
    $data = [
        "dbalias"        => $dbalias,
        "rdbmsname"      => "cassandra",
        "virt_host_name" => "AA-TEST-API",
        "host_name"      => "NPW",
        "username"       => "system",
        "port"           => "1430",
        "comments"       => "desc IPS_A",
        "contact"        => "OAY .L",
        "status"         => "ON",
        "client"         => "CUST2",
        "env"            => "PRD3",
        "globalhost"     => "A",
        "custom1"        => "B",
        "custom2"        => "C",
        "dbaname"        => $username,
        "comments_upd"   => "insert new instance by API"
    ];

    postInstance($data);

    // Get new instance
    getInstance($dbalias);

    // Put new instance
    $data = [
        "rdbmsname"      => "mysql",
        "virt_host_name" => "AA-TEST-API",
        "host_name"      => "NPW",
        "username"       => "system",
        "port"           => "1430",
        "comments"       => "desc IPS_A",
        "contact"        => "OAY .L",
        "status"         => "OFF",
        "client"         => "CUST2",
        "env"            => "PRD3",
        "globalhost"     => "A",
        "custom1"        => "B",
        "custom2"        => "C",
        "comments_upd"   => "update instance status by API"
    ];

    putInstance($dbalias, $data);

    // Get all instances
    getInstances();
}

main();

?>

Shell

#!/bin/sh

bearerToken=""
tempfile=api_dbSQWare.tmp

<<COMMENTS
    Get bearer token.

    :param username : Account username
    :param password : Account password
    :param tempfile : Temporary file
    :return         : Bearer token
COMMENTS
getBearerToken() {
    username="$1"
    password="$2"
    tempfile="$3"

    curl --location --request POST "http://admin-webdba.dbsqware.com/lib/apiUpdate_login.php" -H "Content-Type:application/json" -d '{"username":"'$username'","password":"'$password'"}' > $tempfile 2>$tempfile.err
	if [ ! -s "$tempfile" ]; then
        echo 'Failure of the API request !'
        return 0
    fi

    if grep -q '"error_message"' "$tempfile"; then
        errorMessage=$(grep '"error_message": "' "$tempfile" | cut -d'"' -f4)
        echo "Error message [$errorMessage]"
        return 0
    fi

    # Get bearer token
    bearerToken=`cat $tempfile|grep '\"token\":\"' | cut -d'"' -f4`
    echo "Bearer token : $bearerToken"
    return 0
}

<<COMMENTS
    Get instance.

    :param dbalias : Dbalias name
    :return        : Instance
COMMENTS
getInstance() {
    dbalias="$1"

    curl --location --request GET "http://admin-webdba.dbsqware.com/lib/apiUpdate.php?dbalias=$dbalias" -H "Authorization: Bearer $bearerToken" -H "Content-Type:application/json" > $tempfile 2>$tempfile.err
	if [ ! -s "$tempfile" ]; then
        echo 'Failure of the API request !'
        return 0
    fi

    if grep -q '"error_message"' "$tempfile"; then
        errorMessage=$(grep '"error_message": "' "$tempfile" | cut -d'"' -f4)
        echo "Error message [$errorMessage]"
        return 0
    fi

    # Get instance
    instance=`cat $tempfile|grep -A 1 '\"results\":'`
    echo "Instance : $instance"
    return 0
}

<<COMMENTS
    Get instances.

    :return : Instances
COMMENTS
getInstances() {
    curl --location --request GET "http://admin-webdba.dbsqware.com/lib/apiUpdate.php" -H "Authorization: Bearer $bearerToken" -H "Content-Type:application/json" > $tempfile 2>$tempfile.err
	if [ ! -s "$tempfile" ]; then
        echo 'Failure of the API request !'
        return 0
    fi

    if grep -q '"error_message"' "$tempfile"; then
        errorMessage=$(grep '"error_message": "' "$tempfile" | cut -d'"' -f4)
        echo "Error message [$errorMessage]"
        return 0
    fi

    # Get instances
    instances=`cat $tempfile|grep -A 1 '\"results\":'`
    echo "Instances : $instances"
    return 0
}

<<COMMENTS
    Create new instance.

    :param data : JSON object data
    :return     : Request response
COMMENTS
postInstance() {
    data="$1"

    curl --location --request POST "http://admin-webdba.dbsqware.com/lib/apiUpdate.php" -H "Authorization: Bearer $bearerToken" -H "Content-Type:application/json" -d "$data" > $tempfile 2>$tempfile.err
	if [ ! -s "$tempfile" ]; then
        echo 'Failure of the API request !'
        return 0
    fi

    if grep -q '"error_message"' "$tempfile"; then
        errorMessage=$(grep '"error_message": "' "$tempfile" | cut -d'"' -f4)
        echo "Error message [$errorMessage]"
        return 0
    fi

    echo 'Insert with success !'
    return 1
}

<<COMMENTS
    Update instance.

    :param dbalias : Dbalias name
    :param data    : JSON object data
    :return        : Request response
COMMENTS
putInstance() {
    dbalias="$1"
    data="$2"

    echo $data > test.tmp
    curl --location --request PUT "http://admin-webdba.dbsqware.com/lib/apiUpdate.php?dbalias=$dbalias" -H "Authorization: Bearer $bearerToken" -H "Content-Type:application/json" -d "$data" > $tempfile 2>$tempfile.err
	if [ ! -s "$tempfile" ]; then
        echo 'Failure of the API request !'
        return 0
    fi

    if grep -q '"error_message"' "$tempfile"; then
        errorMessage=$(grep '"error_message": "' "$tempfile" | cut -d'"' -f4)
        echo "Error message [$errorMessage]"
        return 0
    fi

    echo 'Update with success !'
    return 1
}

main() {
    # Variables
    username="toto";
    password="toto";
    dbalias="toto";

    # Get bearer token
    getBearerToken $username $password $tempfile

    # Post new instance
    data='{"dbalias":"'$dbalias'","rdbmsname":"cassandra","virhosname":"AA-TEST-API","hosname":"NPW","username":"system","port":"1430","comments":"desc IPS_A","contact":"OAY .L","status":"ON","client":"CUST2","env":"PRD3","globalhost":"A","custom1":"B","custom2":"C","comments_upd":"insert new instance by API"}'
    postInstance "$data"

    # Get new instance
    getInstance $dbalias

    # Put new instance
    data='{"rdbmsname":"mysql","virhosname":"AA-TEST-API","hosname":"NPW","username":"system","port":"1430","comments":"desc IPS_A","contact":"OAY .L","status":"OFF","client":"CUST2","env":"PRD3","globalhost":"A","custom1":"B","custom2":"C","comments_upd":"update instance status by API"}'
    putInstance $dbalias "$data"

    # Get all instances
    getInstances
}

main