Class: Keycardai::OAuth::HTTP::NetHTTPClient

Inherits:
Object
  • Object
show all
Defined in:
lib/keycardai/oauth/http.rb

Overview

Default transport backed by Net::HTTP. TLS is used for https URLs.

Instance Method Summary collapse

Instance Method Details

#get(url, headers: {}, timeout: nil) ⇒ Response

Parameters:

  • url (String)
  • headers (Hash{String => String}) (defaults to: {})
  • timeout (Numeric, nil) (defaults to: nil)

    open/read timeout in seconds

Returns:

Raises:

  • (NetworkError)

    on DNS, TLS, connect, or timeout failures



36
37
38
39
40
41
# File 'lib/keycardai/oauth/http.rb', line 36

def get(url, headers: {}, timeout: nil)
  uri = URI(url)
  request = Net::HTTP::Get.new(uri)
  headers.each { |name, value| request[name] = value }
  perform(uri, request, timeout)
end

#post_form(url, params, headers: {}, timeout: nil) ⇒ Response

Parameters:

  • url (String)
  • params (Hash)

    form fields, sent application/x-www-form-urlencoded

  • headers (Hash{String => String}) (defaults to: {})
  • timeout (Numeric, nil) (defaults to: nil)

    open/read timeout in seconds

Returns:

Raises:

  • (NetworkError)

    on DNS, TLS, connect, or timeout failures



49
50
51
52
53
54
55
# File 'lib/keycardai/oauth/http.rb', line 49

def post_form(url, params, headers: {}, timeout: nil)
  uri = URI(url)
  request = Net::HTTP::Post.new(uri)
  headers.each { |name, value| request[name] = value }
  request.set_form_data(params)
  perform(uri, request, timeout)
end

#post_json(url, payload, headers: {}, timeout: nil) ⇒ Response

Parameters:

  • url (String)
  • payload (Hash)

    request body, sent as application/json

  • headers (Hash{String => String}) (defaults to: {})
  • timeout (Numeric, nil) (defaults to: nil)

    open/read timeout in seconds

Returns:

Raises:

  • (NetworkError)

    on DNS, TLS, connect, or timeout failures



63
64
65
66
67
68
69
70
# File 'lib/keycardai/oauth/http.rb', line 63

def post_json(url, payload, headers: {}, timeout: nil)
  uri = URI(url)
  request = Net::HTTP::Post.new(uri)
  request["Content-Type"] = "application/json"
  headers.each { |name, value| request[name] = value }
  request.body = JSON.dump(payload)
  perform(uri, request, timeout)
end