Coverage for portality/view/api_v2.py: 89%

104 statements  

« prev     ^ index     » next       coverage.py v6.4.2, created at 2022-07-22 15:59 +0100

1from flask import Blueprint, url_for, redirect 

2 

3from portality.api.current import Api400Error 

4from portality.core import app 

5from portality.decorators import api_key_required, api_key_optional, write_required 

6from portality.lib import plausible 

7from portality.view import api_v3 

8 

9blueprint = Blueprint('api_v2', __name__) 

10 

11API_VERSION_NUMBER = '2.0.0' 

12 

13# Google Analytics category for API events 

14GA_CATEGORY = app.config.get('GA_CATEGORY_API', 'API Hit') 

15GA_ACTIONS = app.config.get('GA_ACTIONS_API', {}) 

16 

17API_UNSUPPORTED_ERROR = "Version 2 is no longer supported." 

18 

19# the API v2 is not supported anymore, this file handles api v3 requests: 

20# requests to articles are redirected to v3 

21# other requests raise the 400 Error 

22 

23####################################### 

24# ARTICLES 

25 

26 

27@blueprint.route('/') 

28def api_v2_root(): 

29 return redirect(url_for('api_v3.api_spec')) 

30 

31 

32@blueprint.route('/docs') 

33def docs(): 

34 return redirect(url_for('doaj.docs')) 

35 

36 

37@blueprint.route('/search/articles/<path:search_query>') 

38def search_articles(search_query): 

39 # Redirects are disabled https://github.com/DOAJ/doajPM/issues/2664 

40 # return redirect(url_for('api_v3.search_articles', search_query=search_query)) 

41 return api_v3.search_articles(search_query) 

42 

43 

44@blueprint.route("/articles", methods=["POST"]) 

45@api_key_required 

46@write_required(api=True) 

47@plausible.pa_event(GA_CATEGORY, action=GA_ACTIONS.get('create_article', 'Create article')) 

48def create_article(): 

49 # Redirects are disabled https://github.com/DOAJ/doajPM/issues/2664 

50 # return redirect(url_for('api_v3.create_article', **request.args), code=301) 

51 return api_v3.create_article() 

52 

53 

54@blueprint.route("/articles/<article_id>", methods=["GET"]) 

55@api_key_optional 

56@plausible.pa_event(GA_CATEGORY, action=GA_ACTIONS.get('retrieve_article', 'Retrieve article'), 

57 record_value_of_which_arg='article_id') 

58def retrieve_article(article_id): 

59 # Redirects are disabled https://github.com/DOAJ/doajPM/issues/2664 

60 # return redirect(url_for('api_v3.retrieve_article', article_id=article_id, **request.args), code=301) 

61 return api_v3.retrieve_article(article_id) 

62 

63 

64@blueprint.route("/articles/<article_id>", methods=["PUT"]) 

65@api_key_required 

66@write_required(api=True) 

67@plausible.pa_event(GA_CATEGORY, action=GA_ACTIONS.get('update_article', 'Update article'), 

68 record_value_of_which_arg='article_id') 

69def update_article(article_id): 

70 # Redirects are disabled https://github.com/DOAJ/doajPM/issues/2664 

71 # return redirect(url_for('api_v3.update_article', article_id=article_id, **request.args), code=301) 

72 return api_v3.update_article(article_id) 

73 

74 

75@blueprint.route("/articles/<article_id>", methods=["DELETE"]) 

76@api_key_required 

77@write_required(api=True) 

78@plausible.pa_event(GA_CATEGORY, action=GA_ACTIONS.get('delete_article', 'Delete article'), 

79 record_value_of_which_arg='article_id') 

80def delete_article(article_id): 

81 # Redirects are disabled https://github.com/DOAJ/doajPM/issues/2664 

82 # return redirect(url_for('api_v3.delete_article', article_id=article_id, **request.args), code=301) 

83 return api_v3.delete_article(article_id) 

84 

85 

86@blueprint.route("/bulk/articles", methods=["POST"]) 

87@api_key_required 

88@write_required(api=True) 

89@plausible.pa_event(GA_CATEGORY, action=GA_ACTIONS.get('bulk_article_create', 'Bulk article create')) 

90def bulk_article_create(): 

91 # Redirects are disabled https://github.com/DOAJ/doajPM/issues/2664 

92 # return redirect(url_for('api_v3.bulk_article_create', **request.args), code=301) 

93 return api_v3.bulk_article_create() 

94 

95 

96@blueprint.route("/bulk/articles", methods=["DELETE"]) 

97@api_key_required 

98@write_required(api=True) 

99@plausible.pa_event(GA_CATEGORY, action=GA_ACTIONS.get('bulk_article_delete', 'Bulk article delete')) 

100def bulk_article_delete(): 

101 # Redirects are disabled https://github.com/DOAJ/doajPM/issues/2664 

102 # return redirect(url_for('api_v3.bulk_article_delete', **request.args), code=301) 

103 return api_v3.bulk_article_delete() 

104 

105 

106####################################### 

107# OTHER 

108 

109@blueprint.route('/journals/<journal_id>', methods=['GET']) 

110@api_key_optional 

111@plausible.pa_event(GA_CATEGORY, action=GA_ACTIONS.get('retrieve_journal', 'Retrieve journal'), 

112 record_value_of_which_arg='journal_id') 

113def retrieve_journal(journal_id): 

114 # Redirects are disabled https://github.com/DOAJ/doajPM/issues/2664 

115 # return redirect(url_for('api_v3.retrieve_journal', journal_id=journal_id)) 

116 return api_v3.retrieve_journal(journal_id) 

117 

118 

119@blueprint.route("/bulk/applications", methods=["POST"]) 

120@api_key_required 

121@write_required(api=True) 

122@plausible.pa_event(GA_CATEGORY, action=GA_ACTIONS.get('bulk_application_create', 'Bulk application create')) 

123def bulk_application_create(): 

124 raise Api400Error(API_UNSUPPORTED_ERROR) 

125 

126 

127@blueprint.route("/bulk/applications", methods=["DELETE"]) 

128@api_key_required 

129@write_required(api=True) 

130@plausible.pa_event(GA_CATEGORY, action=GA_ACTIONS.get('bulk_application_delete', 'Bulk application delete')) 

131def bulk_application_delete(): 

132 raise Api400Error(API_UNSUPPORTED_ERROR) 

133 

134 

135@blueprint.route("/applications", methods=["POST"]) 

136@api_key_required 

137@write_required(api=True) 

138@plausible.pa_event(GA_CATEGORY, action=GA_ACTIONS.get('create_application', 'Create application')) 

139def create_application(): 

140 raise Api400Error(API_UNSUPPORTED_ERROR) 

141 

142 

143@blueprint.route("/applications/<application_id>", methods=["GET"]) 

144@api_key_required 

145@plausible.pa_event(GA_CATEGORY, action=GA_ACTIONS.get('retrieve_application', 'Retrieve application'), 

146 record_value_of_which_arg='application_id') 

147def retrieve_application(application_id): 

148 raise Api400Error(API_UNSUPPORTED_ERROR) 

149 

150 

151@blueprint.route("/applications/<application_id>", methods=["PUT"]) 

152@api_key_required 

153@write_required(api=True) 

154@plausible.pa_event(GA_CATEGORY, action=GA_ACTIONS.get('update_application', 'Update application'), 

155 record_value_of_which_arg='application_id') 

156def update_application(application_id): 

157 raise Api400Error(API_UNSUPPORTED_ERROR) 

158 

159 

160@blueprint.route("/applications/<application_id>", methods=["DELETE"]) 

161@api_key_required 

162@write_required(api=True) 

163@plausible.pa_event(GA_CATEGORY, action=GA_ACTIONS.get('delete_application', 'Delete application'), 

164 record_value_of_which_arg='application_id') 

165def delete_application(application_id): 

166 raise Api400Error(API_UNSUPPORTED_ERROR) 

167 

168 

169@blueprint.route("/search/applications/<path:search_query>") 

170@api_key_required 

171@plausible.pa_event(GA_CATEGORY, action=GA_ACTIONS.get('search_applications', 'Search applications'), 

172 record_value_of_which_arg='search_query') 

173def search_applications(search_query): 

174 # Redirects are disabled https://github.com/DOAJ/doajPM/issues/2664 

175 # return redirect(url_for('api_v3.search_applications', search_query=search_query)) 

176 return api_v3.search_applications(search_query) 

177 

178 

179@blueprint.route('/search/journals/<path:search_query>') 

180@plausible.pa_event(GA_CATEGORY, action=GA_ACTIONS.get('search_journals', 'Search journals'), 

181 record_value_of_which_arg='search_query') 

182def search_journals(search_query): 

183 # Redirects are disabled https://github.com/DOAJ/doajPM/issues/2664 

184 # return redirect(url_for('api_v3.search_journals', search_query=search_query)) 

185 return api_v3.search_journals(search_query)