1- require 'sawyer'
1+ require 'octokit/connection'
2+ require 'octokit/warnable'
23require 'octokit/arguments'
34require 'octokit/repo_arguments'
45require 'octokit/configurable'
@@ -53,6 +54,8 @@ class Client
5354
5455 include Octokit ::Authentication
5556 include Octokit ::Configurable
57+ include Octokit ::Connection
58+ include Octokit ::Warnable
5659 include Octokit ::Client ::Authorizations
5760 include Octokit ::Client ::Commits
5861 include Octokit ::Client ::CommitComments
@@ -101,14 +104,6 @@ def initialize(options = {})
101104 login_from_netrc unless user_authenticated? || application_authenticated?
102105 end
103106
104- # Compares client options to a Hash of requested options
105- #
106- # @param opts [Hash] Options to compare with current client options
107- # @return [Boolean]
108- def same_options? ( opts )
109- opts . hash == options . hash
110- end
111-
112107 # Text representation of the client, masking tokens and passwords
113108 #
114109 # @return [String]
@@ -117,6 +112,7 @@ def inspect
117112
118113 # mask password
119114 inspected = inspected . gsub! @password , "*******" if @password
115+ inspected = inspected . gsub! @management_console_password , "*******" if @management_console_password
120116 # Only show last 4 of token, secret
121117 if @access_token
122118 inspected = inspected . gsub! @access_token , "#{ '*' *36 } #{ @access_token [ 36 ..-1 ] } "
@@ -128,126 +124,6 @@ def inspect
128124 inspected
129125 end
130126
131- # Make a HTTP GET request
132- #
133- # @param url [String] The path, relative to {#api_endpoint}
134- # @param options [Hash] Query and header params for request
135- # @return [Sawyer::Resource]
136- def get ( url , options = { } )
137- request :get , url , parse_query_and_convenience_headers ( options )
138- end
139-
140- # Make a HTTP POST request
141- #
142- # @param url [String] The path, relative to {#api_endpoint}
143- # @param options [Hash] Body and header params for request
144- # @return [Sawyer::Resource]
145- def post ( url , options = { } )
146- request :post , url , options
147- end
148-
149- # Make a HTTP PUT request
150- #
151- # @param url [String] The path, relative to {#api_endpoint}
152- # @param options [Hash] Body and header params for request
153- # @return [Sawyer::Resource]
154- def put ( url , options = { } )
155- request :put , url , options
156- end
157-
158- # Make a HTTP PATCH request
159- #
160- # @param url [String] The path, relative to {#api_endpoint}
161- # @param options [Hash] Body and header params for request
162- # @return [Sawyer::Resource]
163- def patch ( url , options = { } )
164- request :patch , url , options
165- end
166-
167- # Make a HTTP DELETE request
168- #
169- # @param url [String] The path, relative to {#api_endpoint}
170- # @param options [Hash] Query and header params for request
171- # @return [Sawyer::Resource]
172- def delete ( url , options = { } )
173- request :delete , url , options
174- end
175-
176- # Make a HTTP HEAD request
177- #
178- # @param url [String] The path, relative to {#api_endpoint}
179- # @param options [Hash] Query and header params for request
180- # @return [Sawyer::Resource]
181- def head ( url , options = { } )
182- request :head , url , parse_query_and_convenience_headers ( options )
183- end
184-
185- # Make one or more HTTP GET requests, optionally fetching
186- # the next page of results from URL in Link response header based
187- # on value in {#auto_paginate}.
188- #
189- # @param url [String] The path, relative to {#api_endpoint}
190- # @param options [Hash] Query and header params for request
191- # @param block [Block] Block to perform the data concatination of the
192- # multiple requests. The block is called with two parameters, the first
193- # contains the contents of the requests so far and the second parameter
194- # contains the latest response.
195- # @return [Sawyer::Resource]
196- def paginate ( url , options = { } , &block )
197- opts = parse_query_and_convenience_headers ( options . dup )
198- if @auto_paginate || @per_page
199- opts [ :query ] [ :per_page ] ||= @per_page || ( @auto_paginate ? 100 : nil )
200- end
201-
202- data = request ( :get , url , opts . dup )
203-
204- if @auto_paginate
205- while @last_response . rels [ :next ] && rate_limit . remaining > 0
206- @last_response = @last_response . rels [ :next ] . get ( :headers => opts [ :headers ] )
207- if block_given?
208- yield ( data , @last_response )
209- else
210- data . concat ( @last_response . data ) if @last_response . data . is_a? ( Array )
211- end
212- end
213-
214- end
215-
216- data
217- end
218-
219- # Hypermedia agent for the GitHub API
220- #
221- # @return [Sawyer::Agent]
222- def agent
223- @agent ||= Sawyer ::Agent . new ( api_endpoint , sawyer_options ) do |http |
224- http . headers [ :accept ] = default_media_type
225- http . headers [ :content_type ] = "application/json"
226- http . headers [ :user_agent ] = user_agent
227- if basic_authenticated?
228- http . basic_auth ( @login , @password )
229- elsif token_authenticated?
230- http . authorization 'token' , @access_token
231- elsif application_authenticated?
232- http . params = http . params . merge application_authentication
233- end
234- end
235- end
236-
237- # Fetch the root resource for the API
238- #
239- # @return [Sawyer::Resource]
240- def root
241- get "/"
242- end
243-
244- # Response for last HTTP request
245- #
246- # @return [Sawyer::Response]
247- def last_response
248- @last_response if defined? @last_response
249- end
250-
251127 # Duplicate client using client_id and client_secret as
252128 # Basic Authentication credentials.
253129 # @example
@@ -312,72 +188,5 @@ def client_secret=(value)
312188 reset_agent
313189 @client_secret = value
314190 end
315-
316- # Wrapper around Kernel#warn to print warnings unless
317- # OCTOKIT_SILENT is set to true.
318- #
319- # @return [nil]
320- def octokit_warn ( *message )
321- unless ENV [ 'OCTOKIT_SILENT' ]
322- warn message
323- end
324- end
325-
326- private
327-
328- def reset_agent
329- @agent = nil
330- end
331-
332- def request ( method , path , data , options = { } )
333- if data . is_a? ( Hash )
334- options [ :query ] = data . delete ( :query ) || { }
335- options [ :headers ] = data . delete ( :headers ) || { }
336- if accept = data . delete ( :accept )
337- options [ :headers ] [ :accept ] = accept
338- end
339- end
340-
341- @last_response = response = agent . call ( method , URI ::Parser . new . escape ( path . to_s ) , data , options )
342- response . data
343- end
344-
345- # Executes the request, checking if it was successful
346- #
347- # @return [Boolean] True on success, false otherwise
348- def boolean_from_response ( method , path , options = { } )
349- request ( method , path , options )
350- @last_response . status == 204
351- rescue Octokit ::NotFound
352- false
353- end
354-
355-
356- def sawyer_options
357- opts = {
358- :links_parser => Sawyer ::LinkParsers ::Simple . new
359- }
360- conn_opts = @connection_options
361- conn_opts [ :builder ] = @middleware if @middleware
362- conn_opts [ :proxy ] = @proxy if @proxy
363- opts [ :faraday ] = Faraday . new ( conn_opts )
364-
365- opts
366- end
367-
368- def parse_query_and_convenience_headers ( options )
369- headers = options . delete ( :headers ) { Hash . new }
370- CONVENIENCE_HEADERS . each do |h |
371- if header = options . delete ( h )
372- headers [ h ] = header
373- end
374- end
375- query = options . delete ( :query )
376- opts = { :query => options }
377- opts [ :query ] . merge! ( query ) if query && query . is_a? ( Hash )
378- opts [ :headers ] = headers unless headers . empty?
379-
380- opts
381- end
382191 end
383192end
0 commit comments