Coverage for seedboxsync/front/apis/__init__.py: 93%

29 statements  

« prev     ^ index     » next       coverage.py v7.16.2, created at 2026-10-11 07:37 +0000

1# 

2# Copyright (C) 2015-2026 Guillaume Kulakowski <guillaume@kulakowski.fr> 

3# 

4# For the full copyright and license information, please view the LICENSE 

5# file that was distributed with this source code. 

6# 

7"""SeedboxSync api package.""" 

8 

9from flask import Blueprint 

10from flask_restx import Api 

11from seedboxsync.__version__ import ( 

12 __api_path_version__ as api_path_version, 

13 __api_version__ as api_version, 

14) 

15from seedboxsync.core import Flask 

16 

17from seedboxsync.front.apis.core.resources import parser_period, DateTimeOrZero, Resource # isort: skip 

18from seedboxsync.front.apis.downloads import api as nsDownloads 

19from seedboxsync.front.apis.downloads_stats import api as nsDownloadsStats 

20from seedboxsync.front.apis.tasks import api as nsTasks 

21from seedboxsync.front.apis.taskstatus import api as nsTaskStatus 

22from seedboxsync.front.apis.uploads import api as nsUploads 

23from seedboxsync.front.apis.uploads_stats import api as nsUploadsStats 

24from seedboxsync.front.apis.users import api as nsUsers 

25 

26bp = Blueprint("api", __name__, url_prefix=f"/api/{api_path_version}") 

27 

28api = Api( 

29 title="SeedboxSync API", 

30 version=api_version, 

31 description="REST API providing access to the SeedboxSync database and its resources.", 

32 validate=True, 

33) 

34 

35# Add namespaces 

36api.add_namespace(nsDownloads) 

37api.add_namespace(nsDownloadsStats) 

38api.add_namespace(nsUploads) 

39api.add_namespace(nsUploadsStats) 

40api.add_namespace(nsTasks) 

41api.add_namespace(nsTaskStatus) 

42api.add_namespace(nsUsers) 

43 

44__all__ = ["DateTimeOrZero", "Resource", "parser_period"] 

45 

46 

47def register_api_blueprint(app: Flask) -> None: 

48 """ 

49 Configure OpenAPI/Swagger security definitions for Flask-RESTX and register Blueprint. 

50 

51 Registers HTTP Basic Authentication schemes in the Swagger UI 

52 documentation unless authentication is globally disabled. 

53 

54 Args: 

55 app (Flask): The Flask application instance containing configuration flags. 

56 """ 

57 if not app.config.get("LOGIN_DISABLED", False): 

58 api.authorizations = { 

59 "basicAuth": { 

60 "type": "basic", 

61 "description": "Basic authentication with username and password", 

62 }, 

63 "apiKey": { 

64 "type": "apiKey", 

65 "in": "header", 

66 "name": "X-API-Key", 

67 "description": "API key authentication (e.g. 'X-API-Key: sbx_...')", 

68 }, 

69 } 

70 api.security = ["basicAuth", "apiKey"] 

71 

72 if api.blueprint is None: 

73 api.init_app(bp) 

74 app.register_blueprint(bp)