Cleanup: simplify file name incrementing logic
[blender-addons.git] / blenderkit / rerequests.py
blob75d8fa277984002ed5165db1f4e9864989d0a3d8
1 # ##### BEGIN GPL LICENSE BLOCK #####
3 # This program is free software; you can redistribute it and/or
4 # modify it under the terms of the GNU General Public License
5 # as published by the Free Software Foundation; either version 2
6 # of the License, or (at your option) any later version.
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
13 # You should have received a copy of the GNU General Public License
14 # along with this program; if not, write to the Free Software Foundation,
15 # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 # ##### END GPL LICENSE BLOCK #####
20 from blenderkit import ui, utils, paths, tasks_queue, bkit_oauth
22 import requests
23 import bpy
24 import logging
25 bk_logger = logging.getLogger('rerequests')
28 def rerequest(method, url, **kwargs):
29 # first get any additional args from kwargs
30 immediate = False
31 if kwargs.get('immediate'):
32 immediate = kwargs['immediate']
33 kwargs.pop('immediate')
34 # first normal attempt
35 response = requests.request(method, url, **kwargs)
37 bk_logger.debug(url+ str( kwargs))
38 bk_logger.debug(response.status_code)
40 if response.status_code == 401:
41 try:
42 rdata = response.json()
43 except:
44 rdata = {}
46 tasks_queue.add_task((ui.add_report, (method + ' request Failed.' + str(rdata.get('detail')),)))
48 if rdata.get('detail') == 'Invalid token.':
49 user_preferences = bpy.context.preferences.addons['blenderkit'].preferences
50 if user_preferences.api_key != '':
51 if user_preferences.enable_oauth and user_preferences.api_key_refresh != '':
52 tasks_queue.add_task((ui.add_report, (
53 'refreshing token. If this fails, please login in BlenderKit Login panel.', 10)))
54 refresh_url = paths.get_bkit_url()
55 auth_token, refresh_token, oauth_response = bkit_oauth.refresh_token(
56 user_preferences.api_key_refresh, refresh_url)
58 # bk_logger.debug(auth_token, refresh_token)
59 if auth_token is not None:
60 if immediate == True:
61 # this can write tokens occasionally into prefs. used e.g. in upload. Only possible
62 # in non-threaded tasks
63 bpy.context.preferences.addons['blenderkit'].preferences.api_key = auth_token
64 bpy.context.preferences.addons['blenderkit'].preferences.api_key_refresh = refresh_token
65 else:
66 tasks_queue.add_task((bkit_oauth.write_tokens, (auth_token, refresh_token, oauth_response)))
68 kwargs['headers'] = utils.get_headers(auth_token)
69 response = requests.request(method, url, **kwargs)
70 bk_logger.debug('reresult', response.status_code)
71 if response.status_code >= 400:
72 bk_logger.debug('reresult', response.text)
73 tasks_queue.add_task((ui.add_report, (
74 response.text, 10)))
76 else:
77 tasks_queue.add_task((ui.add_report, (
78 'Refreshing token failed.Please login manually.', 10)))
79 # tasks_queue.add_task((bkit_oauth.write_tokens, ('', '', '')))
80 tasks_queue.add_task((bpy.ops.wm.blenderkit_login,( 'INVOKE_DEFAULT',)),fake_context = True)
81 return response
84 def get(url, **kwargs):
85 response = rerequest('get', url, **kwargs)
86 return response
89 def post(url, **kwargs):
90 response = rerequest('post', url, **kwargs)
91 return response
94 def put(url, **kwargs):
95 response = rerequest('put', url, **kwargs)
96 return response
99 def patch(url, **kwargs):
100 response = rerequest('patch', url, **kwargs)
101 return response