Coverage for webapp/integrations/views.py: 41%

49 statements  

« prev     ^ index     » next       coverage.py v7.10.2, created at 2025-08-08 22:07 +0000

1from datetime import datetime 

2from flask import ( 

3 Blueprint, 

4 render_template, 

5 abort, 

6) 

7from flask.json import jsonify 

8 

9from webapp.integrations.logic import interface_logic 

10from webapp.observability.utils import trace_function 

11from webapp.store_api import publisher_gateway 

12 

13 

14integrations = Blueprint( 

15 "integrations", 

16 __name__, 

17 template_folder="/templates", 

18 static_folder="/static", 

19) 

20 

21 

22@trace_function 

23@integrations.route("/integrations.json") 

24def interfaces_json(): 

25 interfaces = interface_logic.get_interfaces() 

26 

27 response = { 

28 "interfaces": interfaces, 

29 "size": len(interfaces), 

30 } 

31 

32 return response 

33 

34 

35@trace_function 

36@integrations.route("/integrations") 

37def all_interfaces(): 

38 interfaces = interface_logic.get_interfaces() 

39 context = {"interfaces": interfaces} 

40 return render_template("interfaces/index.html", **context) 

41 

42 

43def fetch_interface_details(interface_name): 

44 other_requirers = publisher_gateway.find(requires=[interface_name]).get( 

45 "results", [] 

46 ) 

47 other_providers = publisher_gateway.find(provides=[interface_name]).get( 

48 "results", [] 

49 ) 

50 

51 res = { 

52 "other_charms": { 

53 "providers": other_providers, 

54 "requirers": other_requirers, 

55 } 

56 } 

57 

58 interface = interface_logic.get_interface_from_path(interface_name) 

59 if interface is None: 

60 if other_providers or other_requirers: 

61 return res 

62 return None 

63 

64 readme_path = ( 

65 f"interfaces/{interface_name}/v{interface['version']}/README.md" 

66 ) 

67 readme_contentfile = interface_logic.repo.get_contents(readme_path) 

68 readme = readme_contentfile.decoded_content.decode("utf-8") 

69 last_modified = datetime.strptime( 

70 readme_contentfile.last_modified, "%a, %d %b %Y %H:%M:%S %Z" 

71 ).isoformat() 

72 

73 res.update( 

74 { 

75 "body": interface_logic.convert_readme( 

76 interface_name, f"v{interface['version']}", readme, 2 

77 ), 

78 "name": interface_logic.get_interface_name_from_readme(readme), 

79 "charms": { 

80 "providers": interface["providers"], 

81 "requirers": interface["requirers"], 

82 }, 

83 "last_modified": last_modified, 

84 "status": interface["status"], 

85 "version": interface["version"], 

86 } 

87 ) 

88 

89 return res 

90 

91 

92@trace_function 

93@integrations.route("/integrations/<path:path>") 

94def get_single_interface(path): 

95 interface_data = fetch_interface_details(path) 

96 

97 if interface_data is None: 

98 return abort(404) 

99 

100 context = {"interface": interface_data} 

101 return render_template("interfaces/index.html", **context) 

102 

103 

104@trace_function 

105@integrations.route("/integrations/<interface_name>.json") 

106def get_single_interface_json(interface_name): 

107 interface_data = fetch_interface_details(interface_name) 

108 

109 if interface_data is None: 

110 return jsonify({"error": "Interface not found"}), 404 

111 

112 return jsonify(interface_data)