squarecloud-api 3.1.5__tar.gz → 3.2.1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (19) hide show
  1. {squarecloud_api-3.1.5 → squarecloud_api-3.2.1}/PKG-INFO +2 -1
  2. {squarecloud_api-3.1.5 → squarecloud_api-3.2.1}/pyproject.toml +3 -3
  3. {squarecloud_api-3.1.5 → squarecloud_api-3.2.1}/squarecloud/app.py +13 -0
  4. {squarecloud_api-3.1.5 → squarecloud_api-3.2.1}/squarecloud/cli/__init__.py +1 -1
  5. {squarecloud_api-3.1.5 → squarecloud_api-3.2.1}/squarecloud/cli/app.py +99 -2
  6. {squarecloud_api-3.1.5 → squarecloud_api-3.2.1}/squarecloud/client.py +27 -9
  7. {squarecloud_api-3.1.5 → squarecloud_api-3.2.1}/squarecloud/data.py +49 -1
  8. {squarecloud_api-3.1.5 → squarecloud_api-3.2.1}/squarecloud/errors.py +20 -3
  9. {squarecloud_api-3.1.5 → squarecloud_api-3.2.1}/squarecloud/http/endpoints.py +30 -2
  10. {squarecloud_api-3.1.5 → squarecloud_api-3.2.1}/squarecloud/http/http_client.py +52 -2
  11. {squarecloud_api-3.1.5 → squarecloud_api-3.2.1}/LICENSE +0 -0
  12. {squarecloud_api-3.1.5 → squarecloud_api-3.2.1}/README.md +0 -0
  13. {squarecloud_api-3.1.5 → squarecloud_api-3.2.1}/squarecloud/__init__.py +0 -0
  14. {squarecloud_api-3.1.5 → squarecloud_api-3.2.1}/squarecloud/cli/files.py +0 -0
  15. {squarecloud_api-3.1.5 → squarecloud_api-3.2.1}/squarecloud/cli/main.py +0 -0
  16. {squarecloud_api-3.1.5 → squarecloud_api-3.2.1}/squarecloud/file.py +0 -0
  17. {squarecloud_api-3.1.5 → squarecloud_api-3.2.1}/squarecloud/http/__init__.py +0 -0
  18. {squarecloud_api-3.1.5 → squarecloud_api-3.2.1}/squarecloud/listeners.py +0 -0
  19. {squarecloud_api-3.1.5 → squarecloud_api-3.2.1}/squarecloud/logs.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: squarecloud-api
3
- Version: 3.1.5
3
+ Version: 3.2.1
4
4
  Summary: SquareCloud API wrapper
5
5
  Home-page: https://github.com/squarecloudofc/wrapper-api-py
6
6
  License: MIT
@@ -16,6 +16,7 @@ Classifier: Programming Language :: Python :: 3.12
16
16
  Requires-Dist: aiohttp (==3.8.3)
17
17
  Requires-Dist: click (>=8.1.6,<9.0.0)
18
18
  Requires-Dist: pydantic (>=2.5.2,<3.0.0)
19
+ Requires-Dist: python-dotenv (>=1.0.0,<2.0.0)
19
20
  Requires-Dist: rich (>=13.5.2,<14.0.0)
20
21
  Project-URL: Documentation, https://docs.squarecloud.app/sdks/py
21
22
  Project-URL: Repository, https://github.com/squarecloudofc/wrapper-api-py
@@ -1,6 +1,6 @@
1
1
  [tool.poetry]
2
2
  name = "squarecloud-api"
3
- version = "3.1.5"
3
+ version = "3.2.1"
4
4
  description = "SquareCloud API wrapper"
5
5
  authors = ["Robert Nogueira <robertlucasnogueira@gmail.com>"]
6
6
  repository = "https://github.com/squarecloudofc/wrapper-api-py"
@@ -15,6 +15,7 @@ aiohttp = "3.8.3"
15
15
  click = "^8.1.6"
16
16
  rich = "^13.5.2"
17
17
  pydantic = "^2.5.2"
18
+ python-dotenv = "^1.0.0"
18
19
 
19
20
  [tool.poetry.group.dev.dependencies]
20
21
  pytest = "^7.4.3"
@@ -22,7 +23,6 @@ pytest-cov = "^4.1.0"
22
23
  blue = "^0.9.1"
23
24
  isort = "^5.12.0"
24
25
  taskipy = "^1.12.0"
25
- python-dotenv = "^1.0.0"
26
26
  pytest-asyncio = "^0.21.1"
27
27
  pre-commit = "^3.3.3"
28
28
  ruff = "^0.0.289"
@@ -58,4 +58,4 @@ line-length = 79
58
58
  exclude = ['env']
59
59
 
60
60
  [tool.ruff.per-file-ignores]
61
- '__init__.py' = ['F401']
61
+ '__init__.py' = ['F401']
@@ -10,6 +10,7 @@ from .data import (
10
10
  AppData,
11
11
  BackupData,
12
12
  DeployData,
13
+ DomainAnalytics,
13
14
  FileInfo,
14
15
  LogsData,
15
16
  StatusData,
@@ -619,3 +620,15 @@ class Application(AbstractApplication):
619
620
  avoid_listener=True,
620
621
  )
621
622
  return webhook
623
+
624
+ async def domain_analytics(self) -> DomainAnalytics:
625
+ analytics: DomainAnalytics = await self.client.domain_analytics(
626
+ self.id, avoid_listener=True
627
+ )
628
+ return analytics
629
+
630
+ async def set_custom_domain(self, custom_domain: str):
631
+ response: Response = await self.client.set_custom_domain(
632
+ self.id, custom_domain, avoid_listener=True
633
+ )
634
+ return response
@@ -3,7 +3,7 @@ from functools import wraps
3
3
 
4
4
  import click
5
5
 
6
- __version__ = '3.1.5'
6
+ __version__ = '3.2.1'
7
7
 
8
8
 
9
9
  def run_async(func):
@@ -14,6 +14,8 @@ from squarecloud.cli import cli, run_async
14
14
  from squarecloud.data import (
15
15
  AppData,
16
16
  BackupData,
17
+ DeployData,
18
+ DomainAnalytics,
17
19
  LogsData,
18
20
  StatusData,
19
21
  UploadData,
@@ -88,7 +90,7 @@ async def get_app_status(ctx: Context):
88
90
 
89
91
  table = Table(title='Status', header_style='purple')
90
92
 
91
- status_list = status.__slots__
93
+ status_list = status.to_dict()
92
94
  for s in status_list:
93
95
  table.add_column(s.capitalize(), justify='center')
94
96
  table.add_row(
@@ -158,7 +160,7 @@ async def app_data(ctx: Context):
158
160
  data: AppData = await client.app_data(app_id)
159
161
  table = Table(title='Status', header_style='purple')
160
162
 
161
- status_list = data.__slots__
163
+ status_list = data.to_dict()
162
164
  for s in status_list:
163
165
  table.add_column(s.capitalize(), justify='center')
164
166
  table.add_row(
@@ -329,3 +331,98 @@ async def commit(ctx: Context, file: BufferedReader):
329
331
  style='green',
330
332
  )
331
333
  )
334
+
335
+
336
+ @app_group.command(name='last-deploys')
337
+ @click.pass_context
338
+ @run_async
339
+ async def last_deploys(ctx: Context):
340
+ with Console().status('loading'):
341
+ client: Client = ctx.obj['client']
342
+ app_id = ctx.obj['app_id']
343
+ deploys: list[list[DeployData]] = await client.last_deploys(app_id)
344
+ if not deploys:
345
+ print(
346
+ Panel(
347
+ 'You do not have any recent deploys for this application',
348
+ title='No deploys',
349
+ title_align='left',
350
+ style='red',
351
+ ),
352
+ )
353
+ return
354
+ for deploy in deploys:
355
+ table = Table(header_style='purple')
356
+
357
+ attr_list = deploy[0].__dict__
358
+ for s in attr_list:
359
+ table.add_column(s.capitalize(), justify='center')
360
+
361
+ for d in deploy:
362
+ table.add_row(
363
+ *[str(getattr(d, attr)) for attr in attr_list],
364
+ style='green',
365
+ )
366
+
367
+ print(table)
368
+
369
+
370
+ @app_group.command(name='github-integration')
371
+ @click.argument('access_token', type=click.STRING)
372
+ @click.pass_context
373
+ @run_async
374
+ async def github_integration(ctx: Context, access_token: str):
375
+ with Console().status('loading'):
376
+ client: Client = ctx.obj['client']
377
+ app_id = ctx.obj['app_id']
378
+ webhook_url: str = await client.github_integration(
379
+ app_id, access_token
380
+ )
381
+ print(
382
+ Panel(
383
+ webhook_url,
384
+ title='Webhook url',
385
+ border_style='purple',
386
+ style='green',
387
+ )
388
+ )
389
+
390
+
391
+ @app_group.command(name='custom-domain')
392
+ @click.argument('domain', type=click.STRING)
393
+ @click.pass_context
394
+ @run_async
395
+ async def custom_domain(ctx: Context, domain: str):
396
+ with Console().status('loading'):
397
+ client: Client = ctx.obj['client']
398
+ app_id = ctx.obj['app_id']
399
+ await client.set_custom_domain(app_id, domain)
400
+ print(
401
+ Panel(
402
+ f'Domain defined to "{domain}"',
403
+ title='Success',
404
+ border_style='purple',
405
+ style='green',
406
+ )
407
+ )
408
+
409
+
410
+ @app_group.command(name='domain-analytics')
411
+ @click.pass_context
412
+ @run_async
413
+ async def domain_analytics(ctx: Context):
414
+ with Console().status('loading'):
415
+ client: Client = ctx.obj['client']
416
+ app_id = ctx.obj['app_id']
417
+ analytics: DomainAnalytics = await client.domain_analytics(app_id)
418
+ table = Table(title='Status', header_style='purple')
419
+
420
+ attr_list = analytics.to_dict()
421
+ for s in attr_list:
422
+ table.add_column(s.capitalize(), justify='center')
423
+ table.add_row(
424
+ *[str(getattr(analytics, attr)) for attr in attr_list],
425
+ style='green',
426
+ )
427
+
428
+ print(table)
@@ -11,6 +11,7 @@ from .data import (
11
11
  AppData,
12
12
  BackupData,
13
13
  DeployData,
14
+ DomainAnalytics,
14
15
  FileInfo,
15
16
  LogsData,
16
17
  StatisticsData,
@@ -92,17 +93,9 @@ def create_config_file(
92
93
 
93
94
 
94
95
  class Client(AbstractClient):
95
- """A client for interacting with the SquareCloud API.
96
-
97
- :ivar api_key: the api key for the client
98
- :ivar debug: weather the debug messages is enabled
99
-
100
- :type api_key: str
101
- :type debug: bool
102
- """
96
+ """A client for interacting with the SquareCloud API."""
103
97
 
104
98
  def __init__(self, api_key: str, debug: bool = True) -> None:
105
-
106
99
  """
107
100
  The __init__ function is called when the class is instantiated.
108
101
  It sets up the instance of the class, and defines all of its
@@ -619,3 +612,28 @@ class Client(AbstractClient):
619
612
  )
620
613
  data = response.response
621
614
  return data.get('webhook')
615
+
616
+ async def set_custom_domain(
617
+ self, app_id: str, custom_domain: str, **kwargs
618
+ ) -> Response:
619
+ response: Response = await self._http.update_custom_domain(
620
+ app_id=app_id, custom_domain=custom_domain
621
+ )
622
+ if not kwargs.get('avoid_listener'):
623
+ endpoint: Endpoint = response.route.endpoint
624
+ await self._listener.on_request(
625
+ endpoint=endpoint, response=response
626
+ )
627
+ return response
628
+
629
+ async def domain_analytics(self, app_id: str, **kwargs) -> DomainAnalytics:
630
+ response: Response = await self._http.domain_analytics(
631
+ app_id=app_id,
632
+ )
633
+ if not kwargs.get('avoid_listener'):
634
+ endpoint: Endpoint = response.route.endpoint
635
+ await self._listener.on_request(
636
+ endpoint=endpoint, response=response
637
+ )
638
+ analytics: DomainAnalytics = DomainAnalytics(**response.response)
639
+ return analytics
@@ -29,6 +29,9 @@ class PlanData:
29
29
  memory: Dict[str, Any]
30
30
  duration: Dict[str, Any] | None
31
31
 
32
+ def to_dict(self):
33
+ return self.__dict__
34
+
32
35
 
33
36
  # @dataclass(frozen=True)
34
37
  class Language(TypedDict):
@@ -72,8 +75,11 @@ class StatusData:
72
75
  uptime: conint(ge=0) | None = None
73
76
  time: conint(ge=0) | None = None
74
77
 
78
+ def to_dict(self):
79
+ return self.__dict__
80
+
75
81
 
76
- @dataclass(frozen=True, slots=True)
82
+ @dataclass(frozen=True)
77
83
  class AppData:
78
84
  """
79
85
  Application data class
@@ -112,6 +118,9 @@ class AppData:
112
118
  custom: str | None = None
113
119
  desc: str | None = None
114
120
 
121
+ def to_dict(self):
122
+ return self.__dict__
123
+
115
124
 
116
125
  @dataclass(frozen=True)
117
126
  class UserData:
@@ -134,6 +143,9 @@ class UserData:
134
143
  plan: PlanData
135
144
  email: str | None = None
136
145
 
146
+ def to_dict(self):
147
+ return self.__dict__
148
+
137
149
 
138
150
  @dataclass(frozen=True)
139
151
  class LogsData:
@@ -189,6 +201,9 @@ class LogsData:
189
201
  """
190
202
  return isinstance(other, LogsData) and self.logs == other.logs
191
203
 
204
+ def to_dict(self):
205
+ return self.__dict__
206
+
192
207
 
193
208
  @dataclass(frozen=True)
194
209
  class BackupData:
@@ -202,6 +217,9 @@ class BackupData:
202
217
 
203
218
  downloadURL: str
204
219
 
220
+ def to_dict(self):
221
+ return self.__dict__
222
+
205
223
 
206
224
  @dataclass(frozen=True)
207
225
  class UploadData:
@@ -233,6 +251,9 @@ class UploadData:
233
251
  subdomain: str | None = None
234
252
  description: str | None = None
235
253
 
254
+ def to_dict(self):
255
+ return self.__dict__
256
+
236
257
 
237
258
  @dataclass(frozen=True)
238
259
  class FileInfo:
@@ -258,6 +279,9 @@ class FileInfo:
258
279
  lastModified: conint(ge=0) | float
259
280
  path: str
260
281
 
282
+ def to_dict(self):
283
+ return self.__dict__
284
+
261
285
 
262
286
  @dataclass(frozen=True)
263
287
  class StatisticsData:
@@ -280,9 +304,33 @@ class StatisticsData:
280
304
  websites: conint(ge=0)
281
305
  ping: conint(ge=0)
282
306
 
307
+ def to_dict(self):
308
+ return self.__dict__
309
+
283
310
 
284
311
  @dataclass(frozen=True)
285
312
  class DeployData:
286
313
  id: str
287
314
  state: str
288
315
  date: datetime
316
+
317
+ def to_dict(self):
318
+ return self.__dict__
319
+
320
+
321
+ @dataclass(frozen=True)
322
+ class DomainAnalytics:
323
+ hostname: str
324
+ total: list[Any]
325
+ countries: list[Any]
326
+ methods: list[Any]
327
+ referers: list[Any]
328
+ browsers: list[Any]
329
+ deviceTypes: list[Any]
330
+ operatingSystems: list[Any]
331
+ agents: list[Any]
332
+ hosts: list[Any]
333
+ paths: list[Any]
334
+
335
+ def to_dict(self):
336
+ return self.__dict__
@@ -15,7 +15,7 @@ class RequestError(SquareException):
15
15
  """raised when a request fails"""
16
16
 
17
17
  def __init__(
18
- self, route: str, status_code: int, code: str, *args, **kwargs
18
+ self, route: str, status_code: int, code: str, *args, **kwargs
19
19
  ):
20
20
  self.route = route
21
21
  self.status = status_code
@@ -29,7 +29,9 @@ class AuthenticationFailure(RequestError):
29
29
 
30
30
  def __init__(self, *args, **kwargs):
31
31
  super().__init__(*args, **kwargs)
32
- self.message = 'Authentication failed: Invalid API token'
32
+ self.message = (
33
+ 'Authentication failed: ' 'Invalid API token or access denied'
34
+ )
33
35
 
34
36
 
35
37
  class NotFoundError(RequestError):
@@ -199,5 +201,20 @@ class MissingVersion(InvalidConfig):
199
201
 
200
202
 
201
203
  class InvalidAccessToken(RequestError):
202
- def __init__(self, *args, **kwargs):
204
+ """
205
+ raised when the GitHub access token provided by the user is invalid.
206
+ """
207
+
208
+
209
+ def __init__(self, *args, **kwargs):
210
+ super().__init__(*args, **kwargs)
211
+
212
+
213
+ class InvalidDomain(RequestError):
214
+ """
215
+ raised when an invalid domain is provided
216
+ """
217
+
218
+ def __init__(self, domain: str, *args, **kwargs):
203
219
  super().__init__(*args, **kwargs)
220
+ self.message = f'{domain} is a invalid custom domain'
@@ -63,6 +63,14 @@ class Endpoint:
63
63
  'METHOD': 'POST',
64
64
  'PATH': '/apps/{app_id}/deploy/git-webhook',
65
65
  },
66
+ 'CUSTOM_DOMAIN': {
67
+ 'METHOD': 'POST',
68
+ 'PATH': '/apps/{app_id}/network/custom/{custom_domain}',
69
+ },
70
+ 'DOMAIN_ANALYTICS': {
71
+ 'METHOD': 'GET',
72
+ 'PATH': '/apps/{app_id}/network/analytics',
73
+ },
66
74
  }
67
75
 
68
76
  def __init__(self, name: str) -> None:
@@ -242,16 +250,36 @@ class Endpoint:
242
250
  @classmethod
243
251
  def last_deploys(cls):
244
252
  """
245
- The last_deploys function returns a list of the last deploys that have
246
- been made.
253
+ Returns an Endpoint object that represents the
254
+ /apps/{app_id}/deploy/list endpoint.
247
255
  """
248
256
 
249
257
  return cls('LAST_DEPLOYS')
250
258
 
251
259
  @classmethod
252
260
  def github_integration(cls):
261
+ """
262
+ Returns an Endpoint object that represents the
263
+ /apps/{app_id}/deploy/git-webhook endpoint.
264
+ """
253
265
  return cls('GITHUB_INTEGRATION')
254
266
 
267
+ @classmethod
268
+ def domain_analytics(cls):
269
+ """
270
+ Returns an Endpoint object that represents the
271
+ /apps/{app_id}/network/analytics endpoint.
272
+ """
273
+ return cls('DOMAIN_ANALYTICS')
274
+
275
+ @classmethod
276
+ def custom_domain(cls):
277
+ """
278
+ Returns an Endpoint object that represents the
279
+ /apps/{app_id}/network/custom/{custom_domain} endpoint.
280
+ """
281
+ return cls('CUSTOM_DOMAIN')
282
+
255
283
 
256
284
  # pylint: disable=too-few-public-methods
257
285
  class Router:
@@ -13,6 +13,7 @@ from ..errors import (
13
13
  FewMemory,
14
14
  InvalidAccessToken,
15
15
  InvalidDisplayName,
16
+ InvalidDomain,
16
17
  InvalidMain,
17
18
  InvalidMemory,
18
19
  InvalidVersion,
@@ -65,7 +66,17 @@ class Response:
65
66
  return f'{Response.__name__}({self.data})'
66
67
 
67
68
 
68
- def _get_upload_error(code: str) -> type[RequestError] | None:
69
+ def _get_error(code: str) -> type[RequestError] | None:
70
+ """
71
+ The _get_error function is a helper function that takes in an error code
72
+ and returns the corresponding error class.
73
+ This allows us to easily map errors to their respective classes,
74
+ which makes it easier for us to raise them when we need to.
75
+
76
+ :param code: str: Determine which error to raise
77
+ :return: An error class
78
+ :doc-author: Trelent
79
+ """
69
80
  errors = {
70
81
  'FEW_MEMORY': FewMemory,
71
82
  'BAD_MEMORY': BadMemory,
@@ -80,6 +91,7 @@ def _get_upload_error(code: str) -> type[RequestError] | None:
80
91
  'INVALID_VERSION': InvalidVersion,
81
92
  'MISSING_VERSION': MissingVersion,
82
93
  'INVALID_ACCESS_TOKEN': InvalidAccessToken,
94
+ 'REGEX_VALIDATION': InvalidDomain,
83
95
  }
84
96
  error_class = errors.get(code, None)
85
97
  if error_class is None:
@@ -117,6 +129,9 @@ class HTTPClient:
117
129
  headers = {'Authorization': self.api_key}
118
130
  extra_error_kwargs: dict[str, Any] = {}
119
131
 
132
+ if kwargs.get('custom_domain'):
133
+ extra_error_kwargs['domain'] = kwargs.pop('custom_domain')
134
+
120
135
  if route.endpoint in (Endpoint.commit(), Endpoint.upload()):
121
136
  file = kwargs.pop('file')
122
137
  extra_error_kwargs['file'] = file
@@ -161,7 +176,7 @@ class HTTPClient:
161
176
  else:
162
177
  error = RequestError
163
178
 
164
- if _ := _get_upload_error(code):
179
+ if _ := _get_error(code):
165
180
  error = _
166
181
  if error:
167
182
  raise error(
@@ -424,3 +439,38 @@ class HTTPClient:
424
439
  body = {'access_token': github_access_token}
425
440
  response: Response = await self.request(route, json=body)
426
441
  return response
442
+
443
+ async def update_custom_domain(
444
+ self, app_id: str, custom_domain: str
445
+ ) -> Response:
446
+ """
447
+ The update_custom_domain function updates the custom domain of an app.
448
+
449
+ :param self: Represent the instance of a class
450
+ :param app_id: str: Specify the app_id of the application to update
451
+ :param custom_domain: str: Set the custom domain for the app
452
+ :return: A response object
453
+ """
454
+ route: Router = Router(
455
+ Endpoint.custom_domain(),
456
+ app_id=app_id,
457
+ custom_domain=custom_domain,
458
+ )
459
+ response: Response = await self.request(
460
+ route, custom_domain=custom_domain
461
+ )
462
+ return response
463
+
464
+ async def domain_analytics(self, app_id: str) -> Response:
465
+ """
466
+ The domain_analytics function returns a list of all domains analytics
467
+ for the specified app.
468
+
469
+ :param self: Represent the instance of a class
470
+ :param app_id: str: Specify the app_id for which you want to retrieve
471
+ analytics
472
+ :return: A response object
473
+ """
474
+ route: Router = Router(Endpoint.domain_analytics(), app_id=app_id)
475
+ response: Response = await self.request(route)
476
+ return response
File without changes