Class: CFoundry::BaseClient

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

Overview

:nodoc:

Direct Known Subclasses

Client, V2::Base

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(target = "https://api.cloudfoundry.com", token = nil) ⇒ BaseClient

Returns a new instance of BaseClient.



18
19
20
21
22
23
# File 'lib/cfoundry/baseclient.rb', line 18

def initialize(target = "https://api.cloudfoundry.com", 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.



12
13
14
# File 'lib/cfoundry/baseclient.rb', line 12

def rest_client
  @rest_client
end

Instance Method Details

#delete(*args) ⇒ Object



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

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

#get(*args) ⇒ Object



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

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

#infoObject

Cloud metadata



59
60
61
# File 'lib/cfoundry/baseclient.rb', line 59

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

#password_score(password) ⇒ Object



40
41
42
# File 'lib/cfoundry/baseclient.rb', line 40

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

#post(*args) ⇒ Object



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

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

#put(*args) ⇒ Object



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

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

#refresh_token!Object



94
95
96
# File 'lib/cfoundry/baseclient.rb', line 94

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

#request(method, *args) ⇒ Object



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

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



90
91
92
# File 'lib/cfoundry/baseclient.rb', line 90

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

#stream_url(url, &blk) ⇒ Object



98
99
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
# File 'lib/cfoundry/baseclient.rb', line 98

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, 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



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

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



53
54
55
56
# File 'lib/cfoundry/baseclient.rb', line 53

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

#uaaObject



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

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

    if endpoint
      uaa = CFoundry::UAAClient.new(endpoint)
      uaa.trace = trace
      uaa.token = token
      uaa
    else
      nil
    end
  end
end