Module: Hub::GitHubAPI::HttpMethods
- Included in:
- Hub::GitHubAPI
- Defined in:
- lib/hub/github_api.rb
Overview
Methods for performing HTTP requests
Requires access to a ‘config` object that implements:
-
proxy_uri(with_ssl)
-
username(host)
-
password(host, user)
Defined Under Namespace
Modules: ResponseMethods
Instance Method Summary collapse
- #apply_authentication(req, url) ⇒ Object
- #byte_size(str) ⇒ Object
- #configure_connection(req, url) {|url| ... } ⇒ Object
- #create_connection(url) ⇒ Object
- #finalize_request(req, url) ⇒ Object
- #get(url, &block) ⇒ Object
- #get_all(url, &block) ⇒ Object
- #perform_request(url, type) {|req| ... } ⇒ Object
- #post(url, params = nil) ⇒ Object
- #post_form(url, params) ⇒ Object
- #request_uri(url) ⇒ Object
Instance Method Details
#apply_authentication(req, url) ⇒ Object
289 290 291 292 293 |
# File 'lib/hub/github_api.rb', line 289 def apply_authentication req, url user = url.user ? CGI.unescape(url.user) : config.username(url.host) pass = config.password(url.host, user) req.basic_auth user, pass end |
#byte_size(str) ⇒ Object
233 234 235 236 237 238 |
# File 'lib/hub/github_api.rb', line 233 def byte_size str if str.respond_to? :bytesize then str.bytesize elsif str.respond_to? :length then str.length else 0 end end |
#configure_connection(req, url) {|url| ... } ⇒ Object
274 275 276 277 278 279 280 281 282 283 284 285 286 287 |
# File 'lib/hub/github_api.rb', line 274 def configure_connection req, url url.scheme = config.protocol(url.host) url.port = 80 if url.scheme == 'http' if ENV['HUB_TEST_HOST'] req['Host'] = url.host req['X-Original-Scheme'] = url.scheme req['X-Original-Port'] = url.port url = url.dup url.scheme = 'http' url.host, test_port = ENV['HUB_TEST_HOST'].split(':') url.port = test_port ? test_port.to_i : 80 end yield url end |
#create_connection(url) ⇒ Object
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 |
# File 'lib/hub/github_api.rb', line 301 def create_connection url use_ssl = 'https' == url.scheme proxy_args = [] if proxy = config.proxy_uri(use_ssl) proxy_args << proxy.host << proxy.port if proxy.userinfo # proxy user + password proxy_args.concat proxy.userinfo.split(':', 2).map {|a| CGI.unescape a } end end http = Net::HTTP.new(url.host, url.port, *proxy_args) if http.use_ssl = use_ssl # FIXME: enable SSL peer verification! http.verify_mode = OpenSSL::SSL::VERIFY_NONE end return http end |
#finalize_request(req, url) ⇒ Object
295 296 297 298 299 |
# File 'lib/hub/github_api.rb', line 295 def finalize_request(req, url) if !req['Accept'] || req['Accept'] == '*/*' req['Accept'] = 'application/vnd.github.v3+json' end end |
#get(url, &block) ⇒ Object
203 204 205 |
# File 'lib/hub/github_api.rb', line 203 def get url, &block perform_request url, :Get, &block end |
#get_all(url, &block) ⇒ Object
207 208 209 210 211 212 213 214 215 216 217 218 219 220 |
# File 'lib/hub/github_api.rb', line 207 def get_all url, &block previous_res = nil user = url.user while url res = get(url, &block) if url = res.next_url url = url.dup url.user = user end res.data.unshift(*previous_res.data) if res.success? && previous_res previous_res = res end res end |
#perform_request(url, type) {|req| ... } ⇒ Object
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 |
# File 'lib/hub/github_api.rb', line 244 def perform_request url, type url = URI.parse url unless url.respond_to? :host require 'net/https' req = Net::HTTP.const_get(type).new request_uri(url) # TODO: better naming? http = configure_connection(req, url) do |host_url| create_connection host_url end req['User-Agent'] = "Hub #{Hub::VERSION}" apply_authentication(req, url) yield req if block_given? finalize_request(req, url) begin res = http.start { http.request(req) } res.extend ResponseMethods return res rescue SocketError => err raise Context::FatalError, "error with #{type.to_s.upcase} #{url} (#{err.})" end end |
#post(url, params = nil) ⇒ Object
222 223 224 225 226 227 228 229 230 231 |
# File 'lib/hub/github_api.rb', line 222 def post url, params = nil perform_request url, :Post do |req| if params req.body = JSON.dump params req['Content-Type'] = 'application/json;charset=utf-8' end yield req if block_given? req['Content-Length'] = byte_size req.body end end |
#post_form(url, params) ⇒ Object
240 241 242 |
# File 'lib/hub/github_api.rb', line 240 def post_form url, params post(url) {|req| req.set_form_data params } end |
#request_uri(url) ⇒ Object
268 269 270 271 272 |
# File 'lib/hub/github_api.rb', line 268 def request_uri url str = url.request_uri str = '/api/v3' << str if url.host != 'api.github.com' && url.host != 'gist.github.com' str end |