« Général:FAQ API backup » : différence entre les versions
Aller à la navigation
Aller à la recherche
Aucun résumé des modifications |
Aucun résumé des modifications |
||
| (6 versions intermédiaires par le même utilisateur non affichées) | |||
| Ligne 19 : | Ligne 19 : | ||
import json | import json | ||
staticToken = "" | |||
""" | """ | ||
Does API request. | |||
:param | :param method : Request method | ||
:param | :param url : Website url | ||
:param | :param headers : JSON object headers | ||
:param | :param data : JSON object data | ||
:return | :return : API request return | ||
""" | """ | ||
def | def apiRequest(method, url, headers = {}, data = {}): | ||
response = requests.request(method, url, headers=headers, data=data) | |||
return | return response | ||
""" | """ | ||
Create new backup trace. | |||
:param | :param data : JSON object data | ||
:return | :return : Request response | ||
""" | """ | ||
def | def postBackupTrace(data): | ||
# Init request | |||
method = 'POST' | |||
url = f"https://webdba.dbsqware.com/lib/apiAudit.php?type=backup&token={staticToken}" | |||
headers = {'Content-Type': 'application/json'} | |||
data = json.dumps(data) | |||
# API request | |||
response = apiRequest(method, url, headers=headers, data=data) | |||
if response == '': | |||
print( | print('Failure of the API request !') | ||
return 0 | 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 | |||
def main(): | def main(): | ||
# Post new backup trace | # Post new backup trace | ||
data = { | |||
"dbalias" : "AATestAPI_Upd", | |||
"database_name": "master", | |||
"beginning" : "2022-07-11 09:30:00", | |||
"bck_type" : "full stripe 1 comp 1 NewSyntax", | |||
"tools" : "dump", | |||
"end_trt" : "2022-07-11 09:35:00", | |||
"size_bck" : "59.26" | |||
} | |||
postBackupTrace(data) | |||
if __name__ == '__main__': | if __name__ == '__main__': | ||
| Ligne 78 : | Ligne 83 : | ||
<?php | <?php | ||
$ | $staticToken = ""; | ||
/** | /** | ||
* | * Does API request. | ||
* | * | ||
* @param | * @param string $method : Request method | ||
* @param | * @param string $url : Website url | ||
* @param | * @param array $headers : JSON object headers | ||
* @param | * @param string $data : JSON object data | ||
* @return | * @return string | ||
*/ | */ | ||
function | function apiRequest(string $method, string $url, array $headers = [], $data = '{}'): string | ||
$ | { | ||
$curl = curl_init(); | |||
curl_setopt_array($ | curl_setopt_array($curl, [ | ||
CURLOPT_URL => $ | CURLOPT_URL => $url, | ||
CURLOPT_RETURNTRANSFER => true, | CURLOPT_RETURNTRANSFER => true, | ||
CURLOPT_ENCODING => '', | CURLOPT_ENCODING => '', | ||
| Ligne 100 : | Ligne 106 : | ||
CURLOPT_FOLLOWLOCATION => true, | CURLOPT_FOLLOWLOCATION => true, | ||
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, | CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, | ||
CURLOPT_CUSTOMREQUEST => $ | CURLOPT_SSL_VERIFYHOST => 0, | ||
CURLOPT_POSTFIELDS => $ | CURLOPT_SSL_VERIFYPEER => 0, | ||
CURLOPT_HTTPHEADER => $ | CURLOPT_CUSTOMREQUEST => $method, | ||
CURLOPT_POSTFIELDS => $data, | |||
CURLOPT_HTTPHEADER => $headers, | |||
]); | |||
$ | $response = curl_exec($curl); | ||
return $ | curl_close($curl); | ||
return $response; | |||
} | } | ||
/** | /** | ||
* | * Create new backup trace. | ||
* | * | ||
* @param | * @param array $data : JSON object data | ||
* @return | * @return bool | ||
*/ | */ | ||
function | function postBackupTrace($data): bool | ||
global $ | { | ||
global $staticToken; | |||
$ | // Init request | ||
$ | $method = 'POST'; | ||
if( | $url = "https://webdba.dbsqware.com/lib/apiAudit.php?type=backup&token={$staticToken}"; | ||
echo( | $headers = ['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; | return 0; | ||
} | } | ||
$response = json_decode($response, true); | |||
echo("Error message [$ | if (array_key_exists('error_message', $response)) { | ||
return | echo("Error message [{$response['error_message']}]"); | ||
return 0; | |||
} | } | ||
echo('Insert with success !'); | |||
return 1; | |||
} | } | ||
function main(){ | function main(): void | ||
{ | |||
// Post new backup trace | // Post new backup trace | ||
$ | $data = [ | ||
"dbalias" => "AATestAPI_Upd", | |||
"database_name" => "master", | |||
"beginning" => "2022-07-11 09:20:00", | |||
"bck_type" => "full stripe 1 comp 1 NewSyntax", | |||
"tools" => "dump", | |||
"end_trt" => "2022-07-11 09:25:00", | |||
"size_bck" => "59.26" | |||
]; | |||
postBackupTrace($data); | |||
} | } | ||
| Ligne 159 : | Ligne 177 : | ||
#!/bin/sh | #!/bin/sh | ||
staticToken="" | |||
tempfile=api_dbSQWare.tmp | |||
<<COMMENTS | <<COMMENTS | ||
Create new backup trace. | |||
:param | :param data : JSON object data | ||
:return : request response | :return : request response | ||
COMMENTS | COMMENTS | ||
postBackupTrace() { | |||
data="$1" | |||
curl --location --request POST " | # API request | ||
if [ | curl --location --request POST "https://webdba.dbsqware.com/lib/apiAudit.php?type=backup&token=$staticToken" -H "Content-Type:application/json" -d "$data" > $tempfile 2>$tempfile.err | ||
if [ ! -s "$tempfile" ]; then | |||
echo 'Failure of the API request !' | |||
return 0 | |||
fi | |||
echo "Error message [$ | 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 | |||
} | } | ||
main(){ | main() { | ||
# Post new backup trace | # Post new backup trace | ||
data='{"dbalias":"AATestAPI_Upd","database_name":"master","beginning":"2022-07-11 09:35:00","bck_type":"full stripe 1 comp 1 NewSyntax","tools":"dump","end_trt":"2022-07-11 09:40:00","size_bck":"59.26"}' | |||
postBackupTrace "$data" | |||
} | } | ||
| Ligne 194 : | Ligne 217 : | ||
===PowerShell=== | ===PowerShell=== | ||
<syntaxhighlight lang="powershell" line> | <syntaxhighlight lang="powershell" line> | ||
$ | $staticToken = "" | ||
$ | $tempfile = ".\api_dbSQWare.json" | ||
<# | <# | ||
Create new backup trace. | |||
:param | :param data : JSON object data | ||
:return | :return : Request response | ||
#> | #> | ||
function | function postBackupTrace($data) { | ||
$ | $params = @{ | ||
Method = | Method = 'Post' | ||
Uri = " | Uri = "https://webdba.dbsqware.com/lib/apiAudit.php?type=backup&token=$staticToken" | ||
Body = $ | Body = $data | ConvertTo-Json | ||
ContentType = | ContentType = 'application/json' | ||
} | } | ||
Invoke-RestMethod @t_params -OutFile $ | Invoke-RestMethod @t_params -OutFile $tempfile | ||
$ | # API request | ||
$response = Get-Content -Path $tempfile | ConvertFrom-Json | |||
if( | if (-not $response) { | ||
Write-Output | Write-Output 'Failure of the API request !' | ||
return | return 0 | ||
} | } | ||
if ($response.PSObject.Properties.Name -contains 'error_message') { | |||
Write-Output "Error message [$ | Write-Output "Error message [$($response.error_message)]" | ||
return 0 | return 0 | ||
} | } | ||
} | } | ||
function main{ | function main { | ||
# Post | # Post new backup trace | ||
$ | $data = @{ | ||
dbalias = "AATestAPI_Upd" | dbalias = "AATestAPI_Upd" | ||
database_name = "master" | database_name = "master" | ||
| Ligne 236 : | Ligne 260 : | ||
size_bck = "59.26" | size_bck = "59.26" | ||
} | } | ||
postBackupTrace($data) | |||
} | } | ||
main | main | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Dernière version du 25 juin 2025 à 17:20
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 des indicateurs.
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 les tables de traçage des jobs, vous devez savoir ce que vous faites !
Paramétrage préalable
Afin de pouvoir utiliser l'API des indicateurs, vous devez au préalable avoir paramétré un token statique pour l'instance cible.
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, Shell et PowerShell.
Vous pouvez bien évidemment utiliser le langage de votre choix.
Python
import requests
import json
staticToken = ""
"""
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
"""
Create new backup trace.
:param data : JSON object data
:return : Request response
"""
def postBackupTrace(data):
# Init request
method = 'POST'
url = f"https://webdba.dbsqware.com/lib/apiAudit.php?type=backup&token={staticToken}"
headers = {'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
def main():
# Post new backup trace
data = {
"dbalias" : "AATestAPI_Upd",
"database_name": "master",
"beginning" : "2022-07-11 09:30:00",
"bck_type" : "full stripe 1 comp 1 NewSyntax",
"tools" : "dump",
"end_trt" : "2022-07-11 09:35:00",
"size_bck" : "59.26"
}
postBackupTrace(data)
if __name__ == '__main__':
main()
PHP
<?php
$staticToken = "";
/**
* 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;
}
/**
* Create new backup trace.
*
* @param array $data : JSON object data
* @return bool
*/
function postBackupTrace($data): bool
{
global $staticToken;
// Init request
$method = 'POST';
$url = "https://webdba.dbsqware.com/lib/apiAudit.php?type=backup&token={$staticToken}";
$headers = ['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;
}
function main(): void
{
// Post new backup trace
$data = [
"dbalias" => "AATestAPI_Upd",
"database_name" => "master",
"beginning" => "2022-07-11 09:20:00",
"bck_type" => "full stripe 1 comp 1 NewSyntax",
"tools" => "dump",
"end_trt" => "2022-07-11 09:25:00",
"size_bck" => "59.26"
];
postBackupTrace($data);
}
main();
?>
Shell
#!/bin/sh
staticToken=""
tempfile=api_dbSQWare.tmp
<<COMMENTS
Create new backup trace.
:param data : JSON object data
:return : request response
COMMENTS
postBackupTrace() {
data="$1"
# API request
curl --location --request POST "https://webdba.dbsqware.com/lib/apiAudit.php?type=backup&token=$staticToken" -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
}
main() {
# Post new backup trace
data='{"dbalias":"AATestAPI_Upd","database_name":"master","beginning":"2022-07-11 09:35:00","bck_type":"full stripe 1 comp 1 NewSyntax","tools":"dump","end_trt":"2022-07-11 09:40:00","size_bck":"59.26"}'
postBackupTrace "$data"
}
main
PowerShell
$staticToken = ""
$tempfile = ".\api_dbSQWare.json"
<#
Create new backup trace.
:param data : JSON object data
:return : Request response
#>
function postBackupTrace($data) {
$params = @{
Method = 'Post'
Uri = "https://webdba.dbsqware.com/lib/apiAudit.php?type=backup&token=$staticToken"
Body = $data | ConvertTo-Json
ContentType = 'application/json'
}
Invoke-RestMethod @t_params -OutFile $tempfile
# API request
$response = Get-Content -Path $tempfile | ConvertFrom-Json
if (-not $response) {
Write-Output 'Failure of the API request !'
return 0
}
if ($response.PSObject.Properties.Name -contains 'error_message') {
Write-Output "Error message [$($response.error_message)]"
return 0
}
}
function main {
# Post new backup trace
$data = @{
dbalias = "AATestAPI_Upd"
database_name = "master"
beginning = "2022-07-11 10:35:00"
bck_type = "full stripe 1 comp 1 NewSyntax"
tools = "dump"
end_trt = "2022-07-11 10:40:00"
size_bck = "59.26"
}
postBackupTrace($data)
}
main