Class: BitbucketServer::Connection

Inherits:
Object
  • Object
show all
Includes:
ActionView::Helpers::SanitizeHelper
Defined in:
lib/bitbucket_server/connection.rb

Constant Summary collapse

DEFAULT_API_VERSION =
'1.0'
SEPARATOR =
'/'
NETWORK_ERRORS =
[
  SocketError,
  OpenSSL::SSL::SSLError,
  Errno::ECONNRESET,
  Errno::ECONNREFUSED,
  Errno::EHOSTUNREACH,
  Net::OpenTimeout,
  Net::ReadTimeout,
  URI::InvalidURIError,
  Gitlab::HTTP::BlockedUrlError
].freeze
ConnectionError =
Class.new(StandardError)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Connection

Returns a new instance of Connection.



26
27
28
29
30
31
# File 'lib/bitbucket_server/connection.rb', line 26

def initialize(options = {})
  @api_version = options.fetch(:api_version, DEFAULT_API_VERSION)
  @base_uri = options[:base_uri]
  @username = options[:user]
  @token = options[:password]
end

Instance Attribute Details

#api_versionObject (readonly)

Returns the value of attribute api_version.



22
23
24
# File 'lib/bitbucket_server/connection.rb', line 22

def api_version
  @api_version
end

#base_uriObject (readonly)

Returns the value of attribute base_uri.



22
23
24
# File 'lib/bitbucket_server/connection.rb', line 22

def base_uri
  @base_uri
end

#tokenObject (readonly)

Returns the value of attribute token.



22
23
24
# File 'lib/bitbucket_server/connection.rb', line 22

def token
  @token
end

#usernameObject (readonly)

Returns the value of attribute username.



22
23
24
# File 'lib/bitbucket_server/connection.rb', line 22

def username
  @username
end

Instance Method Details

#delete(resource, path, body) ⇒ Object

We need to support two different APIs for deletion:

/rest/api/1.0/projects/projectKey/repos/repositorySlug/branches/default /rest/branch-utils/1.0/projects/projectKey/repos/repositorySlug/branches



63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/bitbucket_server/connection.rb', line 63

def delete(resource, path, body)
  url = delete_url(resource, path)

  response = Gitlab::HTTP.delete(url,
                                 basic_auth: auth,
                                 headers: post_headers,
                                 body: body)

  check_errors!(response)

  response.parsed_response
rescue *NETWORK_ERRORS => e
  raise ConnectionError, e
end

#get(path, extra_query = {}) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/bitbucket_server/connection.rb', line 33

def get(path, extra_query = {})
  response = Gitlab::HTTP.get(build_url(path),
                              basic_auth: auth,
                              headers: accept_headers,
                              query: extra_query)

  check_errors!(response)

  response.parsed_response
rescue *NETWORK_ERRORS => e
  raise ConnectionError, e
end

#post(path, body) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/bitbucket_server/connection.rb', line 46

def post(path, body)
  response = Gitlab::HTTP.post(build_url(path),
                               basic_auth: auth,
                               headers: post_headers,
                               body: body)

  check_errors!(response)

  response.parsed_response
rescue *NETWORK_ERRORS => e
  raise ConnectionError, e
end