Skip to content

Commit

Permalink
Merge pull request #3 from AW1534/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
AW1534 committed Feb 5, 2022
2 parents d62e59b + 875d983 commit 8cf5ecf
Show file tree
Hide file tree
Showing 13 changed files with 146 additions and 55 deletions.
36 changes: 0 additions & 36 deletions .github/workflows/python-publish.yml

This file was deleted.

6 changes: 3 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
/.idea/
*.log
*.env

# all files created by the build process
/docs/_build/
/docs/_build/
/__pycache__/
*.egg-info
1 change: 1 addition & 0 deletions .readthedocs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ build:
sphinx:
builder: html
configuration: docs/conf.py
fail_on_warning: false

python:
install:
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ Just a simple wrapper to assist you with the simple crypto-versus API.

*docs [here](https://cry-vs-py.readthedocs.io/)*

---
<!-- footer gets added here for pypi version in setup.py-->
2 changes: 1 addition & 1 deletion config.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"version": "0.0.0.12"
}
}
14 changes: 12 additions & 2 deletions docs/_build/html/_static/js/script.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
// this script is intended for use with the sphinx books theme for sphinx


// override ctrl + f command
document.addEventListener('keydown', function(e) {
if (e.keyCode === 70 && e.ctrlKey) {
e.preventDefault();
document.getElementById('search-input').focus();
e.preventDefault(); // cancel default behavior
let navigation = document.getElementById("__navigation") // get the navigation element

if (navigation.checked) { // if the navigation is not visible, show it, wait for a small delay and focus the search input
document.getElementById("__navigation").checked = false;
setTimeout(() => document.getElementById('search-input').focus(), 10);
} else { // if the navigation is visible, just focus the search input
document.getElementById('search-input').focus()
}
}
});
2 changes: 1 addition & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,4 @@


def setup(app):
app.add_js_file("_static/js/script.js")
app.add_js_file("js/script.js")
6 changes: 6 additions & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,10 @@ cry.login("username", "password")
this will start the event loop, and will not return until the client is closed. any logic that needs to be done after this function should be done in the `on_ready` event.
```

```{toctree}
:hidden:
reference.md
```


<!-- footer gets added here for pypi version in setup.py-->
79 changes: 79 additions & 0 deletions docs/reference.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
# API reference
An official API wrapper for [Crypto_Versus](https://github.com/ProtagonistsWasTaken/crypto_versus)

## Installation
Installation is simple. just run `$ python -m pip install cry_vs.py` in your terminal.

## Usage
```{note}
This package is used in a way similar to [discord.py](https://pypi.org/project/discord.py/). if you know how to use discord.py, the following steps should be a cakewalk.
```

---
### Initialization & logger setup
First set up the logging module. this is not necessary because cry_vs will try to set up the logging module for you, but is highly recommended.
```python
import logging, sys
logging.basicConfig(encoding="utf-8", stream=sys.stdout, level=logging.INFO)
```

Next, import cry_vs's client class.

```python
from cry_vs.client import Client
```

Once you have done that, you can create a client.

```python
cry = Client()
```

while there are no required arguments, you can also pass in the other arguments.

| **Name** | **Type** | **Default value** | **Description** |
|--------------------|----------|------------------------|-----------------------------------------------------------------------------|
| **host** | string | "cry-vs.herokuapp.com" | The url of the server you want to connect to. |
| **port** | int | 80 | The port that the socket should listen to. |
| **allow_unsecure** | bool | False | Whether to allow unsecure connections. |
| **keep_alive** | bool | True | Whether to keep the client validated by automatically refreshing the token. |


---
### events
Now that you have a client, you can start listening to events. to do so, just make a function with the `@cry.listen` decorator.

for example, if you want to listen to the `on_ready` event, you would do the following:
```python
@cry.listen
async def on_ready():
print("on_ready has been called")
```

you can add as many events as you want, and they depend on the name. (function must be asynchronous)

| **Name** | **Description** |
|----------------------|------------------------------------------------------------------------------------|
| **any_event** | Fired when any event is called | |
| **on_ready** | Fired when the event emitter is initialized (right after the client has logged in) |
| **on_token_refresh** | Fired after the token has been refreshed |

---
### finalize
finally, you can call `cry.login()` to start the client.

````{tabbed} API Key
```python
cry.login("key")
```
````

````{tabbed} Username and Password
```python
cry.login("username", "password")
```
````
```{warning}
this will start the event loop, and will not return until the client is closed. any logic that needs to be done after this function should be done in the `on_ready` event.
```

6 changes: 4 additions & 2 deletions example/src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,18 @@
logging.basicConfig(encoding="utf-8", stream=sys.stdout, level=logging.DEBUG)

from dotenv import load_dotenv

from cry_vs.client import Client

load_dotenv()
cry = Client()


@cry.listen
async def on_ready():
async def on_ready(ctx):
print(ctx)
print("logged in")
input("Press enter to do something")
print(await cry.game.action())


@cry.listen
Expand Down
15 changes: 10 additions & 5 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,30 @@
import os

import setuptools
import json

with open("config.json") as f:
wd = "C:\\Users\\addik\\PycharmProjects\\cry-vs\\" # replace with the absolute path to your project's root directory
with open(wd + "config.json", "r", encoding="utf-8") as f:
config = json.load(f)

version = config["version"]

with open("docs/source/index.md", "r", encoding="utf-8") as fh:
with open(wd + "README.md", "r", encoding="utf-8") as fh:
long_description = fh.read() + "\n\n" + \
"""---\n""" + \
"""*found an issue? [Please make an issue!](https://github.com/AW1534/cry-vs)*"""
"""---\n""" + \
"""*found a bug? [Please make an issue!](https://github.com/AW1534/cry-vs)*"""

setuptools.setup(
name="cry-vs.py",
version=str(version),
author="addikted",
author_email="addiktedmontage@gmail.com",
description="A Crypto-Versus wrapper",
long_description=long_description,
long_description_content_type="text/markdown",
url="https://github.com/AW1534/cry-vs.py",
project_urls={
"Documentation": "https://cry-vs-py.readthedocs.io/en/latest/",
"Bug Tracker": "https://github.com/AW1534/cry-vs.py/issues",
},
classifiers=[
Expand All @@ -36,4 +41,4 @@
install_requires=[
"requests>=2.27.1"
]
)
)
24 changes: 20 additions & 4 deletions src/cry_vs/client.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import datetime
import logging
import secrets
from functools import partial

import cry_vs.emitter
from . import exceptions
Expand All @@ -13,8 +14,22 @@
import json
from .HTTPHelper import Socket


class Client:
class _Game:
def __init__(self, client):
self.client = client

async def action(self):
r = self.client.socket.Send_Request(
method=self.client.socket.Methods.POST,
url=self.client.host + "/api/v0/dostuff",
data=json.dumps({
"token": self.client.auth.token.text
})
)

return r

class _Auth:
class _Token:
text: str = None
Expand All @@ -38,8 +53,8 @@ def _update(self, text: str, time: int):
allowUnsecure: bool = None
keep_alive: bool = None

game: _Game = None
auth: _Auth = _Auth()
token: auth._Token = None

def __init__(self, host="https://cry-vs.herokuapp.com", port=80, allow_unsecure=False, keep_alive=True):
self.emitter = None
Expand Down Expand Up @@ -79,7 +94,8 @@ def finish(r):
logger.debug(self.funcs)
self.auth.token = self._Auth._Token(r.text, int(r.headers["Expire"]))
self.emitter = self.Emitter(funcs=self.funcs, client=self)
self.emitter.enqueue(name="on_ready", args=r.headers["Expire"])
self.game = self._Game(self)
self.emitter.enqueue("on_ready", r.headers["Expire"]) # the user will most likely use this to start their code so ensure the client is ready to go before calling it

try: # test if the 3rd argument has been passed
if args[2] == False:
Expand Down Expand Up @@ -136,5 +152,5 @@ async def before_expire(self):
})
)
self.auth.token._update(r.text, int(r.headers["Expire"]))
self.emitter.enqueue(name="on_token_refresh", args=r.headers["Expire"])
self.emitter.enqueue("on_token_refresh", r.headers["Expire"])
logger.info("Token refreshed")
8 changes: 8 additions & 0 deletions upload.ps1
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
# store the directory of the script in a variable
$dir = Get-Location

# delete any existing distrobution files
Remove-Item "$dir/dist"

# install the latest version of dependencies
py -m pip install --upgrade twine
py -m pip install --upgrade build

# build the distribution
py -m build

# upload the distribution to PyPI
py -m twine upload --repository pypi dist/*
PAUSE

0 comments on commit 8cf5ecf

Please sign in to comment.