Skip to content

Commit 135af0b

Browse files
feat: new method to upload an app, function to create config file and refactor in the routes system to add a decorator "on_request" '-'
1 parent 3236878 commit 135af0b

File tree

6 files changed

+232
-37
lines changed

6 files changed

+232
-37
lines changed

README.md

Lines changed: 97 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,39 @@
11
[SquareCloud]: https://squarecloud.app
2+
23
[API]: https://docs.squarecloud.app/api/introducao
3-
[@alma]: https://github.com/Robert-Nogueira
44

5+
[@alma]: https://github.com/Robert-Nogueira
56

67
# squarecloud-api
8+
79
squarecloud-api is a wrapper of the [API] of [SquareCloud] made by [@alma]
810

911
## Installing
12+
1013
````
1114
pip install squarecloud-api
1215
````
1316

1417
## Starting
18+
1519
### Where to get my api key?
16-
to get your api key/token just go to the [SquareCloud] website and register/login, after that go to `dashboard` > `my account` > `Regenerate API/CLI KEY` and copy the key.
20+
21+
to get your api key/token just go to the [SquareCloud] website and
22+
register/login, after that go
23+
to `dashboard` > `my account` > `Regenerate API/CLI KEY` and copy the key.
1724

1825
### The client instance
26+
1927
with the client you can get information about your application
28+
2029
````python
2130
import squarecloud as square
2231

2332
client = square.Client(api_key='API KEY')
2433

2534
app_id = 'application id'
35+
36+
2637
async def example():
2738
logs = await client.get_logs(app_id)
2839
print(logs)
@@ -39,7 +50,9 @@ async def example():
3950
print(status.storage)
4051

4152
````
53+
4254
you can also manage your application
55+
4356
````python
4457
import squarecloud as square
4558

@@ -48,6 +61,8 @@ import squarecloud as square
4861
client = square.Client(api_key='API KEY')
4962

5063
app_id = 'application id'
64+
65+
5166
async def example():
5267
await client.backup(app_id)
5368
await client.start_app(app_id)
@@ -57,13 +72,18 @@ async def example():
5772
await client.commit(app_id, file=square.File('path/to/your/file'))
5873

5974
````
75+
6076
### Application object
61-
Using the client you can also get an application or a list of applications and use them to manage your application.
77+
78+
Using the client you can also get an application or a list of applications and
79+
use them to manage your application.
80+
6281
````python
6382
import squarecloud as square
6483

6584
client = square.Client(api_key='API KEY')
6685

86+
6787
async def example():
6888
app = await client.app('application id')
6989

@@ -75,7 +95,7 @@ async def example():
7595
print(status.network)
7696
print(status.running)
7797
print(status.storage)
78-
98+
7999
# managing
80100
await app.stop()
81101
await app.start()
@@ -84,19 +104,89 @@ async def example():
84104
await app.delete()
85105
await app.backup()
86106
await app.commit(square.File('path/to/your/file'))
87-
107+
88108
apps_list = await client.all_apps()
89109
for app in apps_list:
90110
print(app)
91111

92112
````
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')
128+
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+
"""
176+
```
177+
178+
93179
### Debug mode
94-
Every time a request is made logs are displayed in the terminal containing useful informations, You can disable this by setting the `debug` parameter to False for the client, this value by default is True.
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+
95184
````py
96185
import squarecloud as square
97186

98-
client = square.Client(api_key='API KEY', debug=False) # no logs
187+
client = square.Client(api_key='API KEY', debug=False) # no logs
99188
````
100189

101190
## License
191+
102192
MIT License

squarecloud/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1-
from .client import Client
1+
from .client import Client, create_config_file
22
from .square import File
33
from .errors import *
4+
from .http import Routes

squarecloud/client.py

Lines changed: 63 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
import logging
55
from abc import ABC, abstractmethod
6-
from typing import List
6+
from typing import List, Literal, Any, Callable
77

88
from .data import (
99
AppData,
@@ -13,10 +13,10 @@
1313
BackupData,
1414
CompleteLogsData,
1515
)
16-
from .http import HTTPClient, Response
16+
from .http import HTTPClient, Response, Routes
1717
from .logs import logger
1818
from .square import File, Application
19-
from .errors import ApplicationNotFound
19+
from .errors import ApplicationNotFound, InvalidFile
2020
from .types import (
2121
UserPayload,
2222
StatusPayload,
@@ -35,6 +35,55 @@ def api_key(self):
3535
"""get the api token"""
3636

3737

38+
def create_config_file(
39+
display_name: str,
40+
main: str,
41+
memory: int,
42+
version: Literal['recommended', 'latest'],
43+
path: str,
44+
avatar: str | None = None,
45+
description: str | None = None,
46+
subdomain: str | None = None,
47+
start: str | None = None
48+
):
49+
"""
50+
Creates a config file (squarecloud.app)
51+
Args:
52+
53+
display_name: name of your application
54+
main: name of your main file
55+
memory: amount of memory to be allocated
56+
version: version of python and node.js, you can choose between
57+
'recommended' and 'latest'
58+
avatar: your application avatar url
59+
description: your application description
60+
subdomain: subdomain of your application
61+
start: the command to start your application
62+
path: the path where the file should be saved
63+
64+
Returns:
65+
TextIOWrapper
66+
"""
67+
content: str = ''
68+
optionals: dict[str, Any] = {
69+
'DISPLAY_NAME': display_name,
70+
'MAIN': main,
71+
'MEMORY': memory,
72+
'VERSION': version,
73+
'AVATAR': avatar,
74+
'DESCRIPTION': description,
75+
'SUBDOMAIN': subdomain,
76+
'START': start,
77+
}
78+
for key, value in optionals.items():
79+
if value is not None:
80+
string: str = f'{key}={value}\n'
81+
content += string
82+
with open(f'./{path}/squarecloud.app', 'w') as f:
83+
f.write(content)
84+
return f
85+
86+
3887
class Client(AbstractClient):
3988
"""A client for interacting with the SquareCloud API."""
4089

@@ -49,6 +98,7 @@ def __init__(self, api_key: str, debug: bool = True) -> None:
4998
self.debug = debug
5099
self._api_key = api_key
51100
self.__http = HTTPClient(api_key=api_key)
101+
self._listeners: dict[Routes, Callable]
52102
if self.debug:
53103
logger.setLevel(logging.DEBUG)
54104

@@ -212,3 +262,13 @@ async def all_apps(self) -> List[Application]:
212262
apps: List[Application] = [Application(client=self, data=data) for data
213263
in apps_data]
214264
return apps
265+
266+
async def upload_app(self, zip: File, check_zip: bool = True):
267+
if not isinstance(zip, File):
268+
raise InvalidFile(
269+
f'you need provide an {File.__name__} object')
270+
elif zip.name.split('.')[-1] != 'zip':
271+
raise InvalidFile('the file must be a .zip file')
272+
await self.__http.upload(zip)
273+
274+
# async def on_request(self):

squarecloud/errors.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,15 @@ class BadRequestError(RequestError):
2020

2121
class ApplicationNotFound(SquareException):
2222
"""raises when an application is not found"""
23+
24+
25+
class InvalidFile(SquareException):
26+
"""raised when a file is invalid"""
27+
28+
29+
class MissingConfigFile(RequestError):
30+
"""raised when the configuration file is missing"""
31+
32+
33+
class MissingDependenciesFile(RequestError):
34+
"""raised when the configuration file is missing"""

0 commit comments

Comments
 (0)