Class: CFoundry::BaseClient
- Inherits:
-
Object
- Object
- CFoundry::BaseClient
- Extended by:
- Forwardable
- Includes:
- ProxyOptions
- Defined in:
- lib/cfoundry/baseclient.rb
Overview
:nodoc:
Instance Attribute Summary collapse
-
#client_id ⇒ Object
Returns the value of attribute client_id.
-
#client_secret ⇒ Object
Returns the value of attribute client_secret.
-
#rest_client ⇒ Object
readonly
Returns the value of attribute rest_client.
Instance Method Summary collapse
- #delete(*args) ⇒ Object
- #get(*args) ⇒ Object
-
#info ⇒ Object
Cloud metadata.
-
#initialize(target, token = nil, options = {}) ⇒ BaseClient
constructor
A new instance of BaseClient.
- #password_score(password) ⇒ Object
- #post(*args) ⇒ Object
- #put(*args) ⇒ Object
- #refresh_token! ⇒ Object
- #request(method, *args) ⇒ Object
- #request_raw(method, path, options) ⇒ Object
- #stream_url(url, &blk) ⇒ Object
- #token=(token) ⇒ Object
- #trace=(trace) ⇒ Object
- #uaa ⇒ Object
Methods included from ProxyOptions
Constructor Details
#initialize(target, token = nil, options = {}) ⇒ BaseClient
Returns a new instance of BaseClient.
20 21 22 23 24 25 26 27 |
# File 'lib/cfoundry/baseclient.rb', line 20 def initialize(target, token = nil, = {}) @rest_client = CFoundry::RestClient.new(target, token) self.client_id = [:client_id] self.client_secret = [:client_secret] self.trace = false self.backtrace = false self.log = false end |
Instance Attribute Details
#client_id ⇒ Object
Returns the value of attribute client_id.
15 16 17 |
# File 'lib/cfoundry/baseclient.rb', line 15 def client_id @client_id end |
#client_secret ⇒ Object
Returns the value of attribute client_secret.
15 16 17 |
# File 'lib/cfoundry/baseclient.rb', line 15 def client_secret @client_secret end |
#rest_client ⇒ Object (readonly)
Returns the value of attribute rest_client.
14 15 16 |
# File 'lib/cfoundry/baseclient.rb', line 14 def rest_client @rest_client end |
Instance Method Details
#delete(*args) ⇒ Object
73 74 75 |
# File 'lib/cfoundry/baseclient.rb', line 73 def delete(*args) request("DELETE", *args) end |
#get(*args) ⇒ Object
69 70 71 |
# File 'lib/cfoundry/baseclient.rb', line 69 def get(*args) request("GET", *args) end |
#info ⇒ Object
Cloud metadata
65 66 67 |
# File 'lib/cfoundry/baseclient.rb', line 65 def info get("info", :accept => :json) end |
#password_score(password) ⇒ Object
46 47 48 |
# File 'lib/cfoundry/baseclient.rb', line 46 def password_score(password) uaa ? uaa.password_score(password) : :unknown end |
#post(*args) ⇒ Object
77 78 79 |
# File 'lib/cfoundry/baseclient.rb', line 77 def post(*args) request("POST", *args) end |
#put(*args) ⇒ Object
81 82 83 |
# File 'lib/cfoundry/baseclient.rb', line 81 def put(*args) request("PUT", *args) end |
#refresh_token! ⇒ Object
100 101 102 |
# File 'lib/cfoundry/baseclient.rb', line 100 def refresh_token! self.token = uaa.try_to_refresh_token! end |
#request(method, *args) ⇒ Object
85 86 87 88 89 90 91 92 93 94 |
# File 'lib/cfoundry/baseclient.rb', line 85 def request(method, *args) if needs_token_refresh? token.auth_header = nil refresh_token! end path, = normalize_arguments(args) request, response = request_raw(method, path, ) handle_response(response, , request) end |
#request_raw(method, path, options) ⇒ Object
96 97 98 |
# File 'lib/cfoundry/baseclient.rb', line 96 def request_raw(method, path, ) @rest_client.request(method, path, ) end |
#stream_url(url, &blk) ⇒ Object
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/cfoundry/baseclient.rb', line 104 def stream_url(url, &blk) uri = URI.parse(url) opts = {} if uri.scheme == "https" opts[:use_ssl] = true opts[:verify_mode] = OpenSSL::SSL::VERIFY_NONE end Net::HTTP.start(uri.host, uri.port, *(uri), opts) do |http| http.read_timeout = 5 req = Net::HTTP::Get.new(uri.request_uri) req["Authorization"] = token.auth_header if token http.request(req) do |response| case response when Net::HTTPOK response.read_body(&blk) when Net::HTTPNotFound raise CFoundry::NotFound.new(response.body, 404) when Net::HTTPForbidden raise CFoundry::Denied.new(response.body, 403) when Net:: raise CFoundry::.new(response.body, 401) else raise CFoundry::BadResponse.new(response.body, response.code) end end end end |
#token=(token) ⇒ Object
50 51 52 53 54 55 56 57 |
# File 'lib/cfoundry/baseclient.rb', line 50 def token=(token) if token.is_a?(String) token = CFoundry::AuthToken.new(token) end @rest_client.token = token @uaa.token = token if @uaa end |
#trace=(trace) ⇒ Object
59 60 61 62 |
# File 'lib/cfoundry/baseclient.rb', line 59 def trace=(trace) @rest_client.trace = trace @uaa.trace = trace if @uaa end |
#uaa ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/cfoundry/baseclient.rb', line 29 def uaa @uaa ||= begin endpoint = info[:authorization_endpoint] if endpoint uaa = CFoundry::UAAClient.new(endpoint, client_id || "cf", client_secret: client_secret) uaa.trace = trace uaa.token = token uaa else nil end end end |