chg: [brood] Centralized methods to issue requests
parent
1be4978f28
commit
63928e756a
|
@ -29,18 +29,40 @@ class BroodsTable extends AppTable
|
||||||
return $validator;
|
return $validator;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function genHTTPClient(Object $brood, array $options=[]): Object
|
||||||
|
{
|
||||||
|
$defaultOptions = [
|
||||||
|
'headers' => [
|
||||||
|
'Authorization' => $brood->authkey,
|
||||||
|
],
|
||||||
|
];
|
||||||
|
if (empty($options['type'])) {
|
||||||
|
$options['type'] = 'json';
|
||||||
|
}
|
||||||
|
$options = array_merge($defaultOptions, $options);
|
||||||
|
$http = new Client($options);
|
||||||
|
return $http;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function HTTPClientGET(String $relativeURL, Object $brood, array $data=[], array $options=[]): Object
|
||||||
|
{
|
||||||
|
$http = $this->genHTTPClient($brood, $options);
|
||||||
|
$url = sprintf('%s%s', $brood->url, $relativeURL);
|
||||||
|
return $http->get($url, $data, $options);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function HTTPClientPOST(String $relativeURL, Object $brood, $data, array $options=[]): Object
|
||||||
|
{
|
||||||
|
$http = $this->genHTTPClient($brood, $options);
|
||||||
|
$url = sprintf('%s%s', $brood->url, $relativeURL);
|
||||||
|
return $http->post($url, $data, $options);
|
||||||
|
}
|
||||||
|
|
||||||
public function queryStatus($id)
|
public function queryStatus($id)
|
||||||
{
|
{
|
||||||
$brood = $this->find()->where(['id' => $id])->first();
|
$brood = $this->find()->where(['id' => $id])->first();
|
||||||
$http = new Client();
|
|
||||||
$start = microtime(true);
|
$start = microtime(true);
|
||||||
$response = $http->get($brood['url'] . '/instance/status.json', [], [
|
$response = $this->HTTPClientGET('/instance/status.json', $brood);
|
||||||
'headers' => [
|
|
||||||
'Authorization' => $brood['authkey'],
|
|
||||||
'Accept' => 'Application/json',
|
|
||||||
'Content-type' => 'Application/json'
|
|
||||||
]
|
|
||||||
]);
|
|
||||||
$ping = ((int)(100 * (microtime(true) - $start)));
|
$ping = ((int)(100 * (microtime(true) - $start)));
|
||||||
$errors = [
|
$errors = [
|
||||||
403 => [
|
403 => [
|
||||||
|
@ -84,15 +106,8 @@ class BroodsTable extends AppTable
|
||||||
if (empty($brood)) {
|
if (empty($brood)) {
|
||||||
throw new NotFoundException(__('Brood not found'));
|
throw new NotFoundException(__('Brood not found'));
|
||||||
}
|
}
|
||||||
$http = new Client();
|
|
||||||
$filterQuery = empty($filter) ? '' : '?quickFilter=' . urlencode($filter);
|
$filterQuery = empty($filter) ? '' : '?quickFilter=' . urlencode($filter);
|
||||||
$response = $http->get($brood['url'] . '/' . $scope . '/index.json' . $filterQuery , [], [
|
$response = $this->HTTPClientGET(sprintf('/%s/index.json%s', $scope, $filterQuery), $brood);
|
||||||
'headers' => [
|
|
||||||
'Authorization' => $brood['authkey'],
|
|
||||||
'Accept' => 'Application/json',
|
|
||||||
'Content-type' => 'Application/json'
|
|
||||||
]
|
|
||||||
]);
|
|
||||||
if ($response->isOk()) {
|
if ($response->isOk()) {
|
||||||
return $response->getJson();
|
return $response->getJson();
|
||||||
} else {
|
} else {
|
||||||
|
@ -107,14 +122,7 @@ class BroodsTable extends AppTable
|
||||||
if (empty($brood)) {
|
if (empty($brood)) {
|
||||||
throw new NotFoundException(__('Brood not found'));
|
throw new NotFoundException(__('Brood not found'));
|
||||||
}
|
}
|
||||||
$http = new Client();
|
$response = $this->HTTPClientGET(sprintf('/%s/view/%s/index.json', $scope, $org_id), $brood);
|
||||||
$response = $http->get($brood['url'] . '/' . $scope . '/view/' . $org_id . '/index.json' , [], [
|
|
||||||
'headers' => [
|
|
||||||
'Authorization' => $brood['authkey'],
|
|
||||||
'Accept' => 'Application/json',
|
|
||||||
'Content-type' => 'Application/json'
|
|
||||||
]
|
|
||||||
]);
|
|
||||||
if ($response->isOk()) {
|
if ($response->isOk()) {
|
||||||
$org = $response->getJson();
|
$org = $response->getJson();
|
||||||
$this->Organisation = TableRegistry::getTableLocator()->get('Organisations');
|
$this->Organisation = TableRegistry::getTableLocator()->get('Organisations');
|
||||||
|
@ -132,14 +140,7 @@ class BroodsTable extends AppTable
|
||||||
if (empty($brood)) {
|
if (empty($brood)) {
|
||||||
throw new NotFoundException(__('Brood not found'));
|
throw new NotFoundException(__('Brood not found'));
|
||||||
}
|
}
|
||||||
$http = new Client();
|
$response = $this->HTTPClientGET(sprintf('/organisations/view/%s/index.json', $org_id), $brood);
|
||||||
$response = $http->get($brood['url'] . '/organisations/view/' . $org_id . '/index.json' , [], [
|
|
||||||
'headers' => [
|
|
||||||
'Authorization' => $brood['authkey'],
|
|
||||||
'Accept' => 'Application/json',
|
|
||||||
'Content-type' => 'Application/json'
|
|
||||||
]
|
|
||||||
]);
|
|
||||||
if ($response->isOk()) {
|
if ($response->isOk()) {
|
||||||
$org = $response->getJson();
|
$org = $response->getJson();
|
||||||
$this->Organisation = TableRegistry::getTableLocator()->get('Organisations');
|
$this->Organisation = TableRegistry::getTableLocator()->get('Organisations');
|
||||||
|
@ -157,14 +158,7 @@ class BroodsTable extends AppTable
|
||||||
if (empty($brood)) {
|
if (empty($brood)) {
|
||||||
throw new NotFoundException(__('Brood not found'));
|
throw new NotFoundException(__('Brood not found'));
|
||||||
}
|
}
|
||||||
$http = new Client();
|
$response = $this->HTTPClientGET(sprintf('/individuals/view/%s/index.json', $individual_id), $brood);
|
||||||
$response = $http->get($brood['url'] . '/individuals/view/' . $individual_id . '/index.json' , [], [
|
|
||||||
'headers' => [
|
|
||||||
'Authorization' => $brood['authkey'],
|
|
||||||
'Accept' => 'Application/json',
|
|
||||||
'Content-type' => 'Application/json'
|
|
||||||
]
|
|
||||||
]);
|
|
||||||
if ($response->isOk()) {
|
if ($response->isOk()) {
|
||||||
$org = $response->getJson();
|
$org = $response->getJson();
|
||||||
$this->Individual = TableRegistry::getTableLocator()->get('Individual');
|
$this->Individual = TableRegistry::getTableLocator()->get('Individual');
|
||||||
|
@ -182,13 +176,7 @@ class BroodsTable extends AppTable
|
||||||
if (empty($brood)) {
|
if (empty($brood)) {
|
||||||
throw new NotFoundException(__('Brood not found'));
|
throw new NotFoundException(__('Brood not found'));
|
||||||
}
|
}
|
||||||
$http = new Client();
|
$response = $this->HTTPClientGET('/localTools/exposedTools', $brood);
|
||||||
$response = $http->get($brood['url'] . '/localTools/exposedTools' , [], [
|
|
||||||
'headers' => [
|
|
||||||
'Authorization' => $brood['authkey']
|
|
||||||
],
|
|
||||||
'type' => 'json'
|
|
||||||
]);
|
|
||||||
if ($response->isOk()) {
|
if ($response->isOk()) {
|
||||||
return $response->getJson();
|
return $response->getJson();
|
||||||
} else {
|
} else {
|
||||||
|
@ -198,19 +186,10 @@ class BroodsTable extends AppTable
|
||||||
|
|
||||||
public function sendRequest($brood, $urlPath, $methodPost = true, $data = []): Response
|
public function sendRequest($brood, $urlPath, $methodPost = true, $data = []): Response
|
||||||
{
|
{
|
||||||
$http = new Client();
|
|
||||||
$config = [
|
|
||||||
'headers' => [
|
|
||||||
'AUTHORIZATION' => $brood->authkey,
|
|
||||||
'Accept' => 'application/json'
|
|
||||||
],
|
|
||||||
'type' => 'json'
|
|
||||||
];
|
|
||||||
$url = $brood->url . $urlPath;
|
|
||||||
if ($methodPost) {
|
if ($methodPost) {
|
||||||
$response = $http->post($url, json_encode($data), $config);
|
$response = $this->HTTPClientPOST($urlPath, $brood, json_encode($data));
|
||||||
} else {
|
} else {
|
||||||
$response = $http->get($brood->url, $data, $config);
|
$response = $this->HTTPClientGET($urlPath, $brood, $data);
|
||||||
}
|
}
|
||||||
return $response;
|
return $response;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue