@@ -28,6 +28,20 @@ def delete_request(path, params={}, options={})
2828 request ( :delete , path , params , options )
2929 end
3030
31+ def retry_token_refresh_errors
32+ count = 0
33+ begin
34+ yield
35+ rescue BitBucket ::Error ::RefreshToken
36+ count += 1
37+ if count <= 3
38+ sleep 0.3 * count
39+ retry
40+ end
41+ raise
42+ end
43+ end
44+
3145 def request ( method , path , params , options = { } )
3246 if !METHODS . include? ( method )
3347 raise ArgumentError , "unkown http method: #{ method } "
@@ -36,25 +50,28 @@ def request(method, path, params, options={})
3650
3751 puts "EXECUTED: #{ method } - #{ path } with #{ params } and #{ options } " if ENV [ 'DEBUG' ]
3852
39- conn = connection ( options )
40- path_prefix = ( path . include? ( '/ssh' ) && BitBucket . options [ :bitbucket_server ] ) ? '/rest/keys' : conn . path_prefix
41- path = ( path_prefix + path ) . gsub ( /\/ \/ / , '/' ) if conn . path_prefix != '/'
42-
43- response = conn . send ( method ) do |request |
44- request [ 'Authorization' ] = "Bearer #{ new_access_token } " unless new_access_token . nil?
45- case method . to_sym
46- when *( METHODS - METHODS_WITH_BODIES )
47- request . body = params . delete ( 'data' ) if params . has_key? ( 'data' )
48- request . url ( path , params )
49- when *METHODS_WITH_BODIES
50- request . path = path
51- unless params . empty?
52- # data = extract_data_from_params(params)
53- # request.body = MultiJson.dump(data)
54- request . body = MultiJson . dump ( params )
53+ response = retry_token_refresh_errors do
54+ conn = connection ( options )
55+ path_prefix = ( path . include? ( '/ssh' ) && BitBucket . options [ :bitbucket_server ] ) ? '/rest/keys' : conn . path_prefix
56+ path = ( path_prefix + path ) . gsub ( /\/ \/ / , '/' ) if conn . path_prefix != '/'
57+
58+ response = conn . send ( method ) do |request |
59+ request [ 'Authorization' ] = "Bearer #{ new_access_token } " unless new_access_token . nil?
60+ case method . to_sym
61+ when *( METHODS - METHODS_WITH_BODIES )
62+ request . body = params . delete ( 'data' ) if params . has_key? ( 'data' )
63+ request . url ( path , params )
64+ when *METHODS_WITH_BODIES
65+ request . path = path
66+ unless params . empty?
67+ # data = extract_data_from_params(params)
68+ # request.body = MultiJson.dump(data)
69+ request . body = MultiJson . dump ( params )
70+ end
5571 end
5672 end
5773 end
74+
5875 response . body
5976 end
6077
0 commit comments