Skip to content

Commit e8b6e23

Browse files
squarecloud-api 2.0.0
1 parent 550b752 commit e8b6e23

File tree

10 files changed

+539
-252
lines changed

10 files changed

+539
-252
lines changed

README.md

Lines changed: 18 additions & 160 deletions
Original file line numberDiff line numberDiff line change
@@ -1,192 +1,50 @@
11
[SquareCloud]: https://squarecloud.app
22

3-
[API]: https://docs.squarecloud.app/api/introducao
3+
[SquareCloudAPI]: https://docs.squarecloud.app/api/introducao
44

55
[@alma]: https://github.com/Robert-Nogueira
66

77
# squarecloud-api
88

9-
squarecloud-api is a wrapper of the [API] of [SquareCloud] made by [@alma]
9+
squarecloud-api is a wrapper for the [SquareCloudAPI] mainted by [@alma]
10+
1011

1112
## Installing
1213

1314
````
1415
pip install squarecloud-api
1516
````
1617

17-
## Starting
1818

19-
### Where to get my api key?
19+
## Getting api key
2020

2121
to get your api key/token just go to the [SquareCloud] website and
2222
register/login, after that go
2323
to `dashboard` > `my account` > `Regenerate API/CLI KEY` and copy the key.
2424

25-
### The client instance
26-
27-
with the client you can get information about your application
28-
29-
````python
30-
import squarecloud as square
31-
32-
client = square.Client(api_key='API KEY')
33-
34-
app_id = 'application id'
35-
36-
37-
async def example():
38-
logs = await client.get_logs(app_id)
39-
print(logs)
40-
41-
complete_logs = await client.get_logs(app_id)
42-
print(complete_logs)
43-
44-
status = await client.app_status(app_id)
45-
print(status.ram)
46-
print(status.cpu)
47-
print(status.requests)
48-
print(status.network)
49-
print(status.running)
50-
print(status.storage)
51-
52-
````
53-
54-
you can also manage your application
55-
56-
````python
57-
import squarecloud as square
58-
59-
# you can define if you want debug logs to be shown or not
60-
# this value by default is True
61-
client = square.Client(api_key='API KEY')
62-
63-
app_id = 'application id'
64-
65-
66-
async def example():
67-
await client.backup(app_id)
68-
await client.start_app(app_id)
69-
await client.stop_app(app_id)
70-
await client.restart_app(app_id)
71-
await client.delete_app(app_id)
72-
await client.commit(app_id, file=square.File('path/to/your/file'))
73-
74-
````
75-
76-
### Application object
77-
78-
Using the client you can also get an application or a list of applications and
79-
use them to manage your application.
25+
> ## [Documentation](docs/)
26+
> - [application](docs/application.md)
27+
> - [capture_listeners](docs/capture_listeners.md)
28+
> - [client](docs/client.md)
29+
> - [commit_and_upload](docs/commit_and_upload.md)
30+
> - [request_listeners](docs/request_listeners.md)
8031
81-
````python
32+
## Basict usage
33+
```python
8234
import squarecloud as square
35+
import asyncio
8336

84-
client = square.Client(api_key='API KEY')
85-
86-
87-
async def example():
88-
app = await client.app('application id')
37+
client = square.Client('API_KEY', debug=True)
8938

90-
# app information
91-
status = await app.status()
39+
async def main():
40+
status = await client.app_status(app_id='application_id')
41+
print(status)
9242
print(status.ram)
9343
print(status.cpu)
94-
print(status.requests)
95-
print(status.network)
96-
print(status.running)
97-
print(status.storage)
98-
99-
# managing
100-
await app.stop()
101-
await app.start()
102-
await app.restart()
103-
await app.backup()
104-
await app.delete()
105-
await app.backup()
106-
await app.commit(square.File('path/to/your/file'))
107-
108-
apps_list = await client.all_apps()
109-
for app in apps_list:
110-
print(app)
111-
112-
````
113-
114-
### Uploading an application
115-
116-
to upload an application you need of a zip contain the following files:
117-
118-
- the main file: the file responsible to start the application
119-
- the dependency file: contains information about with dependencies need to be
120-
installed
121-
- the configuration file(squarecloud.app): a configuration file to upload, for
122-
more information take a look at [this link](https://docs.squarecloud.app/suporte/como-hospedar#configurando-sua-aplicacao)
123-
124-
```python
125-
import squarecloud as square
126-
127-
client = square.Client(api_key='API KEY')
12844

129-
130-
async def example():
131-
await client.upload_app(square.File('path/to/my_file.zip'))
132-
```
133-
for your convenience a function was added to create the configuration file
134-
135-
```python
136-
import squarecloud as square
137-
138-
# application example
139-
square.create_config_file(
140-
path='directory/to/save/', # the path where the file should be saved
141-
display_name='an cool name',
142-
description='an cool description',
143-
main='main.py',
144-
memory=100,
145-
version='recommended',
146-
)
147-
148-
# website example
149-
square.create_config_file(
150-
path='directory/to/save',
151-
display_name='cool website',
152-
description='this is really cool',
153-
main='index.js',
154-
subdomain='coolwebsite.squareweb.app',
155-
start='start this cool website', # if not static it is configurable
156-
memory=512,
157-
version='recommended',
158-
)
159-
160-
"""
161-
[REQUIRED] parameters
162-
---------------------
163-
path: str
164-
display_name: str
165-
main: str
166-
memory: int
167-
version: Literal['recommended', 'latest']
168-
169-
[OPTIONAL] parameters
170-
---------------------
171-
avatar: str | None = None
172-
description: str | None = None
173-
subdomain: str | None = None
174-
start: str | None = None
175-
"""
45+
asyncio.run(main())
17646
```
17747

178-
179-
### Debug mode
180-
Every time a request is made logs are displayed in the terminal containing
181-
useful information, You can disable this by setting the `debug` parameter to
182-
False for the client, this value by default is True.
183-
184-
````py
185-
import squarecloud as square
186-
187-
client = square.Client(api_key='API KEY', debug=False) # no logs
188-
````
189-
19048
## License
19149

19250
MIT License

docs/application.md

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
### Application object
2+
3+
Using the client you can also get an application or a list of applications and
4+
use them to manage your application.
5+
6+
````python
7+
import squarecloud as square
8+
9+
client = square.Client(api_key='API KEY')
10+
11+
12+
async def example():
13+
app = await client.app('application id')
14+
15+
# app information
16+
status = await app.status() # StatusData(...)
17+
18+
print(status.ram) # '70MB'
19+
print(status.cpu) # '5%'
20+
print(status.requests) # 0
21+
print(status.network) # {'total': '0 KB ↑ 0 KB ↓', 'now': '0 KB ↑ 0 KB ↓'}
22+
print(status.running) # True | False
23+
print(status.storage) # '0MB'
24+
25+
# managing
26+
start = await app.start() # Response(success)
27+
stop = await app.stop() # Response(success)
28+
restart = await app.restart() # Response(success)
29+
30+
backup = await app.backup() # BackupData(downloadURL='https://squarecloud.app/dashboard/backup/123.zip')
31+
print(
32+
backup.downloadURL) # 'https://squarecloud.app/dashboard/backup/123.zip'
33+
34+
# [WARNING] this will delete your app, remember to have some backup saved
35+
delete = await app.delete() # Response(success)
36+
37+
apps_list = await client.all_apps()
38+
for app in apps_list:
39+
print(app) # <Application tag='my_cool_bot' id='12345678abcde'>
40+
````
41+
42+
___
43+
44+
## Caching
45+
46+
When a request is made it returns information from the application and caches
47+
it in the application itself. In case you need to access this information again
48+
in a considerable amount of time, that is, if it is not worth making a new
49+
request to have updated data. In cases like this you can
50+
access `Application.cache`
51+
```python
52+
import squarecloud as square
53+
from squarecloud.app import Application
54+
55+
client = square.Client('API_KEY')
56+
57+
async def example():
58+
def print_cache(app: Application):
59+
cache = app.cache
60+
print(cache.status)
61+
print(cache.logs)
62+
print(cache.full_logs)
63+
print(cache.backup)
64+
65+
app = await client.app('application_id')
66+
await app.status()
67+
print_cache(app)
68+
```

docs/capture_listeners.md

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# Capture Listeners
2+
3+
_this implementation is based on a [suggestion](https://github.com/squarecloudofc/wrapper-api-py/pull/1) made
4+
by [@Mudinho](https://github.com/zRickz), thanks for contributing_
5+
6+
Sometimes it's very useful to have listeners, with them, you can implement
7+
features that need to be called whenever "something" is done in your
8+
application.
9+
10+
For example, imagine that every time a request for the '/logs' route is made,
11+
my application executes some task that checks if the new logs are different
12+
from the old ones. Well, let's see how this could be done:
13+
14+
```python
15+
import squarecloud as square
16+
from squarecloud import Endpoint
17+
18+
client = square.Client('API_KEY', debug=False)
19+
apps = await client.all_apps()
20+
app = apps[0]
21+
22+
23+
@app.capture(endpoint=Endpoint.logs())
24+
async def on_logs_request(before, after):
25+
if after != before:
26+
print(f'New logs!!! {after}')
27+
28+
29+
async def example():
30+
await app.logs() # True
31+
await app.logs() # False
32+
await app.logs(avoid_listener=True) # the listener is not called
33+
```
34+
35+
As you may have noticed, the first time the comparison between the logs
36+
happens `after != before` returns `True`, this happens precisely
37+
because `after` is equal to `LogsData(logs=None)`, as there is nothing yet
38+
stored in internal cache.
39+
40+
**Another information about this decorator**
41+
42+
- if you use discord.py or some fork (most likely you do), you should
43+
be familiar with the fact that what differentiates events is the name
44+
of the functions that the decorator involves, but here it differs, to know
45+
which
46+
api route the decorator needs to "watch" the endpoint parameter is used which
47+
takes an `Endpoint` class, so the name of the function that the decorator
48+
wraps
49+
It's up to you.
50+
- the function that the decorator wraps can actually be anything you
51+
be a callable. This includes normal functions, coroutines, and even
52+
classes(`__init__` will be called)
53+
- if the endpoint is not an `Endpoint.app_status()`
54+
, `Endpoints.logs()`, `Endpoints.full_logs()` or `Endpoints.backup()`
55+
only one `response` parameter (of type `squarecloud.http.Response`) will be
56+
returned

0 commit comments

Comments
 (0)