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) ⇒ BaseClient

Returns a new instance of BaseClient.



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

def initialize(target, token = nil)
  @rest_client = CFoundry::RestClient.new(target, token)
  self.trace = false
  self.backtrace = false
  self.log = false
end

Instance Attribute Details

#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

Instance Method Details

#delete(*args) ⇒ Object



69
70
71
# File 'lib/cfoundry/baseclient.rb', line 69

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

#get(*args) ⇒ Object



65
66
67
# File 'lib/cfoundry/baseclient.rb', line 65

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

#infoObject

Cloud metadata



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

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

#password_score(password) ⇒ Object



42
43
44
# File 'lib/cfoundry/baseclient.rb', line 42

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

#post(*args) ⇒ Object



73
74
75
# File 'lib/cfoundry/baseclient.rb', line 73

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

#put(*args) ⇒ Object



77
78
79
# File 'lib/cfoundry/baseclient.rb', line 77

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

#refresh_token!Object



96
97
98
# File 'lib/cfoundry/baseclient.rb', line 96

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

#request(method, *args) ⇒ Object



81
82
83
84
85
86
87
88
89
90
# File 'lib/cfoundry/baseclient.rb', line 81

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



92
93
94
# File 'lib/cfoundry/baseclient.rb', line 92

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

#stream_url(url, &blk) ⇒ Object



100
101
102
103
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
# File 'lib/cfoundry/baseclient.rb', line 100

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



46
47
48
49
50
51
52
53
# File 'lib/cfoundry/baseclient.rb', line 46

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



55
56
57
58
# File 'lib/cfoundry/baseclient.rb', line 55

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

#uaaObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/cfoundry/baseclient.rb', line 27

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

    if endpoint
      uaa = CFoundry::UAAClient.new(endpoint, "cf", http_proxy: http_proxy, https_proxy: https_proxy)
      uaa.trace = trace
      uaa.token = token
      uaa
    else
      nil
    end
  end
end