Class: CFoundry::BaseClient

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
ProxyOptions
Defined in:
lib/cfoundry/baseclient.rb

Overview

:nodoc:

Direct Known Subclasses

Client, V2::Base

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ProxyOptions

#proxy_options_for

Constructor Details

#initialize(target, token = nil, options = {}) ⇒ BaseClient

Returns a new instance of BaseClient.



20
21
22
23
24
25
26
27
28
# File 'lib/cfoundry/baseclient.rb', line 20

def initialize(target, token = nil, options = {})
  @rest_client = CFoundry::RestClient.new(target, token)
  self.client_id = options[:client_id]
  self.client_secret = options[:client_secret]
  self.skip_ssl_validation = options[:skip_ssl_validation]
  self.trace = false
  self.backtrace = false
  self.log = false
end

Instance Attribute Details

#client_idObject

Returns the value of attribute client_id.



15
16
17
# File 'lib/cfoundry/baseclient.rb', line 15

def client_id
  @client_id
end

#client_secretObject

Returns the value of attribute client_secret.



15
16
17
# File 'lib/cfoundry/baseclient.rb', line 15

def client_secret
  @client_secret
end

#rest_clientObject (readonly)

Returns the value of attribute rest_client.



14
15
16
# File 'lib/cfoundry/baseclient.rb', line 14

def rest_client
  @rest_client
end

#skip_ssl_validationObject

Returns the value of attribute skip_ssl_validation.



15
16
17
# File 'lib/cfoundry/baseclient.rb', line 15

def skip_ssl_validation
  @skip_ssl_validation
end

Instance Method Details

#delete(*args) ⇒ Object



75
76
77
# File 'lib/cfoundry/baseclient.rb', line 75

def delete(*args)
  request("DELETE", *args)
end

#get(*args) ⇒ Object



71
72
73
# File 'lib/cfoundry/baseclient.rb', line 71

def get(*args)
  request("GET", *args)
end

#infoObject

Cloud metadata



67
68
69
# File 'lib/cfoundry/baseclient.rb', line 67

def info
  get("info", :accept => :json)
end

#password_score(password) ⇒ Object



48
49
50
# File 'lib/cfoundry/baseclient.rb', line 48

def password_score(password)
  uaa ? uaa.password_score(password) : :unknown
end

#post(*args) ⇒ Object



79
80
81
# File 'lib/cfoundry/baseclient.rb', line 79

def post(*args)
  request("POST", *args)
end

#put(*args) ⇒ Object



83
84
85
# File 'lib/cfoundry/baseclient.rb', line 83

def put(*args)
  request("PUT", *args)
end

#refresh_token!Object



102
103
104
# File 'lib/cfoundry/baseclient.rb', line 102

def refresh_token!
  self.token = uaa.try_to_refresh_token!
end

#request(method, *args) ⇒ Object



87
88
89
90
91
92
93
94
95
96
# File 'lib/cfoundry/baseclient.rb', line 87

def request(method, *args)
  if needs_token_refresh?
    token.auth_header = nil
    refresh_token!
  end

  path, options = normalize_arguments(args)
  request, response = request_raw(method, path, options)
  handle_response(response, options, request)
end

#request_raw(method, path, options) ⇒ Object



98
99
100
# File 'lib/cfoundry/baseclient.rb', line 98

def request_raw(method, path, options)
  @rest_client.request(method, path, options)
end

#stream_url(url, &blk) ⇒ Object



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
136
137
# File 'lib/cfoundry/baseclient.rb', line 106

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, *proxy_options_for(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::HTTPUnauthorized
        raise CFoundry::Unauthorized.new(response.body, 401)
      else
        raise CFoundry::BadResponse.new(response.body, response.code)
      end
    end
  end
end

#token=(token) ⇒ Object



52
53
54
55
56
57
58
59
# File 'lib/cfoundry/baseclient.rb', line 52

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



61
62
63
64
# File 'lib/cfoundry/baseclient.rb', line 61

def trace=(trace)
  @rest_client.trace = trace
  @uaa.trace = trace if @uaa
end

#uaaObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/cfoundry/baseclient.rb', line 30

def uaa
  @uaa ||= begin
    endpoint = info[:authorization_endpoint]

    if endpoint
      uaa = CFoundry::UAAClient.new(endpoint,
                                    client_id || "cf",
                                    client_secret: client_secret,
                                    skip_ssl_validation: skip_ssl_validation)
      uaa.trace = trace
      uaa.token = token
      uaa
    else
      nil
    end
  end
end