Skip to content

Commit

Permalink
update to support header for api key, cleanup fetcher implementation,…
Browse files Browse the repository at this point in the history
… rename some deprecated endpoints
  • Loading branch information
Plancke committed Mar 1, 2021
1 parent 37007dc commit 0f4af99
Show file tree
Hide file tree
Showing 23 changed files with 377 additions and 372 deletions.
156 changes: 111 additions & 45 deletions src/HypixelPHP.php

Large diffs are not rendered by default.

27 changes: 7 additions & 20 deletions src/cache/CacheHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,17 @@
use Plancke\HypixelPHP\classes\HypixelObject;
use Plancke\HypixelPHP\classes\Module;
use Plancke\HypixelPHP\responses\booster\Boosters;
use Plancke\HypixelPHP\responses\counts\Counts;
use Plancke\HypixelPHP\responses\friend\Friends;
use Plancke\HypixelPHP\responses\gameCounts\GameCounts;
use Plancke\HypixelPHP\responses\guild\Guild;
use Plancke\HypixelPHP\responses\KeyInfo;
use Plancke\HypixelPHP\responses\Leaderboards;
use Plancke\HypixelPHP\responses\player\Player;
use Plancke\HypixelPHP\responses\PlayerCount;
use Plancke\HypixelPHP\responses\PunishmentStats;
use Plancke\HypixelPHP\responses\RecentGames;
use Plancke\HypixelPHP\responses\Resource;
use Plancke\HypixelPHP\responses\skyblock\SkyBlockProfile;
use Plancke\HypixelPHP\responses\Status;
use Plancke\HypixelPHP\responses\PunishmentStats;

/**
* Class CacheHandler
Expand All @@ -40,13 +39,12 @@ abstract class CacheHandler extends Module {
CacheTimes::GUILD_NOT_FOUND => 10 * 60,

CacheTimes::LEADERBOARDS => 10 * 60,
CacheTimes::PLAYER_COUNT => 10 * 60,
CacheTimes::BOOSTERS => 10 * 60,
CacheTimes::STATUS => 10 * 60,
CacheTimes::KEY_INFO => 10 * 60,
CacheTimes::FRIENDS => 10 * 60,
CacheTimes::PUNISHMENT_STATS => 10 * 60,
CacheTimes::GAME_COUNTS => 10 * 60,
CacheTimes::COUNTS => 10 * 60,

CacheTimes::SKYBLOCK_PROFILE => 10 * 60
];
Expand Down Expand Up @@ -252,26 +250,15 @@ public abstract function getPunishmentStats();
public abstract function setPunishmentStats(PunishmentStats $punishmentStats);

/**
* @return PlayerCount|null
*/
public abstract function getPlayerCount();

/**
* @param PlayerCount $playerCount
* @return void
*/
public abstract function setPlayerCount(PlayerCount $playerCount);

/**
* @return GameCounts|null
* @return Counts|null
*/
public abstract function getGameCounts();
public abstract function getCounts();

/**
* @param GameCounts $gameCounts
* @param Counts $gameCounts
* @return void
*/
public abstract function setGameCounts(GameCounts $gameCounts);
public abstract function setCounts(Counts $gameCounts);

/**
* @param $profile_id
Expand Down
3 changes: 1 addition & 2 deletions src/cache/CacheTimes.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,13 @@ abstract class CacheTimes {
const GUILD = 'guild';
const GUILD_NOT_FOUND = 'guildNotFound';
const LEADERBOARDS = 'leaderboards';
const PLAYER_COUNT = 'playerCount';
const BOOSTERS = 'boosters';
const STATUS = 'status';
const RECENT_GAMES = 'recentGames';
const KEY_INFO = 'keyInfo';
const FRIENDS = 'friends';
const PUNISHMENT_STATS = 'punishmentStats';
const GAME_COUNTS = 'gameCounts';
const COUNTS = 'counts';
const SKYBLOCK_PROFILE = 'skyblock_profile';

}
3 changes: 1 addition & 2 deletions src/cache/CacheTypes.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ abstract class CacheTypes {
const LEADERBOARDS = 'leaderboards';
const BOOSTERS = 'boosters';
const PUNISHMENT_STATS = 'punishmentStats';
const PLAYER_COUNT = 'playerCount';
const GAME_COUNTS = 'gameCounts';
const COUNTS = 'counts';

}
37 changes: 9 additions & 28 deletions src/cache/impl/FlatFileCacheHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,17 @@
use Plancke\HypixelPHP\cache\CacheTimes;
use Plancke\HypixelPHP\cache\CacheTypes;
use Plancke\HypixelPHP\responses\booster\Boosters;
use Plancke\HypixelPHP\responses\counts\Counts;
use Plancke\HypixelPHP\responses\friend\Friends;
use Plancke\HypixelPHP\responses\gameCounts\GameCounts;
use Plancke\HypixelPHP\responses\guild\Guild;
use Plancke\HypixelPHP\responses\KeyInfo;
use Plancke\HypixelPHP\responses\Leaderboards;
use Plancke\HypixelPHP\responses\player\Player;
use Plancke\HypixelPHP\responses\PlayerCount;
use Plancke\HypixelPHP\responses\PunishmentStats;
use Plancke\HypixelPHP\responses\RecentGames;
use Plancke\HypixelPHP\responses\Resource;
use Plancke\HypixelPHP\responses\skyblock\SkyBlockProfile;
use Plancke\HypixelPHP\responses\Status;
use Plancke\HypixelPHP\responses\PunishmentStats;
use Plancke\HypixelPHP\util\CacheUtil;

/**
Expand Down Expand Up @@ -341,39 +340,21 @@ public function setPunishmentStats(PunishmentStats $punishmentStats) {
}

/**
* @return PlayerCount|null
*/
public function getPlayerCount() {
return $this->wrapProvider(
$this->getHypixelPHP()->getProvider()->getPlayerCount(),
$this->_getCache(CacheTypes::PLAYER_COUNT)
);
}

/**
* @param PlayerCount $playerCount
* @throws InvalidArgumentException
*/
public function setPlayerCount(PlayerCount $playerCount) {
$this->_setCache(CacheTypes::PLAYER_COUNT, $playerCount);
}

/**
* @return GameCounts|null
* @return Counts|null
*/
public function getGameCounts() {
public function getCounts() {
return $this->wrapProvider(
$this->getHypixelPHP()->getProvider()->getGameCounts(),
$this->_getCache(CacheTypes::GAME_COUNTS)
$this->getHypixelPHP()->getProvider()->getCounts(),
$this->_getCache(CacheTypes::COUNTS)
);
}

/**
* @param GameCounts $gameCounts
* @param Counts $gameCounts
* @throws InvalidArgumentException
*/
public function setGameCounts(GameCounts $gameCounts) {
$this->_setCache(CacheTypes::GAME_COUNTS, $gameCounts);
public function setCounts(Counts $gameCounts) {
$this->_setCache(CacheTypes::COUNTS, $gameCounts);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/cache/impl/MongoCacheHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ public function getGuild($id) {
*/
public function setGuild(Guild $guild) {
$this->selectDB()->selectCollection(CacheTypes::GUILDS)->replaceOne(
['record._id' => (string)$guild->getID()], $this->objToArray($guild), self::UPDATE_OPTIONS
['record._id' => $guild->getID()], $this->objToArray($guild), self::UPDATE_OPTIONS
);
}

Expand Down
16 changes: 4 additions & 12 deletions src/cache/impl/NoCacheHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,16 @@

use Plancke\HypixelPHP\cache\CacheHandler;
use Plancke\HypixelPHP\responses\booster\Boosters;
use Plancke\HypixelPHP\responses\counts\Counts;
use Plancke\HypixelPHP\responses\friend\Friends;
use Plancke\HypixelPHP\responses\gameCounts\GameCounts;
use Plancke\HypixelPHP\responses\guild\Guild;
use Plancke\HypixelPHP\responses\KeyInfo;
use Plancke\HypixelPHP\responses\Leaderboards;
use Plancke\HypixelPHP\responses\player\Player;
use Plancke\HypixelPHP\responses\PlayerCount;
use Plancke\HypixelPHP\responses\PunishmentStats;
use Plancke\HypixelPHP\responses\RecentGames;
use Plancke\HypixelPHP\responses\skyblock\SkyBlockProfile;
use Plancke\HypixelPHP\responses\Status;
use Plancke\HypixelPHP\responses\PunishmentStats;

/**
* Class NoCacheHandler
Expand Down Expand Up @@ -110,17 +109,10 @@ function getPunishmentStats() {
return null;
}

function setPlayerCount(PlayerCount $playerCount) {
}

function getPlayerCount() {
return null;
}

function setGameCounts(GameCounts $gameCounts) {
function setCounts(Counts $counts) {
}

function getGameCounts() {
function getCounts() {
return null;
}

Expand Down
2 changes: 1 addition & 1 deletion src/fetch/FetchParams.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ abstract class FetchParams {
/**
* @return array
*/
public static function values() {
public static function values(): array {
return [
self::PLAYER_BY_NAME,
self::PLAYER_BY_UUID,
Expand Down
6 changes: 3 additions & 3 deletions src/fetch/FetchTypes.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ abstract class FetchTypes {
const RECENT_GAMES = 'recentGames';
const KEY = 'key';
const PUNISHMENT_STATS = 'punishmentStats';
const PLAYER_COUNT = 'playerCount';
const COUNTS = 'counts';
const GAME_COUNTS = 'gameCounts';

const SKYBLOCK_PROFILE = 'skyblock/profile';

public static function values() {
public static function values(): array {
return [
self::RESOURCES,
self::PLAYER,
Expand All @@ -39,7 +39,7 @@ public static function values() {
self::RECENT_GAMES,
self::KEY,
self::PUNISHMENT_STATS,
self::PLAYER_COUNT,
self::COUNTS,
self::GAME_COUNTS,
self::SKYBLOCK_PROFILE
];
Expand Down
55 changes: 48 additions & 7 deletions src/fetch/Fetcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
namespace Plancke\HypixelPHP\fetch;

use Closure;
use Plancke\HypixelPHP\cache\CacheHandler;
use Plancke\HypixelPHP\classes\Module;
use Plancke\HypixelPHP\exceptions\HypixelPHPException;
use Plancke\HypixelPHP\fetch\adapter\ResponseAdapter;
use Plancke\HypixelPHP\HypixelPHP;

Expand Down Expand Up @@ -34,7 +36,7 @@ public function __construct(HypixelPHP $HypixelPHP) {
* @param Closure $getter
* @return $this
*/
public function setResponseAdapterGetter(Closure $getter) {
public function setResponseAdapterGetter(Closure $getter): Fetcher {
$this->responseAdapterGetter = $getter;
$this->responseAdapter = null;
return $this;
Expand All @@ -43,7 +45,7 @@ public function setResponseAdapterGetter(Closure $getter) {
/**
* @return ResponseAdapter
*/
public function getResponseAdapter() {
public function getResponseAdapter(): ResponseAdapter {
if ($this->responseAdapter == null) {
$getter = $this->responseAdapterGetter;
$this->responseAdapter = $getter($this->getHypixelPHP());
Expand All @@ -55,7 +57,7 @@ public function getResponseAdapter() {
* @param ResponseAdapter $responseAdapter
* @return $this
*/
public function setResponseAdapter(ResponseAdapter $responseAdapter) {
public function setResponseAdapter(ResponseAdapter $responseAdapter): Fetcher {
$this->responseAdapter = $responseAdapter;
$this->responseAdapterGetter = null;
return $this;
Expand All @@ -64,29 +66,68 @@ public function setResponseAdapter(ResponseAdapter $responseAdapter) {
/**
* @return int
*/
public function getTimeOut() {
public function getTimeOut(): int {
return $this->timeOut;
}

/**
* @param int $timeOut
* @return $this
*/
public function setTimeOut($timeOut) {
public function setTimeOut(int $timeOut): Fetcher {
$this->timeOut = $timeOut;
return $this;
}

/**
* @param string $fetch
* @param array $keyValues
* @return string
*/
public function createUrl(string $fetch, $keyValues = []): string {
$requestURL = Fetcher::BASE_URL . $fetch;
if (sizeof($keyValues) > 0) {
$requestURL .= '?';
foreach ($keyValues as $key => $value) {
$value = urlencode(trim($value));
$requestURL .= '&' . $key . '=' . $value;
}
}
return $requestURL;
}

/**
* @param string $fetch
* @param string $url
* @param array $options
* @return Response
* @throws HypixelPHPException
*/
abstract function fetch($fetch, $keyValues = []);
public function fetch(string $fetch, string $url, $options = []): Response {
if (!is_array($options)) {
throw new HypixelPHPException("options is not an array");
}

$this->getHypixelPHP()->getLogger()->log(LOG_DEBUG, 'Starting Fetch: ' . $url);

$response = $this->getURLContents($url, $options);
if (!$response->wasSuccessful()) {
$this->getHypixelPHP()->getLogger()->log(LOG_DEBUG, 'Fetch Failed! ' . var_export($response, true));

// If one fails, stop trying for that status
// ideally also have a cached check before
$this->getHypixelPHP()->getCacheHandler()->setGlobalTime(CacheHandler::MAX_CACHE_TIME);
} else {
$this->getHypixelPHP()->getLogger()->log(LOG_DEBUG, 'Fetch successful!');
}

return $this->getResponseAdapter()->adaptResponse($fetch, $response);
}

/**
* @param string $url
* @param array $options
* @return Response
*/
abstract function getURLContents($url);
abstract function getURLContents(string $url, $options = []): Response;
}
Loading

0 comments on commit 0f4af99

Please sign in to comment.