Module: BitBucket::Request

Included in:
API
Defined in:
lib/bitbucket_rest_api/request.rb,
lib/bitbucket_rest_api/request/oauth.rb,
lib/bitbucket_rest_api/request/basic_auth.rb

Overview

Defines HTTP verbs

Defined Under Namespace

Classes: BasicAuth, Jsonize, OAuth

Constant Summary collapse

METHODS =
[:get, :post, :put, :delete, :patch]
METHODS_WITH_BODIES =
[ :post, :put, :patch ]

Instance Method Summary collapse

Instance Method Details

#delete_request(path, params = {}, options = {}) ⇒ Object



27
28
29
# File 'lib/bitbucket_rest_api/request.rb', line 27

def delete_request(path, params={}, options={})
  request(:delete, path, params, options)
end

#get_request(path, params = {}, options = {}) ⇒ Object



10
11
12
# File 'lib/bitbucket_rest_api/request.rb', line 10

def get_request(path, params={}, options={})
  request(:get, path, params, options)
end

#patch_request(path, params = {}, options = {}) ⇒ Object



15
16
17
# File 'lib/bitbucket_rest_api/request.rb', line 15

def patch_request(path, params={}, options={})
  request(:patch, path, params, options)
end

#post_request(path, params = {}, options = {}) ⇒ Object



19
20
21
# File 'lib/bitbucket_rest_api/request.rb', line 19

def post_request(path, params={}, options={})
  request(:post, path, params, options)
end

#put_request(path, params = {}, options = {}) ⇒ Object



23
24
25
# File 'lib/bitbucket_rest_api/request.rb', line 23

def put_request(path, params={}, options={})
  request(:put, path, params, options)
end

#request(method, path, params, options = {}) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/bitbucket_rest_api/request.rb', line 45

def request(method, path, params, options={})
  if !METHODS.include?(method)
    raise ArgumentError, "unkown http method: #{method}"
  end
  # _extract_mime_type(params, options)

  puts "EXECUTED: #{method} - #{path} with #{params} and #{options}" if ENV['DEBUG']

  response = retry_token_refresh_errors do
    conn = connection(options)
    prefix = path_prefix(path, conn)
    path = (prefix + path).gsub(/\/\//,'/') if conn.path_prefix != '/'

    response = conn.send(method) do |request|
      request['Authorization'] = "Bearer #{new_access_token}" unless new_access_token.nil?
      case method.to_sym
      when *(METHODS - METHODS_WITH_BODIES)
        request.body = params.delete('data') if params.has_key?('data')
        request.url(path, params)
      when *METHODS_WITH_BODIES
        request.path = path
        unless params.empty?
          # data = extract_data_from_params(params)
          # request.body = MultiJson.dump(data)
          request.body = MultiJson.dump(params)
        end
      end
    end
  end

  response.body
end

#retry_token_refresh_errorsObject



31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/bitbucket_rest_api/request.rb', line 31

def retry_token_refresh_errors
  count = 0
  begin
    yield
  rescue BitBucket::Error::RefreshToken
    count += 1
    if count <= 3
      sleep 0.3 * count
      retry
    end
    raise
  end
end