You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 

181 lines
4.3 KiB

<?php
namespace SamApiLib;
class SamApi
{
private const ws_accept = "application/json";
private const ws_content_type = "application/json";
private const ws_user_agent = "service";
private $base_url;
private $api_token;
function __construct($base_url, $api_token)
{
$this->base_url = $base_url;
$this->api_token = $api_token;
}
/**
* REST API query helper
* @param $ws_route
* @param $url_params
* @param $query_params
* @param $body_params
* @return array
*/
public function query($ws_route, $url_params, $query_params, $body_params)
{
list($ws_method, $ws_url) = explode(" ", trim($ws_route));
$query_string = http_build_query($query_params);
foreach ($url_params as $url_param => $value)
{
$ws_url = str_replace('{'.$url_param.'}', $value, $ws_url);
}
$url = $this->base_url . $ws_url . "?" . $query_string;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, ["Accept: ".$this::ws_accept, "Content-type: ".$this::ws_content_type]);
curl_setopt($ch, CURLOPT_USERAGENT, $this::ws_user_agent);
$ws_method = strtoupper($ws_method);
if($ws_method !== "GET")
{
if($ws_method === "POST")
{
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, (json_encode($body_params)));
}
else
{
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $ws_method);
}
}
curl_setopt($ch, CURLOPT_COOKIE, "LAABS-AUTH=".urlencode($this->api_token));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$res = curl_exec($ch);
if ($res === false)
{
echo "curl error #" . curl_errno($ch) . " : " . curl_error($ch); die;
}
curl_close($ch);
list($headers, $body) = explode("\r\n\r\n", $res); // PHP_EOL.PHP_EOL
$headers = explode(PHP_EOL, $headers);
$body2 = json_decode($body, true);
if(json_last_error() === JSON_ERROR_NONE)
{ // content was really json
$body = $body2; // use decoded content
}
return [$headers, $body];
}
/**
* get the file plan (organization structure)
* @return array
*/
public function getFilePlan ()
{
$ws_route = " get /filePlan/filePlan/Tree";
$url_params = [];
$query_params = [];
$body_params = [];
list($headers, $body) = $this->query($ws_route, $url_params, $query_params, $body_params);
return $body;
}
/**
* get a user by its login
* @param $login
* @return array
*/
public function getUsersByLogin ($login)
{
$ws_route = " get /auth/userAccount/Search";
$url_params = [];
$query_params = [
"query" => "accountName = '$login'",
];
$body_params = [];
list($headers, $body) = $this->query($ws_route, $url_params, $query_params, $body_params);
if(count($body) > 0)
return $body[0];
else
null;
}
/**
* get the Lifecycle journal
* @param string $eventType
* @param string $objectClass
* @param string $objectId
* @param string $minDate
* @param string $maxDate
* @param string $org
* @param number $maxResults
* @return array
*/
public function getLifeCycleJournals ($eventType="", $objectClass="", $objectId="", $minDate="", $maxDate="", $org="", $maxResults=50)
{
$ws_route = "get /lifeCycle/event/Search";
$url_params = [];
$query_params = [
"eventType" => $eventType,
"objectClass" => $objectClass,
"objectId" => $objectId,
"minDate" => $minDate,
"maxDate" => $maxDate,
"org" => $org,
"maxResults" => $maxResults,
];
$body_params = [];
list($headers, $body) = $this->query($ws_route, $url_params, $query_params, $body_params);
return $body;
}
/**
* get the application journal
* @param string $fromDate
* @param string $toDate
* @param string $event
* @param string $accountId
* @param string $status
* @param string $term
* @param number $maxResults
* @return array
*/
public function getApplicationJournals ($fromDate="", $toDate="", $event="", $accountId="", $status="", $term="", $maxResults=50)
{
$ws_route = "get /audit/event/Search";
$url_params = [];
$query_params = [
"fromDate" => $fromDate,
"toDate" => $toDate,
"event" => $event,
"accountId" => $accountId,
"status" => $status,
"term" => $term,
"maxResults" => $maxResults,
];
$body_params = [];
list($headers, $body) = $this->query($ws_route, $url_params, $query_params, $body_params);
return $body;
}
}