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
25 bk_logger
= logging
.getLogger('rerequests')
28 def rerequest(method
, url
, **kwargs
):
29 # first get any additional args from kwargs
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:
42 rdata
= response
.json()
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:
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
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
, (
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)
84 def get(url
, **kwargs
):
85 response
= rerequest('get', url
, **kwargs
)
89 def post(url
, **kwargs
):
90 response
= rerequest('post', url
, **kwargs
)
94 def put(url
, **kwargs
):
95 response
= rerequest('put', url
, **kwargs
)
99 def patch(url
, **kwargs
):
100 response
= rerequest('patch', url
, **kwargs
)