updated async http requests

This commit is contained in:
kremsy 2017-03-30 20:27:57 +02:00
parent 1369aab401
commit 389ff7925c
6 changed files with 65 additions and 27 deletions

View File

@ -2,7 +2,7 @@
namespace ManiaControl\ManiaExchange; namespace ManiaControl\ManiaExchange;
use ManiaControl\Files\AsynchronousFileReader; use ManiaControl\Files\AsyncHttpRequest;
use ManiaControl\General\UsageInformationAble; use ManiaControl\General\UsageInformationAble;
use ManiaControl\General\UsageInformationTrait; use ManiaControl\General\UsageInformationTrait;
use ManiaControl\ManiaControl; use ManiaControl\ManiaControl;
@ -170,7 +170,9 @@ class ManiaExchangeManager implements UsageInformationAble {
$url .= "&key=" . $key; $url .= "&key=" . $key;
} }
$this->maniaControl->getFileReader()->loadFile($url, function ($mapInfo, $error) use ($titlePrefix, $url) { $asyncHttpRequest = new AsyncHttpRequest($this->maniaControl, $url);
$asyncHttpRequest->setContentType(AsyncHttpRequest::CONTENT_TYPE_JSON);
$asyncHttpRequest->setCallable(function ($mapInfo, $error) use ($titlePrefix, $url) {
if ($error) { if ($error) {
trigger_error("Error: '{$error}' for Url '{$url}'"); trigger_error("Error: '{$error}' for Url '{$url}'");
return; return;
@ -196,7 +198,9 @@ class ManiaExchangeManager implements UsageInformationAble {
} }
$this->updateMapObjectsWithManiaExchangeIds($maps); $this->updateMapObjectsWithManiaExchangeIds($maps);
}, AsynchronousFileReader::CONTENT_TYPE_JSON); });
$asyncHttpRequest->getData();
} }
/** /**
@ -267,7 +271,9 @@ class ManiaExchangeManager implements UsageInformationAble {
$url .= "&key=" . $key; $url .= "&key=" . $key;
} }
$this->maniaControl->getFileReader()->loadFile($url, function ($mapInfo, $error) use (&$function, $titlePrefix, $url) { $asyncHttpRequest = new AsyncHttpRequest($this->maniaControl, $url);
$asyncHttpRequest->setContentType(AsyncHttpRequest::CONTENT_TYPE_JSON);
$asyncHttpRequest->setCallable(function ($mapInfo, $error) use (&$function, $titlePrefix, $url) {
$mxMapInfo = null; $mxMapInfo = null;
if ($error) { if ($error) {
trigger_error($error); trigger_error($error);
@ -280,7 +286,9 @@ class ManiaExchangeManager implements UsageInformationAble {
} }
} }
call_user_func($function, $mxMapInfo); call_user_func($function, $mxMapInfo);
}, AsynchronousFileReader::CONTENT_TYPE_JSON); });
$asyncHttpRequest->getData();
} }
/** /**

View File

@ -6,6 +6,7 @@ use ManiaControl\Callbacks\CallbackListener;
use ManiaControl\Callbacks\EchoListener; use ManiaControl\Callbacks\EchoListener;
use ManiaControl\Callbacks\TimerListener; use ManiaControl\Callbacks\TimerListener;
use ManiaControl\Commands\CommandListener; use ManiaControl\Commands\CommandListener;
use ManiaControl\Files\AsyncHttpRequest;
use ManiaControl\Files\FileUtil; use ManiaControl\Files\FileUtil;
use ManiaControl\Logger; use ManiaControl\Logger;
use ManiaControl\ManiaControl; use ManiaControl\ManiaControl;
@ -478,9 +479,14 @@ class PluginManager {
*/ */
public function fetchPluginList(callable $function) { public function fetchPluginList(callable $function) {
$url = ManiaControl::URL_WEBSERVICE . 'plugins'; $url = ManiaControl::URL_WEBSERVICE . 'plugins';
$this->maniaControl->getFileReader()->loadFile($url, function ($dataJson, $error) use (&$function) {
$asyncHttpRequest = new AsyncHttpRequest($this->maniaControl, $url);
$asyncHttpRequest->setContentType(AsyncHttpRequest::CONTENT_TYPE_JSON);
$asyncHttpRequest->setCallable(function ($dataJson, $error) use (&$function) {
$data = json_decode($dataJson); $data = json_decode($dataJson);
call_user_func($function, $data, $error); call_user_func($function, $data, $error);
}); });
$asyncHttpRequest->getData();
} }
} }

View File

@ -84,6 +84,7 @@ class UsageReporter implements TimerListener {
$url = ManiaControl::URL_WEBSERVICE . 'usagereport'; $url = ManiaControl::URL_WEBSERVICE . 'usagereport';
$asyncRequest = new AsyncHttpRequest($this->maniaControl, $url); $asyncRequest = new AsyncHttpRequest($this->maniaControl, $url);
$asyncRequest->setContentType(AsyncHttpRequest::CONTENT_TYPE_JSON);
$asyncRequest->setContent($usageReport); $asyncRequest->setContent($usageReport);
$asyncRequest->setCallable(function ($response, $error) { $asyncRequest->setCallable(function ($response, $error) {
$response = json_decode($response); $response = json_decode($response);

View File

@ -6,6 +6,7 @@ use ManiaControl\Callbacks\CallbackListener;
use ManiaControl\Callbacks\CallbackManager; use ManiaControl\Callbacks\CallbackManager;
use ManiaControl\Callbacks\TimerListener; use ManiaControl\Callbacks\TimerListener;
use ManiaControl\Commands\CommandListener; use ManiaControl\Commands\CommandListener;
use ManiaControl\Files\AsyncHttpRequest;
use ManiaControl\Files\BackupUtil; use ManiaControl\Files\BackupUtil;
use ManiaControl\Files\FileUtil; use ManiaControl\Files\FileUtil;
use ManiaControl\Logger; use ManiaControl\Logger;
@ -240,7 +241,9 @@ class PluginUpdateManager implements CallbackListener, CommandListener, TimerLis
* @param bool $update * @param bool $update
*/ */
private function installPlugin(PluginUpdateData $pluginUpdateData, Player $player = null, $update = false) { private function installPlugin(PluginUpdateData $pluginUpdateData, Player $player = null, $update = false) {
$this->maniaControl->getFileReader()->loadFile($pluginUpdateData->url, function ($updateFileContent, $error) use ( $asyncHttpRequest = new AsyncHttpRequest($this->maniaControl, $pluginUpdateData->url);
$asyncHttpRequest->setContentType(AsyncHttpRequest::CONTENT_TYPE_JSON);
$asyncHttpRequest->setCallable(function ($updateFileContent, $error) use (
&$pluginUpdateData, &$player, &$update &$pluginUpdateData, &$player, &$update
) { ) {
if (!$updateFileContent || $error) { if (!$updateFileContent || $error) {
@ -320,6 +323,8 @@ class PluginUpdateManager implements CallbackListener, CommandListener, TimerLis
} }
} }
}); });
$asyncHttpRequest->getData();
} }
/** /**
@ -355,7 +360,10 @@ class PluginUpdateManager implements CallbackListener, CommandListener, TimerLis
$pluginId = substr($actionId, strlen(InstallMenu::ACTION_PREFIX_INSTALL_PLUGIN)); $pluginId = substr($actionId, strlen(InstallMenu::ACTION_PREFIX_INSTALL_PLUGIN));
$url = ManiaControl::URL_WEBSERVICE . 'plugins/' . $pluginId; $url = ManiaControl::URL_WEBSERVICE . 'plugins/' . $pluginId;
$this->maniaControl->getFileReader()->loadFile($url, function ($data, $error) use (&$player) {
$asyncHttpRequest = new AsyncHttpRequest($this->maniaControl, $url);
$asyncHttpRequest->setContentType(AsyncHttpRequest::CONTENT_TYPE_JSON);
$asyncHttpRequest->setCallable(function ($data, $error) use (&$player) {
if ($error || !$data) { if ($error || !$data) {
$message = "Error loading Plugin Install Data! {$error}"; $message = "Error loading Plugin Install Data! {$error}";
$this->maniaControl->getChat()->sendError($message, $player); $this->maniaControl->getChat()->sendError($message, $player);
@ -372,6 +380,8 @@ class PluginUpdateManager implements CallbackListener, CommandListener, TimerLis
$pluginUpdateData = new PluginUpdateData($data); $pluginUpdateData = new PluginUpdateData($data);
$this->installPlugin($pluginUpdateData, $player); $this->installPlugin($pluginUpdateData, $player);
}); });
$asyncHttpRequest->getData();
} }
} }

View File

@ -6,6 +6,7 @@ use ManiaControl\Admin\AuthenticationManager;
use ManiaControl\Callbacks\CallbackListener; use ManiaControl\Callbacks\CallbackListener;
use ManiaControl\Callbacks\TimerListener; use ManiaControl\Callbacks\TimerListener;
use ManiaControl\Commands\CommandListener; use ManiaControl\Commands\CommandListener;
use ManiaControl\Files\AsyncHttpRequest;
use ManiaControl\Files\BackupUtil; use ManiaControl\Files\BackupUtil;
use ManiaControl\Files\FileUtil; use ManiaControl\Files\FileUtil;
use ManiaControl\Logger; use ManiaControl\Logger;
@ -143,7 +144,9 @@ class UpdateManager implements CallbackListener, CommandListener, TimerListener
$updateChannel = $this->getCurrentUpdateChannelSetting(); $updateChannel = $this->getCurrentUpdateChannelSetting();
$url = ManiaControl::URL_WEBSERVICE . 'versions?current=1&channel=' . $updateChannel; $url = ManiaControl::URL_WEBSERVICE . 'versions?current=1&channel=' . $updateChannel;
$this->maniaControl->getFileReader()->loadFile($url, function ($dataJson, $error) use (&$function) { $asyncHttpRequest = new AsyncHttpRequest($this->maniaControl, $url);
$asyncHttpRequest->setContentType(AsyncHttpRequest::CONTENT_TYPE_JSON);
$asyncHttpRequest->setCallable(function ($dataJson, $error) use (&$function) {
if ($error) { if ($error) {
Logger::logError('Error on UpdateCheck: ' . $error); Logger::logError('Error on UpdateCheck: ' . $error);
return; return;
@ -156,6 +159,8 @@ class UpdateManager implements CallbackListener, CommandListener, TimerListener
call_user_func($function, $updateData); call_user_func($function, $updateData);
} }
}); });
$asyncHttpRequest->getData();
} }
/** /**
@ -284,8 +289,7 @@ class UpdateManager implements CallbackListener, CommandListener, TimerListener
// No update available // No update available
return; return;
} }
if ($this->maniaControl->getPlayerManager()->getPlayerCount(false) > 0 if ($this->maniaControl->getPlayerManager()->getPlayerCount(false) > 0) {
) {
// Server not empty // Server not empty
return; return;
} }
@ -331,7 +335,10 @@ class UpdateManager implements CallbackListener, CommandListener, TimerListener
} }
$updateData = $this->coreUpdateData; $updateData = $this->coreUpdateData;
$this->maniaControl->getFileReader()->loadFile($updateData->url, function ($updateFileContent, $error) use (
$asyncHttpRequest = new AsyncHttpRequest($this->maniaControl, $updateData->url);
$asyncHttpRequest->setContentType(AsyncHttpRequest::CONTENT_TYPE_JSON);
$asyncHttpRequest->setCallable(function ($updateFileContent, $error) use (
$updateData, &$player $updateData, &$player
) { ) {
if (!$updateFileContent || $error) { if (!$updateFileContent || $error) {
@ -393,6 +400,7 @@ class UpdateManager implements CallbackListener, CommandListener, TimerListener
$this->maniaControl->restart(); $this->maniaControl->restart();
}); });
$asyncHttpRequest->getData();
return true; return true;
} }
@ -419,8 +427,7 @@ class UpdateManager implements CallbackListener, CommandListener, TimerListener
return; return;
} }
// Announce available update // Announce available update
if (!$this->maniaControl->getAuthenticationManager()->checkPermission($player, self::SETTING_PERMISSION_UPDATE) if (!$this->maniaControl->getAuthenticationManager()->checkPermission($player, self::SETTING_PERMISSION_UPDATE)) {
) {
return; return;
} }
@ -447,8 +454,7 @@ class UpdateManager implements CallbackListener, CommandListener, TimerListener
* @param Player $player * @param Player $player
*/ */
public function handle_CheckUpdate(array $chatCallback, Player $player) { public function handle_CheckUpdate(array $chatCallback, Player $player) {
if (!$this->maniaControl->getAuthenticationManager()->checkPermission($player, self::SETTING_PERMISSION_UPDATECHECK) if (!$this->maniaControl->getAuthenticationManager()->checkPermission($player, self::SETTING_PERMISSION_UPDATECHECK)) {
) {
$this->maniaControl->getAuthenticationManager()->sendNotAllowed($player); $this->maniaControl->getAuthenticationManager()->sendNotAllowed($player);
return; return;
} }
@ -492,8 +498,7 @@ class UpdateManager implements CallbackListener, CommandListener, TimerListener
* @param Player $player * @param Player $player
*/ */
public function handle_CoreUpdate(array $chatCallback, Player $player) { public function handle_CoreUpdate(array $chatCallback, Player $player) {
if (!$this->maniaControl->getAuthenticationManager()->checkPermission($player, self::SETTING_PERMISSION_UPDATE) if (!$this->maniaControl->getAuthenticationManager()->checkPermission($player, self::SETTING_PERMISSION_UPDATE)) {
) {
$this->maniaControl->getAuthenticationManager()->sendNotAllowed($player); $this->maniaControl->getAuthenticationManager()->sendNotAllowed($player);
return; return;
} }

View File

@ -252,7 +252,9 @@ class KarmaPlugin implements CallbackListener, TimerListener, Plugin {
$this->mxKarma['connectionInProgress'] = true; $this->mxKarma['connectionInProgress'] = true;
$this->maniaControl->getFileReader()->loadFile($query, function ($json, $error) use ($mxKarmaCode) { $asyncHttpRequest = new AsyncHttpRequest($this->maniaControl, $query);
$asyncHttpRequest->setContentType(AsyncHttpRequest::CONTENT_TYPE_JSON);
$asyncHttpRequest->setCallable(function ($json, $error) use ($mxKarmaCode) {
$this->mxKarma['connectionInProgress'] = false; $this->mxKarma['connectionInProgress'] = false;
if ($error) { if ($error) {
$this->maniaControl->getErrorHandler()->triggerDebugNotice('mx karma error: ' . $error); $this->maniaControl->getErrorHandler()->triggerDebugNotice('mx karma error: ' . $error);
@ -260,7 +262,7 @@ class KarmaPlugin implements CallbackListener, TimerListener, Plugin {
} }
$data = json_decode($json); $data = json_decode($json);
if (!$data) { if (!$data) {
$this->maniaControl->getErrorHandler()->triggerDebugNotice('auth error', $json, $data); $this->maniaControl->getErrorHandler()->triggerDebugNotice('auth error' . $json);
return; return;
} }
if ($data->success) { if ($data->success) {
@ -272,7 +274,9 @@ class KarmaPlugin implements CallbackListener, TimerListener, Plugin {
$this->maniaControl->getErrorHandler()->triggerDebugNotice('auth error', $data->data->message); $this->maniaControl->getErrorHandler()->triggerDebugNotice('auth error', $data->data->message);
$this->mxKarma['connectionInProgress'] = false; $this->mxKarma['connectionInProgress'] = false;
} }
}, AsynchronousFileReader::CONTENT_TYPE_JSON, 1000); });
$asyncHttpRequest->getData(1000);
} }
/** /**
@ -287,15 +291,17 @@ class KarmaPlugin implements CallbackListener, TimerListener, Plugin {
$query .= '?sessionKey=' . urlencode($this->mxKarma['session']->sessionKey); $query .= '?sessionKey=' . urlencode($this->mxKarma['session']->sessionKey);
$query .= '&activationHash=' . urlencode($hash); $query .= '&activationHash=' . urlencode($hash);
$this->maniaControl->getFileReader()->loadFile($query, function ($json, $error) use ($query) { $asyncHttpRequest = new AsyncHttpRequest($this->maniaControl, $query);
$asyncHttpRequest->setContentType(AsyncHttpRequest::CONTENT_TYPE_JSON);
$asyncHttpRequest->setCallable(function ($json, $error) use ($query) {
$this->mxKarma['connectionInProgress'] = false; $this->mxKarma['connectionInProgress'] = false;
if ($error) { if ($error) {
$this->maniaControl->getErrorHandler()->triggerDebugNotice('mx karma error', $error); $this->maniaControl->getErrorHandler()->triggerDebugNotice('mx karma error' . $error);
return; return;
} }
$data = json_decode($json); $data = json_decode($json);
if (!$data) { if (!$data) {
$this->maniaControl->getErrorHandler()->triggerDebugNotice('parse error', $json, $data); $this->maniaControl->getErrorHandler()->triggerDebugNotice('parse error' . $json);
return; return;
} }
if ($data->success && $data->data->activated) { if ($data->success && $data->data->activated) {
@ -308,12 +314,14 @@ class KarmaPlugin implements CallbackListener, TimerListener, Plugin {
$permission = $this->maniaControl->getSettingManager()->getSettingValue($this->maniaControl->getAuthenticationManager(), PluginMenu::SETTING_PERMISSION_CHANGE_PLUGIN_SETTINGS); $permission = $this->maniaControl->getSettingManager()->getSettingValue($this->maniaControl->getAuthenticationManager(), PluginMenu::SETTING_PERMISSION_CHANGE_PLUGIN_SETTINGS);
$this->maniaControl->getChat()->sendErrorToAdmins("Invalid Mania-Exchange Karma code in Karma Plugin specified!", $permission); $this->maniaControl->getChat()->sendErrorToAdmins("Invalid Mania-Exchange Karma code in Karma Plugin specified!", $permission);
} else { } else {
$this->maniaControl->getErrorHandler()->triggerDebugNotice('auth error', $data->data->message, $query); $this->maniaControl->getErrorHandler()->triggerDebugNotice('auth error' . $data->data->message . $query);
} }
Logger::logError("Error while activating Mania-Exchange Karma Session: " . $data->data->message); Logger::logError("Error while activating Mania-Exchange Karma Session: " . $data->data->message);
unset($this->mxKarma['session']); unset($this->mxKarma['session']);
} }
}, AsynchronousFileReader::CONTENT_TYPE_JSON, 1000); });
$asyncHttpRequest->getData(1000);
} }
/** /**