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;
use ManiaControl\Files\AsynchronousFileReader;
use ManiaControl\Files\AsyncHttpRequest;
use ManiaControl\General\UsageInformationAble;
use ManiaControl\General\UsageInformationTrait;
use ManiaControl\ManiaControl;
@ -18,7 +18,7 @@ use ManiaControl\Maps\MapManager;
*/
class ManiaExchangeManager implements UsageInformationAble {
use UsageInformationTrait;
/*
* Constants
* @deprecated SEARCH Constants
@ -170,7 +170,9 @@ class ManiaExchangeManager implements UsageInformationAble {
$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) {
trigger_error("Error: '{$error}' for Url '{$url}'");
return;
@ -196,7 +198,9 @@ class ManiaExchangeManager implements UsageInformationAble {
}
$this->updateMapObjectsWithManiaExchangeIds($maps);
}, AsynchronousFileReader::CONTENT_TYPE_JSON);
});
$asyncHttpRequest->getData();
}
/**
@ -267,7 +271,9 @@ class ManiaExchangeManager implements UsageInformationAble {
$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;
if ($error) {
trigger_error($error);
@ -280,7 +286,9 @@ class ManiaExchangeManager implements UsageInformationAble {
}
}
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\TimerListener;
use ManiaControl\Commands\CommandListener;
use ManiaControl\Files\AsyncHttpRequest;
use ManiaControl\Files\FileUtil;
use ManiaControl\Logger;
use ManiaControl\ManiaControl;
@ -478,9 +479,14 @@ class PluginManager {
*/
public function fetchPluginList(callable $function) {
$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);
call_user_func($function, $data, $error);
});
$asyncHttpRequest->getData();
}
}

View File

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

View File

@ -6,6 +6,7 @@ use ManiaControl\Callbacks\CallbackListener;
use ManiaControl\Callbacks\CallbackManager;
use ManiaControl\Callbacks\TimerListener;
use ManiaControl\Commands\CommandListener;
use ManiaControl\Files\AsyncHttpRequest;
use ManiaControl\Files\BackupUtil;
use ManiaControl\Files\FileUtil;
use ManiaControl\Logger;
@ -240,7 +241,9 @@ class PluginUpdateManager implements CallbackListener, CommandListener, TimerLis
* @param bool $update
*/
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
) {
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));
$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) {
$message = "Error loading Plugin Install Data! {$error}";
$this->maniaControl->getChat()->sendError($message, $player);
@ -372,6 +380,8 @@ class PluginUpdateManager implements CallbackListener, CommandListener, TimerLis
$pluginUpdateData = new PluginUpdateData($data);
$this->installPlugin($pluginUpdateData, $player);
});
$asyncHttpRequest->getData();
}
}

View File

@ -6,6 +6,7 @@ use ManiaControl\Admin\AuthenticationManager;
use ManiaControl\Callbacks\CallbackListener;
use ManiaControl\Callbacks\TimerListener;
use ManiaControl\Commands\CommandListener;
use ManiaControl\Files\AsyncHttpRequest;
use ManiaControl\Files\BackupUtil;
use ManiaControl\Files\FileUtil;
use ManiaControl\Logger;
@ -143,7 +144,9 @@ class UpdateManager implements CallbackListener, CommandListener, TimerListener
$updateChannel = $this->getCurrentUpdateChannelSetting();
$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) {
Logger::logError('Error on UpdateCheck: ' . $error);
return;
@ -156,6 +159,8 @@ class UpdateManager implements CallbackListener, CommandListener, TimerListener
call_user_func($function, $updateData);
}
});
$asyncHttpRequest->getData();
}
/**
@ -284,8 +289,7 @@ class UpdateManager implements CallbackListener, CommandListener, TimerListener
// No update available
return;
}
if ($this->maniaControl->getPlayerManager()->getPlayerCount(false) > 0
) {
if ($this->maniaControl->getPlayerManager()->getPlayerCount(false) > 0) {
// Server not empty
return;
}
@ -331,7 +335,10 @@ class UpdateManager implements CallbackListener, CommandListener, TimerListener
}
$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
) {
if (!$updateFileContent || $error) {
@ -393,6 +400,7 @@ class UpdateManager implements CallbackListener, CommandListener, TimerListener
$this->maniaControl->restart();
});
$asyncHttpRequest->getData();
return true;
}
@ -419,8 +427,7 @@ class UpdateManager implements CallbackListener, CommandListener, TimerListener
return;
}
// Announce available update
if (!$this->maniaControl->getAuthenticationManager()->checkPermission($player, self::SETTING_PERMISSION_UPDATE)
) {
if (!$this->maniaControl->getAuthenticationManager()->checkPermission($player, self::SETTING_PERMISSION_UPDATE)) {
return;
}
@ -447,8 +454,7 @@ class UpdateManager implements CallbackListener, CommandListener, TimerListener
* @param 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);
return;
}
@ -492,8 +498,7 @@ class UpdateManager implements CallbackListener, CommandListener, TimerListener
* @param 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);
return;
}

View File

@ -252,7 +252,9 @@ class KarmaPlugin implements CallbackListener, TimerListener, Plugin {
$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;
if ($error) {
$this->maniaControl->getErrorHandler()->triggerDebugNotice('mx karma error: ' . $error);
@ -260,7 +262,7 @@ class KarmaPlugin implements CallbackListener, TimerListener, Plugin {
}
$data = json_decode($json);
if (!$data) {
$this->maniaControl->getErrorHandler()->triggerDebugNotice('auth error', $json, $data);
$this->maniaControl->getErrorHandler()->triggerDebugNotice('auth error' . $json);
return;
}
if ($data->success) {
@ -272,7 +274,9 @@ class KarmaPlugin implements CallbackListener, TimerListener, Plugin {
$this->maniaControl->getErrorHandler()->triggerDebugNotice('auth error', $data->data->message);
$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 .= '&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;
if ($error) {
$this->maniaControl->getErrorHandler()->triggerDebugNotice('mx karma error', $error);
$this->maniaControl->getErrorHandler()->triggerDebugNotice('mx karma error' . $error);
return;
}
$data = json_decode($json);
if (!$data) {
$this->maniaControl->getErrorHandler()->triggerDebugNotice('parse error', $json, $data);
$this->maniaControl->getErrorHandler()->triggerDebugNotice('parse error' . $json);
return;
}
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);
$this->maniaControl->getChat()->sendErrorToAdmins("Invalid Mania-Exchange Karma code in Karma Plugin specified!", $permission);
} 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);
unset($this->mxKarma['session']);
}
}, AsynchronousFileReader::CONTENT_TYPE_JSON, 1000);
});
$asyncHttpRequest->getData(1000);
}
/**