initial
This commit is contained in:
44
classes/APIgoatDoc.php
Normal file
44
classes/APIgoatDoc.php
Normal file
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
|
||||
class APIgoatDoc extends APIgoatTemplate
|
||||
{
|
||||
|
||||
static function getDocs(array $data, array $headers)
|
||||
{
|
||||
$content = '';
|
||||
$menu = '';
|
||||
|
||||
//preprint($data);
|
||||
|
||||
foreach ($data as $row) {
|
||||
|
||||
$menu .= li(href($row['name'], '#' . $row['title']), "class='page_item current_page_item'");
|
||||
$content .= div(
|
||||
anchor($row['title']) . h3($row['name'])
|
||||
. h2($row['title'], "class='entry-title'")
|
||||
. p($row['type'] . " " . $row['value'])
|
||||
. ((!empty($row['example'])) ? pre(htmlentities($row['example'])) : '')
|
||||
. p(trim($row['text']))
|
||||
);
|
||||
}
|
||||
|
||||
foreach ($headers as $name => $header) {
|
||||
$menu = h3($header) . $menu;
|
||||
}
|
||||
|
||||
|
||||
return div(
|
||||
div(
|
||||
ul($menu, "class='doc-nav-list'"),
|
||||
'',
|
||||
"class='wedocs-sidebar wedocs-hide-mobile'"
|
||||
) . div(
|
||||
$content,
|
||||
'',
|
||||
"class='wedocs-single-content'"
|
||||
),
|
||||
'',
|
||||
"class='wedocs-single-wrap'"
|
||||
);
|
||||
}
|
||||
}
|
||||
104
classes/APIgoatFetchAPI.php
Normal file
104
classes/APIgoatFetchAPI.php
Normal file
@@ -0,0 +1,104 @@
|
||||
<?php
|
||||
|
||||
use BernardoSilva\JWTAPIClient\APIClient;
|
||||
use BernardoSilva\JWTAPIClient\AccessTokenCredentials;
|
||||
use \Firebase\JWT\JWT;
|
||||
|
||||
include_once plugin_dir_path(dirname(__FILE__)) . 'includes/html_helper.php';
|
||||
|
||||
class APIgoatFetchAPI
|
||||
{
|
||||
|
||||
private $username = 'wp-behavior@apigoat.com';
|
||||
private $password = '0sKtegdSSk';
|
||||
private $baseURI = 'https://goat.local/p/goatcheese/api/v1/';
|
||||
private $jwt_pubkey = '9sKjdjuue8sSjwh6';
|
||||
private $jwt_alg = ['HS256'];
|
||||
private $client;
|
||||
private $credentials;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->client = new APIClient($this->baseURI);
|
||||
$this->clientOptions = [
|
||||
'verify' => false,
|
||||
'content-Type' => 'application/json',
|
||||
'accept' => 'application/json'
|
||||
];
|
||||
|
||||
if (!$this->authenticationValid()) {
|
||||
$this->authenticate();
|
||||
$this->saveCredentials();
|
||||
$this->client = new APIClient($this->baseURI, $this->credentials);
|
||||
} else {
|
||||
$this->credentials = new AccessTokenCredentials($_SESSION['APIgoat']['API_jwt_token']);
|
||||
$this->client->setCredentials($this->credentials);
|
||||
}
|
||||
}
|
||||
|
||||
public function fetchBehaviors()
|
||||
{
|
||||
$clientOptions = $this->clientOptions;
|
||||
$clientOptions['query'] = [
|
||||
"Query" => [
|
||||
"select" => [
|
||||
["behavior.name", "name"], ["code", "title"], ["description", "text"], "value", "example", "type", ["behavior_category.name", "category_name"]
|
||||
],
|
||||
"filter" => [
|
||||
"behavior" => [
|
||||
0 => ["group", "Free"],
|
||||
1 => ["status", "Active"]
|
||||
]
|
||||
],
|
||||
"join" => ["behavior_category"],
|
||||
"limit" => 20
|
||||
],
|
||||
"debug" => true
|
||||
];
|
||||
|
||||
|
||||
$response = $this->client->get('Behavior', $clientOptions);
|
||||
|
||||
$body = json_decode($response->getBody()->getContents(), true);
|
||||
if ($response->getStatusCode() == 200) {
|
||||
return $body;
|
||||
} else {
|
||||
$body = json_decode($response->getBody(), true);
|
||||
return $body;
|
||||
}
|
||||
}
|
||||
|
||||
private function saveCredentials()
|
||||
{
|
||||
if (!empty($this->jwt_pubkey)) {
|
||||
$decoded = JWT::decode($this->credentials->getAccessToken(), $this->jwt_pubkey, $this->jwt_alg);
|
||||
$_SESSION['APIgoat']['API_jwt_expire'] = $decoded->exp;
|
||||
$_SESSION['APIgoat']['API_jwt_token'] = $this->credentials->getAccessToken();
|
||||
}
|
||||
}
|
||||
|
||||
private function authenticate()
|
||||
{
|
||||
$options = [
|
||||
'verify' => false, // might need this if API uses self signed certificate
|
||||
'form_params' => [
|
||||
'u' => $this->username,
|
||||
'pw' => md5($this->password)
|
||||
],
|
||||
'debug' => false
|
||||
];
|
||||
// authenticate on API to get token
|
||||
$response = $this->client->post('Authy/auth', $options);
|
||||
$loginResponseDecoded = json_decode($response->getBody()->getContents(), true);
|
||||
|
||||
$this->credentials = new AccessTokenCredentials($loginResponseDecoded['token']);
|
||||
}
|
||||
|
||||
private function authenticationValid()
|
||||
{
|
||||
if (isset($_SESSION['APIgoat']) && !empty($_SESSION['APIgoat']['API_jwt_token']) && $_SESSION['APIgoat']['API_jwt_expire'] > time()) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
25
classes/APIgoatList.php
Normal file
25
classes/APIgoatList.php
Normal file
@@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
class APIgoatList
|
||||
{
|
||||
|
||||
static function init($atts, $content = null)
|
||||
{
|
||||
$APIgoatFetchAPI = new APIgoatFetchAPI();
|
||||
$Behaviors = $APIgoatFetchAPI->fetchBehaviors();
|
||||
/*if (isset($Behaviors['debug'])) {
|
||||
echo "<br>" . preprint($Behaviors['debug']) . "<br>";
|
||||
}
|
||||
|
||||
if (isset($Behaviors['messages'])) {
|
||||
echo "<br>" . preprint($Behaviors['messages']) . "<br>";
|
||||
}*/
|
||||
if ($Behaviors['data']) {
|
||||
$table = APIgoatDoc::getDocs($Behaviors['data'], ['Code' => 'Parameters']);
|
||||
|
||||
return $content . div($table, '', "class='site-main'");
|
||||
} else {
|
||||
return $content . "<br>Error" . preprint($Behaviors);
|
||||
}
|
||||
}
|
||||
}
|
||||
44
classes/APIgoatTemplate.php
Normal file
44
classes/APIgoatTemplate.php
Normal file
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
|
||||
include_once plugin_dir_path(dirname(__FILE__)) . 'includes/html_helper.php';
|
||||
|
||||
class APIgoatTemplate
|
||||
{
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
}
|
||||
|
||||
public function getTable(array $data, array $headers = null, array $classes = null)
|
||||
{
|
||||
$rows = '';
|
||||
$cols = '';
|
||||
$header = '';
|
||||
$th = '';
|
||||
|
||||
foreach ($data as $row) {
|
||||
if (is_array($row)) {
|
||||
$header = '';
|
||||
foreach ($row as $name => $field) {
|
||||
if ($headers == null || in_array($name, $headers)) {
|
||||
$cols .= td($field);
|
||||
}
|
||||
}
|
||||
$rows .= tr($cols);
|
||||
$cols = '';
|
||||
} else {
|
||||
$rows .= tr(td($row));
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($headers as $name => $header) {
|
||||
$th .= th($header);
|
||||
}
|
||||
|
||||
|
||||
return table(
|
||||
thead($th) . tbody($rows),
|
||||
"class='" . $classes['table'] . "'"
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user