Class: JIRA::HttpClient

Inherits:
RequestClient show all
Defined in:
lib/jira/http_client.rb

Overview

Client using HTTP Basic Authentication

Examples:

Basic authentication

options = {
  auth_type:        :basic,
  site:             "https://jira.example.com",
  use_ssl:          true,
  ssl_verify_mode:  OpenSSL::SSL::VERIFY_PEER,
  cert_path:        '/usr/local/etc/trusted-certificates.pem',
  username:         'jamie',
  password:         'password'
}
client = JIRA::Client.new(options)

Bearer token authentication

options = {
  auth_type:        :basic,
  site:             "https://jira.example.com",
  default_headers:  { 'authorization' => "Bearer #{bearer_token_str}" },
  use_ssl:          true,
  ssl_verify_mode:  OpenSSL::SSL::VERIFY_PEER
  cert_path:        '/usr/local/etc/trusted-certificates.pem',
}
client = JIRA::Client.new(options)

Direct Known Subclasses

JwtClient

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from RequestClient

#request, #request_multipart

Constructor Details

#initialize(options) ⇒ HttpClient

Generally not used directly, but through JIRA::Client. See JIRA::Client for documentation.

Parameters:

  • options (Hash)

    Options as passed from JIRA::Client constructor.

Options Hash (options):

  • :username (String)

    The username to authenticate with

  • :password (String)

    The password to authenticate with

  • :default_headers (Hash)

    Additional headers for requests

  • :proxy_uri (String)

    Proxy URI

  • :proxy_user (String)

    Proxy user

  • :proxy_password (String)

    Proxy Password



51
52
53
54
# File 'lib/jira/http_client.rb', line 51

def initialize(options)
  @options = DEFAULT_OPTIONS.merge(options)
  @cookies = {}
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



40
41
42
# File 'lib/jira/http_client.rb', line 40

def options
  @options
end

Instance Method Details

#authenticated?Boolean

Returns true if the client is authenticated.

Returns:

  • (Boolean)

    True if the client is authenticated



137
138
139
# File 'lib/jira/http_client.rb', line 137

def authenticated?
  @authenticated
end


56
57
58
59
60
61
# File 'lib/jira/http_client.rb', line 56

def make_cookie_auth_request
  body = { username: @options[:username].to_s, password: @options[:password].to_s }.to_json
  @options.delete(:username)
  @options.delete(:password)
  make_request(:post, "#{@options[:context_path]}/rest/auth/1/session", body, 'Content-Type' => 'application/json')
end

#make_multipart_request(url, body, headers = {}) ⇒ Net::HTTPResponse

Makes a multipart request to the JIRA server.

This is used for file uploads.

Generally you should not call this method directly, but use the helper methods in JIRA::Client.

Parameters:

  • url (String)

    The JIRA REST URL to call

  • body (Hash)

    The Net::HTTP::Post::Multipart data to send with the request

  • headers (Hash) (defaults to: {})

    The headers to send with the request

Returns:

  • (Net::HTTPResponse)

    The response object

Raises:



95
96
97
98
99
100
# File 'lib/jira/http_client.rb', line 95

def make_multipart_request(url, body, headers = {})
  path = request_path(url)
  request = Net::HTTP::Post::Multipart.new(path, body, headers)

  execute_request(request)
end

#make_request(http_method, url, body = '', headers = {}) ⇒ Net::HTTPResponse

Makes a request to the JIRA server.

Generally you should not call this method directly, but use the helper methods in JIRA::Client.

File uploads are not supported with this method. Use make_multipart_request instead.

Parameters:

  • http_method (Symbol)

    The HTTP method to use

  • url (String)

    The JIRA REST URL to call

  • body (String) (defaults to: '')

    The request body

  • headers (Hash) (defaults to: {})

    Additional headers to send with the request

Returns:

  • (Net::HTTPResponse)

    The response from the server

Raises:



75
76
77
78
79
80
81
82
# File 'lib/jira/http_client.rb', line 75

def make_request(http_method, url, body = '', headers = {})
  # When a proxy is enabled, Net::HTTP expects that the request path omits the domain name
  path = request_path(url)
  request = Net::HTTP.const_get(http_method.to_s.capitalize).new(path, headers)
  request.body = body unless body.nil?

  execute_request(request)
end

#uriURI

The URI of the JIRA REST API call

Returns:

  • (URI)

    The URI of the JIRA REST API call



131
132
133
# File 'lib/jira/http_client.rb', line 131

def uri
  URI.parse(@options[:site])
end