Skip to content

Commit 5a28ec4

Browse files
committed
Added client documentation.
1 parent c09314c commit 5a28ec4

File tree

4 files changed

+168
-1
lines changed

4 files changed

+168
-1
lines changed

lib/jira/client.rb

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,41 +286,82 @@ def Agile
286286
end
287287

288288
# HTTP methods without a body
289+
290+
# Make an HTTP DELETE request
291+
# @param [String] path The path to request
292+
# @param [Hash] headers The headers to send with the request
293+
# @return [Net::HTTPResponse] The response object
294+
# @raise [JIRA::HTTPError] If the response is not an HTTP success code
289295
def delete(path, headers = {})
290296
request(:delete, path, nil, merge_default_headers(headers))
291297
end
292298

299+
# Make an HTTP GET request
300+
# @param [String] path The path to request
301+
# @param [Hash] headers The headers to send with the request
302+
# @return [Net::HTTPResponse] The response object
303+
# @raise [JIRA::HTTPError] If the response is not an HTTP success code
293304
def get(path, headers = {})
294305
request(:get, path, nil, merge_default_headers(headers))
295306
end
296307

308+
# Make an HTTP HEAD request
309+
# @param [String] path The path to request
310+
# @param [Hash] headers The headers to send with the request
311+
# @return [Net::HTTPResponse] The response object
312+
# @raise [JIRA::HTTPError] If the response is not an HTTP success code
297313
def head(path, headers = {})
298314
request(:head, path, nil, merge_default_headers(headers))
299315
end
300316

301317
# HTTP methods with a body
318+
319+
# Make an HTTP POST request
320+
# @param [String] path The path to request
321+
# @param [String] body The body of the request
322+
# @param [Hash] headers The headers to send with the request
323+
# @return [Net::HTTPResponse] The response object
302324
def post(path, body = '', headers = {})
303325
headers = { 'Content-Type' => 'application/json' }.merge(headers)
304326
request(:post, path, body, merge_default_headers(headers))
305327
end
306328

329+
# Make an HTTP POST request with a file upload
330+
# @param [String] path The path to request
331+
# @param [String] file The file to upload
332+
# @param [Hash] headers The headers to send with the request
333+
# @return [Net::HTTPResponse] The response object
334+
# @raise [JIRA::HTTPError] If the response is not an HTTP success code
307335
def post_multipart(path, file, headers = {})
308336
puts "post multipart: #{path} - [#{file}]" if @http_debug
309337
@request_client.request_multipart(path, file, merge_default_headers(headers))
310338
end
311339

340+
# Make an HTTP PUT request
341+
# @param [String] path The path to request
342+
# @param [String] body The body of the request
343+
# @param [Hash] headers The headers to send with the request
344+
# @return [Net::HTTPResponse] The response object
345+
# @raise [JIRA::HTTPError] If the response is not an HTTP success code
312346
def put(path, body = '', headers = {})
313347
headers = { 'Content-Type' => 'application/json' }.merge(headers)
314348
request(:put, path, body, merge_default_headers(headers))
315349
end
316350

317351
# Sends the specified HTTP request to the REST API through the
318352
# appropriate method (oauth, basic).
353+
# @param [Symbol] http_method The HTTP method to use
354+
# @param [String] path The path to request
355+
# @param [String] body The body of the request
356+
# @param [Hash] headers The headers to send with the request
357+
# @return [Net::HTTPResponse] The response object
358+
# @raise [JIRA::HTTPError] If the response is not an HTTP success code
319359
def request(http_method, path, body = '', headers = {})
320360
puts "#{http_method}: #{path} - [#{body}]" if @http_debug
321361
@request_client.request(http_method, path, body, headers)
322362
end
323363

364+
# @private
324365
# Stops sensitive client information from being displayed in logs
325366
def inspect
326367
"#<JIRA::Client:#{object_id}>"

lib/jira/http_client.rb

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,25 @@
66
require 'uri'
77

88
module JIRA
9+
# Client using HTTP Basic Authentication
910
class HttpClient < RequestClient
11+
# @private
1012
DEFAULT_OPTIONS = {
1113
username: nil,
1214
password: nil
1315
}.freeze
1416

17+
# @!attribute [r] options
18+
# @return [Hash] The client options
1519
attr_reader :options
1620

21+
# @param [Hash] options Options as passed from JIRA::Client constructor.
22+
# @option options [String] :username The username to authenticate with
23+
# @option options [String] :password The password to authenticate with
24+
# @option options [Hash] :default_headers Additional headers for requests
25+
# @option options [String] :proxy_uri Proxy URI
26+
# @option options [String] :proxy_user Proxy user
27+
# @option options [String] :proxy_password Proxy Password
1728
def initialize(options)
1829
@options = DEFAULT_OPTIONS.merge(options)
1930
@cookies = {}
@@ -26,6 +37,18 @@ def make_cookie_auth_request
2637
make_request(:post, "#{@options[:context_path]}/rest/auth/1/session", body, 'Content-Type' => 'application/json')
2738
end
2839

40+
# Makes a request to the JIRA server.
41+
#
42+
# Generally you should not call this method directly, but use the helper methods in JIRA::Client.
43+
#
44+
# File uploads are not supported with this method. Use make_multipart_request instead.
45+
#
46+
# @param [Symbol] http_method The HTTP method to use
47+
# @param [String] url The JIRA REST URL to call
48+
# @param [String] body The request body
49+
# @param [Hash] headers Additional headers to send with the request
50+
# @return [Net::HTTPResponse] The response from the server
51+
# @raise [JIRA::HTTPError] If the response is not an HTTP success code
2952
def make_request(http_method, url, body = '', headers = {})
3053
# When a proxy is enabled, Net::HTTP expects that the request path omits the domain name
3154
path = request_path(url)
@@ -35,17 +58,30 @@ def make_request(http_method, url, body = '', headers = {})
3558
execute_request(request)
3659
end
3760

61+
# Makes a multipart request to the JIRA server.
62+
#
63+
# This is used for file uploads.
64+
#
65+
# Generally you should not call this method directly, but use the helper methods in JIRA::Client.
66+
#
67+
# @param [String] url The JIRA REST URL to call
68+
# @param [Hash] body The Net::HTTP::Post::Multipart data to send with the request
69+
# @param [Hash] headers The headers to send with the request
70+
# @return [Net::HTTPResponse] The response object
71+
# @raise [JIRA::HTTPError] If the response is not an HTTP success code
3872
def make_multipart_request(url, body, headers = {})
3973
path = request_path(url)
4074
request = Net::HTTP::Post::Multipart.new(path, body, headers)
4175

4276
execute_request(request)
4377
end
4478

79+
# @private
4580
def basic_auth_http_conn
4681
http_conn(uri)
4782
end
4883

84+
# @private
4985
def http_conn(uri)
5086
http_conn =
5187
if @options[:proxy_address]
@@ -67,10 +103,14 @@ def http_conn(uri)
67103
http_conn
68104
end
69105

106+
# The URI of the JIRA REST API call
107+
# @return [URI] The URI of the JIRA REST API call
70108
def uri
71109
URI.parse(@options[:site])
72110
end
73111

112+
# Returns true if the client is authenticated.
113+
# @return [Boolean] True if the client is authenticated
74114
def authenticated?
75115
@authenticated
76116
end

lib/jira/oauth_client.rb

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,16 @@
55
require 'forwardable'
66

77
module JIRA
8+
# Client using OAuth 1.0
9+
#
10+
# @!attribute [rw] consumer
11+
# @return [OAuth::Consumer] The oauth consumer object
12+
# @!attribute [r] options
13+
# @return [Hash] The oauth connection options
14+
# @!attribute [r] access_token
15+
# @return [OAuth::AccessToken] The oauth access token
816
class OauthClient < RequestClient
17+
# @private
918
DEFAULT_OPTIONS = {
1019
signature_method: 'RSA-SHA1',
1120
request_token_path: '/plugins/servlet/oauth/request-token',
@@ -31,11 +40,28 @@ def message
3140

3241
def_instance_delegators :@consumer, :key, :secret, :get_request_token
3342

43+
# @param [Hash] options Options as passed from JIRA::Client constructor.
44+
# @option options [String] :signature_method The signature method to use (defaults to 'RSA-SHA1')
45+
# @option options [String] :request_token_path The path to request a token (defaults to '/plugins/servlet/oauth/request-token')
46+
# @option options [String] :authorize_path The path to authorize a token (defaults to '/plugins/servlet/oauth/authorize')
47+
# @option options [String] :access_token_path The path to access a token (defaults to '/plugins/servlet/oauth/access-token')
48+
# @option options [String] :private_key_file The path to the private key file
49+
# @option options [String] :consumer_key The OAuth 1.0 consumer key
50+
# @option options [String] :consumer_secret The OAuth 1.0 consumer secret
51+
# @option options [Hash] :default_headers Additional headers for requests
52+
# @option options [String] :proxy_uri Proxy URI
53+
# @option options [String] :proxy_user Proxy user
54+
# @option options [String] :proxy_password Proxy Password
3455
def initialize(options)
3556
@options = DEFAULT_OPTIONS.merge(options)
3657
@consumer = init_oauth_consumer(@options)
3758
end
3859

60+
# @private
61+
# Initialises the OAuth consumer object.
62+
# Generally you should not call this method directly, it is called by the constructor.
63+
# @param [Hash] _options The options hash
64+
# @return [OAuth::Consumer] The OAuth consumer object
3965
def init_oauth_consumer(_options)
4066
@options[:request_token_path] = @options[:context_path] + @options[:request_token_path]
4167
@options[:authorize_path] = @options[:context_path] + @options[:authorize_path]
@@ -47,22 +73,31 @@ def init_oauth_consumer(_options)
4773

4874
# Returns the current request token if it is set, else it creates
4975
# and sets a new token.
76+
# @param [Hash] options
5077
def request_token(options = {}, ...)
5178
@request_token ||= get_request_token(options, ...)
5279
end
5380

5481
# Sets the request token from a given token and secret.
82+
# @param [String] token The request token
83+
# @param [String] secret The request token secret
84+
# @return [OAuth::RequestToken] The request token object
5585
def set_request_token(token, secret)
5686
@request_token = OAuth::RequestToken.new(@consumer, token, secret)
5787
end
5888

5989
# Initialises and returns a new access token from the params hash
6090
# returned by the OAuth transaction.
91+
# @param [Hash] params The params hash returned by the OAuth transaction
92+
# @return [OAuth::AccessToken] The access token object
6193
def init_access_token(params)
6294
@access_token = request_token.get_access_token(params)
6395
end
6496

6597
# Sets the access token from a preexisting token and secret.
98+
# @param [String] token The access token
99+
# @param [String] secret The access token secret
100+
# @return [OAuth::AccessToken] The access token object
66101
def set_access_token(token, secret)
67102
@access_token = OAuth::AccessToken.new(@consumer, token, secret)
68103
@authenticated = true
@@ -71,12 +106,25 @@ def set_access_token(token, secret)
71106

72107
# Returns the current access token. Raises an
73108
# JIRA::Client::UninitializedAccessTokenError exception if it is not set.
109+
# @return [OAuth::AccessToken] The access token object
74110
def access_token
75111
raise UninitializedAccessTokenError unless @access_token
76112

77113
@access_token
78114
end
79115

116+
# Makes a request to the JIRA server using the oauth gem.
117+
#
118+
# Generally you should not call this method directly, but use the helper methods in JIRA::Client.
119+
#
120+
# File uploads are not supported with this method. Use make_multipart_request instead.
121+
#
122+
# @param [Symbol] http_method The HTTP method to use
123+
# @param [String] url The JIRA REST URL to call
124+
# @param [String] body The body of the request
125+
# @param [Hash] headers The headers to send with the request
126+
# @return [Net::HTTPResponse] The response object
127+
# @raise [JIRA::HTTPError] If the response is not an HTTP success code
80128
def make_request(http_method, url, body = '', headers = {})
81129
# When using oauth_2legged we need to add an empty oauth_token parameter to every request.
82130
if @options[:auth_type] == :oauth_2legged
@@ -100,6 +148,17 @@ def make_request(http_method, url, body = '', headers = {})
100148
response
101149
end
102150

151+
# Makes a multipart request to the JIRA server using the oauth gem.
152+
#
153+
# This is used for file uploads.
154+
#
155+
# Generally you should not call this method directly, but use the helper methods in JIRA::Client.
156+
#
157+
# @param [String] url The JIRA REST URL to call
158+
# @param [Hash] data The Net::HTTP::Post::Multipart data to send with the request
159+
# @param [Hash] headers The headers to send with the request
160+
# @return [Net::HTTPResponse] The response object
161+
# @raise [JIRA::HTTPError] If the response is not an HTTP success code
103162
def make_multipart_request(url, data, headers = {})
104163
request = Net::HTTP::Post::Multipart.new url, data, headers
105164

@@ -110,6 +169,8 @@ def make_multipart_request(url, data, headers = {})
110169
response
111170
end
112171

172+
# Returns true if the client is authenticated.
173+
# @return [Boolean] True if the client is authenticated
113174
def authenticated?
114175
@authenticated
115176
end

lib/jira/request_client.rb

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,29 +5,54 @@
55
require 'net/https'
66

77
module JIRA
8+
# Base class for request clients specific to a particular authentication method.
89
class RequestClient
10+
# Makes the JIRA REST API call.
11+
#
912
# Returns the response if the request was successful (HTTP::2xx) and
1013
# raises a JIRA::HTTPError if it was not successful, with the response
1114
# attached.
12-
15+
#
16+
# Generally you should not call this method directly, but use derived classes.
17+
#
18+
# File uploads are not supported with this method. Use request_multipart instead.
19+
#
20+
# @param [Array] args Arguments to pass to the request method
21+
# @return [Net::HTTPResponse] The response from the server
22+
# @raise [JIRA::HTTPError] if it was not successful
1323
def request(*args)
1424
response = make_request(*args)
1525
raise HTTPError, response unless response.is_a?(Net::HTTPSuccess)
1626

1727
response
1828
end
1929

30+
# Makes a multipart request to the JIRA server.
31+
#
32+
# This is used for file uploads.
33+
#
34+
# Generally you should not call this method directly, but use derived classes.
35+
#
36+
# @param [Array] args Arguments to pass to the request method
37+
# @return [Net::HTTPResponse] The response from the server
38+
# @raise [JIRA::HTTPError] if it was not successful
2039
def request_multipart(*args)
2140
response = make_multipart_request(*args)
2241
raise HTTPError, response unless response.is_a?(Net::HTTPSuccess)
2342

2443
response
2544
end
2645

46+
# Abstract method to make a request to the JIRA server.
47+
# @abstract
48+
# @param [Array] args Arguments to pass to the request method
2749
def make_request(*args)
2850
raise NotImplementedError
2951
end
3052

53+
# Abstract method to make a request to the JIRA server with a file upload.
54+
# @abstract
55+
# @param [Array] args Arguments to pass to the request method
3156
def make_multipart_request(*args)
3257
raise NotImplementedError
3358
end

0 commit comments

Comments
 (0)