Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1from flask import request, current_app, session 

2from flask_restx import Resource 

3from flask_jwt_extended import jwt_required, get_jwt_identity, decode_token 

4 

5from threading import Thread 

6 

7# External service modules 

8from src.service import ExternalService 

9from src.dto import ExternalDto 

10from src.utils import validation_error, err_resp 

11 

12api = ExternalDto.api 

13oauth_external = ExternalDto.oauth_url 

14 

15 

16@api.route("/spotify") 

17class ExternalSpotifyResource(Resource): 

18 @api.doc( 

19 "Oauth2 Spotify", 

20 responses={ 

21 201: ("Successfully send", oauth_external), 

22 401: ("Authentication required"), 

23 404: "User not found!", 

24 } 

25 ) 

26 @jwt_required 

27 def get(self): 

28 """ Get oauth spotify """ 

29 user_uuid = get_jwt_identity() 

30 

31 return ExternalService.get_spotify_oauth(user_uuid) 

32 

33 

34@api.route("/spotify/callback") 

35class ExternalSpotifyCallbackResource(Resource): 

36 oauth_external = ExternalDto.oauth_spotify_callback 

37 @api.doc( 

38 "Spotify Oauth2 Callback", 

39 responses={ 

40 200 : ("Spotify oauth canceled"), 

41 201: ("Successfully received callback"), 

42 401: ("Authentication required"), 

43 404: "User not found!", 

44 } 

45 ) 

46 @api.expect(oauth_external, validate=True) 

47 @jwt_required 

48 def post(self): 

49 """ Get access and refresh tokens """ 

50 data = request.get_json() 

51 if "error" in data.keys(): 

52 return err_resp("Spotify oauth canceled", 200) 

53 

54 csrf = data['state'] 

55 user_uuid = get_jwt_identity() 

56 code = data['code'] 

57 

58 res,code = ExternalService.spotify_callback(csrf, code, user_uuid) 

59 if res['status']: 

60 thread = Thread(target=ExternalService.get_spotify_data, args=( 

61 user_uuid, current_app._get_current_object())) 

62 thread.daemon = True 

63 thread.start() 

64 return res,code 

65 

66@api.route("/tmdb") 

67class ExternalTmdbResource(Resource): 

68 @api.doc( 

69 "Oauth2 tmdb", 

70 responses={ 

71 201: ("Successfully send", oauth_external), 

72 401: ("Authentication required"), 

73 404: "User not found!", 

74 } 

75 ) 

76 @jwt_required 

77 def get(self): 

78 """ Get oauth tmdb """ 

79 user_uuid = get_jwt_identity() 

80 

81 return ExternalService.get_tmdb_oauth(user_uuid) 

82 

83@api.route("/tmdb/callback") 

84class ExternalTmdbCallbackResource(Resource): 

85 oauth_external = ExternalDto.oauth_tmdb_callback 

86 @api.doc( 

87 "tmdb Oauth2 Callback", 

88 responses={ 

89 200 : ("Authorization denied"), 

90 201: ("Successfully received callback"), 

91 401: ("Authentication required"), 

92 404: "User not found!", 

93 } 

94 ) 

95 

96 @api.expect(oauth_external, validate=True) 

97 @jwt_required 

98 def post(self): 

99 """ Get access and refresh tokens """ 

100 user_uuid = get_jwt_identity() 

101 

102 data=request.get_json() 

103 if 'denied' in data.keys(): 

104 return err_resp("Authorization denied", 200) 

105 request_token = data['request_token'] 

106 

107 res,code = ExternalService.tmdb_callback(request_token, user_uuid) 

108 if res['status'] : 

109 thread = Thread(target=ExternalService.get_tmdb_data, args=( 

110 user_uuid, current_app._get_current_object())) 

111 thread.daemon = True 

112 thread.start() 

113 return res,code 

114 

115@api.route("/gbooks") 

116class ExternalTmdbResource(Resource): 

117 @api.doc( 

118 "Oauth2 gbooks", 

119 responses={ 

120 201: ("Successfully send", oauth_external), 

121 401: ("Authentication required"), 

122 404: "User not found!", 

123 } 

124 ) 

125 @jwt_required 

126 def get(self): 

127 """ Get oauth gbooks """ 

128 user_uuid = get_jwt_identity() 

129 

130 return ExternalService.get_gbooks_oauth(user_uuid) 

131 

132@api.route("/gbooks/callback") 

133class ExternalTmdbCallbackResource(Resource): 

134 oauth_external = ExternalDto.oauth_gbooks_callback 

135 @api.doc( 

136 "gbooks Oauth2 Callback", 

137 responses={ 

138 200 : ("Authorization canceled by user"), 

139 201: ("Successfully received callback"), 

140 401: ("Authentication required"), 

141 404: "User not found!", 

142 } 

143 ) 

144 

145 @api.expect(oauth_external, validate=True) 

146 @jwt_required 

147 def post(self): 

148 """ Get access and refresh tokens """ 

149 user_uuid = get_jwt_identity() 

150 

151 data=request.get_json() 

152 state = data['state'] 

153 if ("error" in data.keys()) : 

154 return err_resp("Authorization canceled by user", 200) 

155 code = data['code'] 

156 

157 res,code = ExternalService.gbooks_callback(user_uuid, code, state) 

158 if res['status'] : 

159 thread = Thread(target=ExternalService.get_gbooks_data, args=( 

160 user_uuid, current_app._get_current_object())) 

161 thread.daemon = True 

162 thread.start() 

163 return res,code