Skip to content

Commit

Permalink
Add caching, and various stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
janaSunrise committed Nov 4, 2020
1 parent 272916d commit 20b0930
Show file tree
Hide file tree
Showing 14 changed files with 111 additions and 11 deletions.
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,18 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [0.0.5](https://github.com/janaSunrise/HypixelIO/releases/tag/v0.0.5) - 04-11-2020

### Added
- Modular caching of requests,
- Added timeout for the cache and fetch
- Allow users to specify the caching according to needs, and various functions to make experience better
- Functions allowed:
- Clearing Cache
- Uninstall cache, If enabled
- removing expired cache
- Get the existsing cache, and check it.

## [0.0.4](https://github.com/janaSunrise/HypixelIO/releases/tag/v0.0.4) - 31-10-2020

### Added
Expand Down
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,20 @@ print(boosters[0].ID)
print(friends.FRIENDS[0].RECEIVER_ID)
```

#### Implementing caching with the Requests.

```python
from hypixelio import Client, Converters, Caching, CacheBackend

config = Caching("cache", CacheBackend.memory, 100, False)

client = Client(api_key="your-api-key", cache=True, cache_config=config)

boosters = client.get_boosters()

print(boosters[0].ID)
```

### TODOs PLANNED

- [x] Implement Games and leaderboard Models in searching
Expand All @@ -52,6 +66,8 @@ print(friends.FRIENDS[0].RECEIVER_ID)
- Username to UUID
- [x] Fix `__repr__` and `__str__`
- [x] Implement caching for efficiency
- [x] Allow To opt for, or out of caching, Using Variables in `__init__` of `Client` class
- [ ] Allow users to compare two objects, and override in code by extending them.

If you're interested in seeing the **Changelog**, Go [here!](https://github.com/janaSunrise/HypixelIO/blob/main/CHANGELOG.md)

Expand Down
4 changes: 2 additions & 2 deletions hypixelio/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
from .lib import Client
from .lib import Converters
from .lib import Client, Converters
from .models.caching import Caching, CacheBackend
15 changes: 12 additions & 3 deletions hypixelio/lib/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

from hypixelio.models import (
boosters,
caching,
friends,
games,
guild,
Expand All @@ -33,8 +34,6 @@
GuildNotFoundError
)

requests_cache.install_cache('cache', backend='sqlite', expire_after=500)


class Client:
"""
Expand All @@ -44,16 +43,26 @@ class Client:
api_key (t.Union[str, list]):
This contains the Api Key, or the List of API Keys for the authentication.
"""
def __init__(self, api_key: t.Union[str, list]) -> None:
def __init__(self, api_key: t.Union[str, list], cache: bool = False, cache_config: caching.Caching = None) -> None:
"""
The constructor for the `Client` class.
Parameters:
api_key (str): The API Key generated in Hypixel using `/api new` command.
cache (bool): Whether to enable caching
cache_config (Caching): The configuration for the saving, and reusing of the cache
"""
if not isinstance(api_key, list):
self.api_key = [api_key]

if cache:
requests_cache.install_cache(
cache_name=cache_config.cache_name,
backend=cache_config.backend,
expire_after=cache_config.expire_after,
old_data_on_error=cache_config.old_data_on_error,
)

def _fetch(self, url: str, data: dict = None) -> tuple[dict, bool]:
"""
Get the JSON Response from the Root Hypixel API URL,
Expand Down
3 changes: 0 additions & 3 deletions hypixelio/lib/converters.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import typing as t

import requests
import requests_cache

from hypixelio.exceptions.exceptions import (
InvalidArgumentError,
Expand All @@ -13,8 +12,6 @@
TIMEOUT
)

requests_cache.install_cache('cache', backend='sqlite', expire_after=500)


class Converters:
@classmethod
Expand Down
3 changes: 3 additions & 0 deletions hypixelio/models/boosters/booster_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,8 @@ def __init__(

self.STACKED = True if "stacked" in info else False

def __str__(self) -> str:
return self.PURCHASER_UUID

def __repr__(self) -> str:
return f'<{self.__class__.__name__} id="{self.ID}" purchaser="{self.PURCHASER_UUID}" stacked={self.STACKED}>'
6 changes: 6 additions & 0 deletions hypixelio/models/boosters/boosters.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,9 @@ def __init__(
boosters: list,
) -> None:
self.BOOSTERS = [BoosterInfo(booster) for booster in boosters]

def __str__(self) -> str:
return str(len(self.BOOSTERS))

def __repr__(self) -> str:
return f"<{self.__class__.__name__} booster_count={len(self.BOOSTERS)}>"
4 changes: 4 additions & 0 deletions hypixelio/models/caching/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
"""This module is dedicated to definition of the Caching class."""

from .backend import CacheBackend
from .caching import Caching
5 changes: 5 additions & 0 deletions hypixelio/models/caching/backend.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class CacheBackend:
sqlite = "sqlite"
mongodb = "mongodb"
redis = "redis"
memory = "memory"
45 changes: 45 additions & 0 deletions hypixelio/models/caching/caching.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
"""This module is dedicated to definition of the Caching class."""

from .backend import CacheBackend
from requests_cache import core


class Caching:
"""
This is the Custom Caching model, for the Hypixel Requests to be made.
Attributes:
cache_name (str): The name of the Cache, Which will be stored
backend (CacheBackend): The format of storage of the Cache
expire_after (int): When will the cahe expire, In seconds
old_data_on_error (bool): Whether to use old data, If an error occurs out of no where.
"""
def __init__(
self,
cache_name: str = "cache",
backend: CacheBackend = CacheBackend.memory,
expire_after: int = None,
old_data_on_error: bool = False
) -> None:
self.cache_name = cache_name
self.backend = backend
self.expire_after = expire_after
self.old_data_on_error = old_data_on_error

def remove_expired_responses(self) -> None:
core.remove_expired_responses()

def get_cache(self) -> core.CachedSession:
return core.get_cache()

def clear_cache(self) -> None:
core.clear()

def uninstall_cache(self) -> None:
core.uninstall_cache()

def __str__(self) -> str:
return self.backend

def __repr__(self) -> str:
return f'<{self.__class__.__name__} cache_name="{self.cache_name}" backend="{self.backend}">'
3 changes: 3 additions & 0 deletions hypixelio/models/friends/friends.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,6 @@ def __init__(
friends: list
) -> None:
self.FRIENDS = [FriendData(friend) for friend in friends]

def __repr__(self) -> str:
return f"<{self.__class__.__name__} friends_count={len(self.FRIENDS)}>"
2 changes: 1 addition & 1 deletion hypixelio/models/games/game_count.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ def __init__(
self.MODES = game.get("modes")

def __repr__(self) -> str:
return f'<{self.__class__.__name__} players="{self.PLAYERS}">'
return f'<{self.__class__.__name__} players="{self.PLAYERS}" modes={self.MODES}>'
2 changes: 1 addition & 1 deletion hypixelio/models/games/games.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,4 @@ def __init__(
self.QUEUE = GameCount(games["QUEUE"])

def __repr__(self) -> str:
return f'<{self.__class__.__name__} lobby={self.MAIN_LOBBY} idle={self.IDLE}>'
return f'<{self.__class__.__name__} lobby={self.MAIN_LOBBY} idle={self.IDLE} queue={self.QUEUE}>'
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

setuptools.setup(
name="HypixelIO",
version="0.0.4",
version="0.0.5",

author="Sunrit Jana",
author_email="warriordefenderz@gmail.com",
Expand Down

0 comments on commit 20b0930

Please sign in to comment.