reenhanced_bitbucket_api 0.1.6 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/bitbucket_rest_api/api/arguments.rb +248 -0
- data/lib/bitbucket_rest_api/api/config/property.rb +30 -0
- data/lib/bitbucket_rest_api/api/config/property_set.rb +118 -0
- data/lib/bitbucket_rest_api/api/config.rb +107 -0
- data/lib/bitbucket_rest_api/api/factory.rb +29 -0
- data/lib/bitbucket_rest_api/api.rb +155 -33
- data/lib/bitbucket_rest_api/{invitations.rb → client/invitations.rb} +4 -3
- data/lib/bitbucket_rest_api/{issues → client/issues}/comments.rb +2 -11
- data/lib/bitbucket_rest_api/{issues → client/issues}/components.rb +3 -6
- data/lib/bitbucket_rest_api/{issues → client/issues}/milestones.rb +3 -7
- data/lib/bitbucket_rest_api/{issues.rb → client/issues.rb} +9 -25
- data/lib/bitbucket_rest_api/{repos → client/repos}/changesets.rb +3 -2
- data/lib/bitbucket_rest_api/{repos → client/repos}/following.rb +2 -1
- data/lib/bitbucket_rest_api/{repos → client/repos}/keys.rb +2 -1
- data/lib/bitbucket_rest_api/client/repos/pull_requests/activity.rb +22 -0
- data/lib/bitbucket_rest_api/client/repos/pull_requests/comments.rb +40 -0
- data/lib/bitbucket_rest_api/client/repos/pull_requests/commits.rb +24 -0
- data/lib/bitbucket_rest_api/client/repos/pull_requests.rb +205 -0
- data/lib/bitbucket_rest_api/{repos → client/repos}/services.rb +2 -1
- data/lib/bitbucket_rest_api/{repos → client/repos}/sources.rb +2 -1
- data/lib/bitbucket_rest_api/{repos.rb → client/repos.rb} +26 -40
- data/lib/bitbucket_rest_api/{user.rb → client/user.rb} +2 -7
- data/lib/bitbucket_rest_api/{users → client/users}/account.rb +2 -1
- data/lib/bitbucket_rest_api/client/users.rb +14 -0
- data/lib/bitbucket_rest_api/client.rb +28 -32
- data/lib/bitbucket_rest_api/configuration.rb +24 -67
- data/lib/bitbucket_rest_api/connection.rb +15 -50
- data/lib/bitbucket_rest_api/constants.rb +1 -9
- data/lib/bitbucket_rest_api/core_ext/array.rb +1 -1
- data/lib/bitbucket_rest_api/error/invalid_options.rb +1 -1
- data/lib/bitbucket_rest_api/error/required_params.rb +1 -1
- data/lib/bitbucket_rest_api/error/unknown_value.rb +1 -1
- data/lib/bitbucket_rest_api/ext/faraday.rb +38 -0
- data/lib/bitbucket_rest_api/middleware.rb +31 -0
- data/lib/bitbucket_rest_api/null_encoder.rb +25 -0
- data/lib/bitbucket_rest_api/page_iterator.rb +90 -0
- data/lib/bitbucket_rest_api/page_links.rb +33 -0
- data/lib/bitbucket_rest_api/paged_request.rb +29 -0
- data/lib/bitbucket_rest_api/pagination.rb +98 -0
- data/lib/bitbucket_rest_api/parameter_filter.rb +1 -1
- data/lib/bitbucket_rest_api/params_hash.rb +100 -0
- data/lib/bitbucket_rest_api/request/basic_auth.rb +4 -2
- data/lib/bitbucket_rest_api/request/jsonize.rb +5 -0
- data/lib/bitbucket_rest_api/request/oauth.rb +3 -3
- data/lib/bitbucket_rest_api/request/verbs.rb +53 -0
- data/lib/bitbucket_rest_api/request.rb +60 -36
- data/lib/bitbucket_rest_api/response/header.rb +68 -0
- data/lib/bitbucket_rest_api/response_wrapper.rb +157 -0
- data/lib/bitbucket_rest_api/result.rb +5 -77
- data/lib/bitbucket_rest_api/users.rb +4 -8
- data/lib/bitbucket_rest_api/validations/presence.rb +16 -11
- data/lib/bitbucket_rest_api/validations.rb +6 -6
- data/lib/bitbucket_rest_api/version.rb +2 -2
- data/lib/bitbucket_rest_api.rb +89 -44
- metadata +39 -131
- data/lib/bitbucket_rest_api/api_factory.rb +0 -30
@@ -27,13 +27,7 @@ module BitBucket
|
|
27
27
|
|
28
28
|
ACCEPT_CHARSET = 'Accept-Charset'.freeze
|
29
29
|
|
30
|
-
# Link
|
31
|
-
HEADER_LINK = "Link".freeze
|
32
|
-
|
33
|
-
HEADER_NEXT = "X-Next".freeze
|
34
|
-
|
35
|
-
HEADER_LAST = "X-Last".freeze
|
36
|
-
|
30
|
+
# Link params
|
37
31
|
META_REL = "rel".freeze
|
38
32
|
|
39
33
|
META_LAST = "last".freeze
|
@@ -46,8 +40,6 @@ module BitBucket
|
|
46
40
|
|
47
41
|
PARAM_PAGE = "page".freeze
|
48
42
|
|
49
|
-
PARAM_PER_PAGE = "per_page".freeze
|
50
|
-
|
51
43
|
PARAM_START_PAGE = "start_page".freeze
|
52
44
|
|
53
45
|
# URI parsing
|
@@ -8,7 +8,7 @@ module BitBucket #:nodoc
|
|
8
8
|
super(
|
9
9
|
generate_message(
|
10
10
|
:problem => "Invalid option #{invalid.keys.join(', ')} provided for this request.",
|
11
|
-
:summary => "BitBucket gem checks the request parameters passed to ensure that
|
11
|
+
:summary => "BitBucket gem checks the request parameters passed to ensure that bitbucket api is not hit unnecessairly and to fail fast.",
|
12
12
|
:resolution => "Valid options are: #{valid.join(', ')}, make sure these are the ones you are using"
|
13
13
|
)
|
14
14
|
)
|
@@ -8,7 +8,7 @@ module BitBucket #:nodoc
|
|
8
8
|
super(
|
9
9
|
generate_message(
|
10
10
|
:problem => "Missing required parameters: #{provided.keys.join(', ')} provided for this request.",
|
11
|
-
:summary => "BitBucket gem checks the request parameters passed to ensure that
|
11
|
+
:summary => "BitBucket gem checks the request parameters passed to ensure that bitbucket api is not hit unnecessairly and to fail fast.",
|
12
12
|
:resolution => "Required parameters are: #{required.join(', ')}, make sure these are the ones you are using"
|
13
13
|
)
|
14
14
|
)
|
@@ -8,7 +8,7 @@ module BitBucket #:nodoc
|
|
8
8
|
super(
|
9
9
|
generate_message(
|
10
10
|
:problem => "Wrong value of '#{value}' for the parameter: #{key} provided for this request.",
|
11
|
-
:summary => "BitBucket gem checks the request parameters passed to ensure that
|
11
|
+
:summary => "BitBucket gem checks the request parameters passed to ensure that bitbucket api is not hit unnecessairly and fails fast.",
|
12
12
|
:resolution => "Permitted values are: #{permitted}, make sure these are the ones you are using"
|
13
13
|
)
|
14
14
|
)
|
@@ -0,0 +1,38 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module Faraday
|
4
|
+
module Utils
|
5
|
+
|
6
|
+
class ParamsHash
|
7
|
+
def params_encoder(encoder = nil)
|
8
|
+
if encoder
|
9
|
+
@encoder = encoder
|
10
|
+
else
|
11
|
+
@encoder
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def to_query(encoder = nil)
|
16
|
+
Utils.build_nested_query(self, nil, params_encoder)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def build_nested_query(value, prefix = nil, encoder = nil)
|
21
|
+
case value
|
22
|
+
when Array
|
23
|
+
value.map { |v| build_nested_query(v, "#{prefix}%5B%5D", encoder) }.join("&")
|
24
|
+
when Hash
|
25
|
+
value.map { |k, v|
|
26
|
+
processed_value = encoder ? encoder.escape(k) : escape(k)
|
27
|
+
build_nested_query(v, prefix ? "#{prefix}%5B#{processed_value}%5D" : processed_value, encoder)
|
28
|
+
}.join("&")
|
29
|
+
when NilClass
|
30
|
+
prefix
|
31
|
+
else
|
32
|
+
raise ArgumentError, "value must be a Hash" if prefix.nil?
|
33
|
+
processed_value = encoder ? encoder.escape(value) : escape(value)
|
34
|
+
"#{prefix}=#{processed_value}"
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'bitbucket_rest_api/response'
|
4
|
+
require 'bitbucket_rest_api/response/mashify'
|
5
|
+
require 'bitbucket_rest_api/response/jsonize'
|
6
|
+
require 'bitbucket_rest_api/response/raise_error'
|
7
|
+
require 'bitbucket_rest_api/response/header'
|
8
|
+
|
9
|
+
module BitBucket
|
10
|
+
class Middleware
|
11
|
+
def self.default(options = {})
|
12
|
+
api = options[:api]
|
13
|
+
proc do |builder|
|
14
|
+
builder.use Faraday::Request::Multipart
|
15
|
+
builder.use Faraday::Request::UrlEncoded
|
16
|
+
builder.use FaradayMiddleware::OAuth, {:consumer_key => api.client_id, :consumer_secret => api.client_secret, :token => api.oauth_token, :token_secret => api.oauth_secret} if api.client_id? and api.client_secret?
|
17
|
+
builder.use BitBucket::Request::BasicAuth, api.authentication if api.basic_authed?
|
18
|
+
builder.use FaradayMiddleware::EncodeJson
|
19
|
+
|
20
|
+
builder.use Faraday::Response::Logger if ENV['DEBUG']
|
21
|
+
#builder.use BitBucket::Response::Helpers
|
22
|
+
unless options[:raw]
|
23
|
+
builder.use BitBucket::Response::Mashify
|
24
|
+
builder.use BitBucket::Response::Jsonize
|
25
|
+
end
|
26
|
+
builder.use BitBucket::Response::RaiseError
|
27
|
+
builder.adapter options[:adapter]
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end # Middleware
|
31
|
+
end # BitBucket
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'faraday'
|
4
|
+
|
5
|
+
module BitBucket
|
6
|
+
|
7
|
+
# Skip encoding of the key nested parameters
|
8
|
+
module NullParamsEncoder
|
9
|
+
if defined?(Faraday::NestedParamsEncoder)
|
10
|
+
class << self
|
11
|
+
Faraday::NestedParamsEncoder.singleton_methods do |m|
|
12
|
+
define_method m, ::Faraday::NestedParamsEncoder.method(m).to_proc
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.escape(s)
|
18
|
+
s.to_s
|
19
|
+
end
|
20
|
+
|
21
|
+
def self.unescape(s)
|
22
|
+
s.to_s
|
23
|
+
end
|
24
|
+
end # NullEncoder
|
25
|
+
end # BitBucket
|
@@ -0,0 +1,90 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'bitbucket_rest_api/utils/url'
|
4
|
+
require 'uri'
|
5
|
+
|
6
|
+
module BitBucket
|
7
|
+
class PageIterator
|
8
|
+
include BitBucket::Constants
|
9
|
+
include BitBucket::Utils::Url
|
10
|
+
include BitBucket::PagedRequest
|
11
|
+
|
12
|
+
# Setup attribute accesor for all the link types
|
13
|
+
ATTRIBUTES = [ META_FIRST, META_NEXT, META_PREV ]
|
14
|
+
|
15
|
+
ATTRIBUTES.each do |attr|
|
16
|
+
attr_accessor :"#{attr}_page_uri", :"#{attr}_page"
|
17
|
+
end
|
18
|
+
|
19
|
+
attr_reader :current_api
|
20
|
+
|
21
|
+
def initialize(links, current_api)
|
22
|
+
@links = links
|
23
|
+
@current_api = current_api
|
24
|
+
update_page_links @links
|
25
|
+
end
|
26
|
+
|
27
|
+
def has_next?
|
28
|
+
next_page == 0 || !next_page_uri.nil?
|
29
|
+
end
|
30
|
+
|
31
|
+
# Perform http get request for the first resource
|
32
|
+
#
|
33
|
+
def first
|
34
|
+
return nil unless first_page_uri
|
35
|
+
perform_request(first_page_uri)
|
36
|
+
end
|
37
|
+
|
38
|
+
# Perform http get request for the next resource
|
39
|
+
#
|
40
|
+
def next
|
41
|
+
return nil unless has_next?
|
42
|
+
perform_request(next_page_uri)
|
43
|
+
end
|
44
|
+
|
45
|
+
# Perform http get request for the previous resource
|
46
|
+
#
|
47
|
+
def prev
|
48
|
+
return nil unless prev_page_uri
|
49
|
+
perform_request(prev_page_uri)
|
50
|
+
end
|
51
|
+
|
52
|
+
# Returns the result for a specific page.
|
53
|
+
#
|
54
|
+
def get_page(page_number)
|
55
|
+
# Find URI that we can work with, if we cannot get the first
|
56
|
+
# page URI then there is only one page.
|
57
|
+
return nil unless first_page_uri
|
58
|
+
params = parse_query URI(first_page_uri).query
|
59
|
+
params['page'] = page_number
|
60
|
+
|
61
|
+
response = page_request URI(first_page_uri).path, params
|
62
|
+
update_page_links response.links
|
63
|
+
response
|
64
|
+
end
|
65
|
+
|
66
|
+
private
|
67
|
+
|
68
|
+
def perform_request(attribute)
|
69
|
+
page_uri = URI(attribute)
|
70
|
+
params = parse_query(page_uri.query)
|
71
|
+
|
72
|
+
if next_page and next_page >= 1
|
73
|
+
params['page'] = attribute.to_i
|
74
|
+
end
|
75
|
+
|
76
|
+
response = page_request(page_uri.path, params)
|
77
|
+
update_page_links response.links
|
78
|
+
response
|
79
|
+
end
|
80
|
+
|
81
|
+
# Wholesale update of all link attributes
|
82
|
+
def update_page_links(links) # :nodoc:
|
83
|
+
ATTRIBUTES.each do |attr|
|
84
|
+
self.send(:"#{attr}_page_uri=", links.send(:"#{attr}"))
|
85
|
+
self.send(:"#{attr}_page=", links.send(:"#{attr}"))
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
end # PageIterator
|
90
|
+
end # BitBucket
|
@@ -0,0 +1,33 @@
|
|
1
|
+
module BitBucket
|
2
|
+
# Determines the links in the current response to be used
|
3
|
+
# to find the links to other pages of request responses.
|
4
|
+
class PageLinks
|
5
|
+
include BitBucket::Constants
|
6
|
+
|
7
|
+
FIRST_PAGE_NUMBER = 1 # :nodoc:
|
8
|
+
|
9
|
+
# Hold the extracted values for URI from the response body
|
10
|
+
# for the next and previous page.
|
11
|
+
attr_accessor :response_dup, :first, :next, :prev
|
12
|
+
|
13
|
+
# Parses links from executed request
|
14
|
+
#
|
15
|
+
def initialize(response)
|
16
|
+
self.response_dup = response
|
17
|
+
if response.body.is_a?(Hash) and !response.body[PARAM_PAGE].nil?
|
18
|
+
self.first = path_for_page(FIRST_PAGE_NUMBER)
|
19
|
+
self.next = response.body[META_NEXT] unless response.body
|
20
|
+
self.prev = response.body[META_PREV]
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
private
|
25
|
+
|
26
|
+
def path_for_page(page_number)
|
27
|
+
if response_dup.respond_to?(:url)
|
28
|
+
self.response_dup.url.to_s.gsub(BitBucket.endpoint, '')
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
end # PageLinks
|
33
|
+
end # BitBucket
|
@@ -0,0 +1,29 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module BitBucket
|
4
|
+
|
5
|
+
# A module that adds http get request to response pagination
|
6
|
+
module PagedRequest
|
7
|
+
include BitBucket::Constants
|
8
|
+
include BitBucket::Normalizer
|
9
|
+
|
10
|
+
FIRST_PAGE = 1 # Default request page if none provided
|
11
|
+
|
12
|
+
NOT_FOUND = -1 # Page parameter not present
|
13
|
+
|
14
|
+
def default_page
|
15
|
+
current_api.current_page ? current_api.current_page : FIRST_PAGE
|
16
|
+
end
|
17
|
+
|
18
|
+
# Perform http get request with pagination parameters
|
19
|
+
#
|
20
|
+
def page_request(path, params={})
|
21
|
+
if params[PARAM_PAGE] && params[PARAM_PAGE] == NOT_FOUND
|
22
|
+
params[PARAM_PAGE] = default_page
|
23
|
+
end
|
24
|
+
|
25
|
+
current_api.get_request(path, ParamsHash.new(params))
|
26
|
+
end
|
27
|
+
|
28
|
+
end # PagedRequest
|
29
|
+
end # BitBucket
|
@@ -0,0 +1,98 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module BitBucket
|
4
|
+
|
5
|
+
# A module that decorates response with pagination helpers
|
6
|
+
module Pagination
|
7
|
+
include BitBucket::Constants
|
8
|
+
|
9
|
+
def paginated?
|
10
|
+
body.is_a?(Hash) and body[PARAM_PAGE].nil?
|
11
|
+
end
|
12
|
+
|
13
|
+
# Return page links
|
14
|
+
def links
|
15
|
+
@links = BitBucket::PageLinks.new(self.response)
|
16
|
+
end
|
17
|
+
|
18
|
+
# Iterate over results set pages by automatically calling `next_page`
|
19
|
+
# until all pages are exhausted. Caution needs to be exercised when
|
20
|
+
# using this feature - 100 pages iteration will perform 100 API calls.
|
21
|
+
# By default this is off. You can set it on the client, individual API
|
22
|
+
# instances or just per given request.
|
23
|
+
#
|
24
|
+
def auto_paginate(auto=false)
|
25
|
+
if (current_api.auto_pagination? || auto)
|
26
|
+
resources_bodies = []
|
27
|
+
each_page do |resource|
|
28
|
+
if resource.body.respond_to?(:values)
|
29
|
+
resources_bodies += resource.body[:values].collect {|value| ::Hashie::Mash.new(value) }
|
30
|
+
else
|
31
|
+
resources_bodies += Array(resource.body)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
self.body = resources_bodies
|
35
|
+
end
|
36
|
+
self
|
37
|
+
end
|
38
|
+
|
39
|
+
# Iterator like each for response pages. If there are no pages to
|
40
|
+
# iterate over this method will return current page.
|
41
|
+
def each_page
|
42
|
+
yield self
|
43
|
+
while page_iterator.has_next?
|
44
|
+
yield next_page
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
# Retrives the result of the first page. Returns <tt>nil</tt> if there is
|
49
|
+
# no first page - either because you are already on the first page
|
50
|
+
# or there are no pages at all in the result.
|
51
|
+
def first_page
|
52
|
+
first_request = page_iterator.first
|
53
|
+
self.instance_eval { @env = first_request.env } if first_request
|
54
|
+
first_request
|
55
|
+
end
|
56
|
+
|
57
|
+
# Retrives the result of the next page. Returns <tt>nil</tt> if there is
|
58
|
+
# no next page or no pages at all.
|
59
|
+
def next_page
|
60
|
+
next_request = page_iterator.next
|
61
|
+
self.instance_eval { @env = next_request.env } if next_request
|
62
|
+
next_request
|
63
|
+
end
|
64
|
+
|
65
|
+
# Retrives the result of the previous page. Returns <tt>nil</tt> if there is
|
66
|
+
# no previous page or no pages at all.
|
67
|
+
def prev_page
|
68
|
+
prev_request = page_iterator.prev
|
69
|
+
self.instance_eval { @env = prev_request.env } if prev_request
|
70
|
+
prev_request
|
71
|
+
end
|
72
|
+
alias :previous_page :prev_page
|
73
|
+
|
74
|
+
# Retrives a specific result for a page given page number.
|
75
|
+
# The <tt>page_number</tt> parameter is not validate, hitting a page
|
76
|
+
# that does not exist will return BitBucket API error. Consequently, if
|
77
|
+
# there is only one page, this method returns nil
|
78
|
+
def page(page_number)
|
79
|
+
request = page_iterator.get_page(page_number)
|
80
|
+
self.instance_eval { @env = request.env } if request
|
81
|
+
request
|
82
|
+
end
|
83
|
+
|
84
|
+
# Returns <tt>true</tt> if there is another page in the result set,
|
85
|
+
# otherwise <tt>false</tt>
|
86
|
+
def has_next_page?
|
87
|
+
page_iterator.has_next?
|
88
|
+
end
|
89
|
+
|
90
|
+
private
|
91
|
+
|
92
|
+
# Internally used page iterator
|
93
|
+
def page_iterator # :nodoc:
|
94
|
+
@page_iterator = BitBucket::PageIterator.new(links, current_api)
|
95
|
+
end
|
96
|
+
|
97
|
+
end # Pagination
|
98
|
+
end # BitBucket
|
@@ -10,7 +10,7 @@ module BitBucket
|
|
10
10
|
#
|
11
11
|
def filter!(keys, params, options={:recursive => true}) # :nodoc:
|
12
12
|
case params
|
13
|
-
when Hash
|
13
|
+
when Hash, ParamsHash
|
14
14
|
params.keys.each do |k, v|
|
15
15
|
unless (keys.include?(k) or BitBucket::Validations::VALID_API_KEYS.include?(k))
|
16
16
|
params.delete(k)
|
@@ -0,0 +1,100 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'delegate'
|
4
|
+
require 'base64'
|
5
|
+
|
6
|
+
module BitBucket
|
7
|
+
|
8
|
+
# Class responsible for holding request parameters
|
9
|
+
class ParamsHash < DelegateClass(Hash)
|
10
|
+
include Normalizer
|
11
|
+
|
12
|
+
def initialize(hash)
|
13
|
+
super(normalize!(Hash[hash]))
|
14
|
+
end
|
15
|
+
|
16
|
+
# Create empty hash
|
17
|
+
#
|
18
|
+
def self.empty
|
19
|
+
new({})
|
20
|
+
end
|
21
|
+
|
22
|
+
# Extract and parse media type param
|
23
|
+
#
|
24
|
+
# [.version].param[+json]
|
25
|
+
#
|
26
|
+
def media
|
27
|
+
parse(delete('media'))
|
28
|
+
end
|
29
|
+
|
30
|
+
# Return accept header if present
|
31
|
+
#
|
32
|
+
def accept
|
33
|
+
if has_key?('accept')
|
34
|
+
delete('accept')
|
35
|
+
elsif has_key?('media')
|
36
|
+
media
|
37
|
+
else
|
38
|
+
nil
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
# Extract request data from parameters
|
43
|
+
#
|
44
|
+
def data
|
45
|
+
if has_key?('data') && !self['data'].nil?
|
46
|
+
return delete('data')
|
47
|
+
else
|
48
|
+
return to_hash
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
def encoder
|
53
|
+
if has_key?('encoder') && self['encoder']
|
54
|
+
return delete('encoder')
|
55
|
+
else
|
56
|
+
return {}
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
# Any client configuration options
|
61
|
+
#
|
62
|
+
def options
|
63
|
+
opts = has_key?('options') ? delete('options') : {}
|
64
|
+
headers = opts.fetch(:headers) { {} }
|
65
|
+
if value = accept
|
66
|
+
headers[:accept] = value
|
67
|
+
end
|
68
|
+
if value = delete('content_type')
|
69
|
+
headers[:content_type] = value
|
70
|
+
end
|
71
|
+
opts[:raw] = has_key?('raw') ? delete('raw') : false
|
72
|
+
opts[:headers] = headers unless headers.empty?
|
73
|
+
opts
|
74
|
+
end
|
75
|
+
|
76
|
+
# Update hash with default parameters for non existing keys
|
77
|
+
#
|
78
|
+
def merge_default(defaults)
|
79
|
+
if defaults && !defaults.empty?
|
80
|
+
defaults.each do |key, value|
|
81
|
+
self[key] = value unless self.has_key?(key)
|
82
|
+
end
|
83
|
+
end
|
84
|
+
self
|
85
|
+
end
|
86
|
+
|
87
|
+
# Base64 encode string removing newline characters
|
88
|
+
#
|
89
|
+
def strict_encode64(key)
|
90
|
+
value = self[key]
|
91
|
+
encoded = if Base64.respond_to?(:strict_encode64)
|
92
|
+
Base64.strict_encode64(value)
|
93
|
+
else
|
94
|
+
[value].pack("m0")
|
95
|
+
end
|
96
|
+
self[key] = encoded.delete("\n\r")
|
97
|
+
end
|
98
|
+
|
99
|
+
end # ParamsHash
|
100
|
+
end # BitBucket
|
@@ -4,12 +4,14 @@ require 'faraday'
|
|
4
4
|
require 'base64'
|
5
5
|
|
6
6
|
module BitBucket
|
7
|
-
|
7
|
+
class Request
|
8
8
|
class BasicAuth < Faraday::Middleware
|
9
9
|
dependency 'base64'
|
10
10
|
|
11
11
|
def call(env)
|
12
|
-
|
12
|
+
unless @auth.to_s.empty?
|
13
|
+
env[:request_headers].merge!('Authorization' => "Basic #{@auth}\"")
|
14
|
+
end
|
13
15
|
|
14
16
|
@app.call env
|
15
17
|
end
|
@@ -14,6 +14,11 @@ module BitBucket
|
|
14
14
|
if request_with_body?(env)
|
15
15
|
env[:request_headers][CONTENT_TYPE] ||= MIME_TYPE
|
16
16
|
env[:body] = encode_body env unless env[:body].respond_to?(:to_str)
|
17
|
+
else
|
18
|
+
# Ensure valid body for put and post requests
|
19
|
+
if [:put, :patch, :post].include? env[:method]
|
20
|
+
env[:body] = encode_body({})
|
21
|
+
end
|
17
22
|
end
|
18
23
|
@app.call env
|
19
24
|
end
|
@@ -3,7 +3,7 @@
|
|
3
3
|
require 'faraday'
|
4
4
|
|
5
5
|
module BitBucket
|
6
|
-
|
6
|
+
class Request
|
7
7
|
class OAuth < Faraday::Middleware
|
8
8
|
include BitBucket::Utils::Url
|
9
9
|
|
@@ -18,12 +18,12 @@ module BitBucket
|
|
18
18
|
if (@token and @secret) and (!@token.empty? and !@secret.empty?)
|
19
19
|
access_token = ::OAuth::AccessToken.new(@consumer, @token, @secret)
|
20
20
|
env[:url].query = build_query params
|
21
|
-
|
21
|
+
|
22
22
|
puts oauth_helper.header
|
23
23
|
puts oauth_helper.header.class
|
24
24
|
env[:request_headers].merge!(AUTH_HEADER => oauth_helper.header)
|
25
25
|
end
|
26
|
-
|
26
|
+
|
27
27
|
env[:url].query = build_query params
|
28
28
|
|
29
29
|
|
@@ -0,0 +1,53 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module BitBucket
|
4
|
+
# A class responsible for dispatching http requests
|
5
|
+
class Request
|
6
|
+
|
7
|
+
# Defines HTTP verbs
|
8
|
+
module Verbs
|
9
|
+
# Make a get request
|
10
|
+
#
|
11
|
+
# @api public
|
12
|
+
def get_request(path, params = ParamsHash.empty)
|
13
|
+
request = Request.new(:get, path, self).call(current_options, params)
|
14
|
+
request.auto_paginate
|
15
|
+
end
|
16
|
+
|
17
|
+
# Make a patch request
|
18
|
+
#
|
19
|
+
# @api public
|
20
|
+
def patch_request(path, params = ParamsHash.empty)
|
21
|
+
Request.new(:patch, path, self).call(current_options, params)
|
22
|
+
end
|
23
|
+
|
24
|
+
# Make a post request
|
25
|
+
#
|
26
|
+
# @api public
|
27
|
+
def post_request(path, params = ParamsHash.empty)
|
28
|
+
Request.new(:post, path, self).call(current_options, params)
|
29
|
+
end
|
30
|
+
|
31
|
+
# Make a put request
|
32
|
+
#
|
33
|
+
# @api public
|
34
|
+
def put_request(path, params = ParamsHash.empty)
|
35
|
+
Request.new(:put, path, self).call(current_options, params)
|
36
|
+
end
|
37
|
+
|
38
|
+
# Make a delete request
|
39
|
+
#
|
40
|
+
# @api public
|
41
|
+
def delete_request(path, params = ParamsHash.empty)
|
42
|
+
Request.new(:delete, path, self).call(current_options, params)
|
43
|
+
end
|
44
|
+
|
45
|
+
# Make a options request
|
46
|
+
#
|
47
|
+
# @api public
|
48
|
+
def options_request(path, params = ParamsHash.empty)
|
49
|
+
Request.new(:options, path, self).call(current_options, params)
|
50
|
+
end
|
51
|
+
end # Verbs
|
52
|
+
end # Request
|
53
|
+
end # BitBucket
|