Class: Rackspace::Connection
- Inherits:
-
Object
- Object
- Rackspace::Connection
- Defined in:
- lib/rackspace/connection.rb
Constant Summary collapse
- AUTH_URL =
"https://auth.api.rackspacecloud.com"
- VERSION_URL =
"https://servers.api.rackspacecloud.com"
Class Method Summary collapse
-
.api_key ⇒ Object
This returns the API key being used for calls.
-
.api_user ⇒ Object
This returns the API user being used for calls.
-
.api_version ⇒ Object
This returns the API version being used for calls.
-
.auth_response ⇒ Object
This caches the authentication response for subsequent usage.
-
.auth_token ⇒ Object
This is the auth token provided by Rackspace after a successful authentication.
-
.authenticate ⇒ Object
This authenticates with Rackspace and returns the information necessary to make subsequent authenticated calls to the API.
-
.cdn_management_url ⇒ Object
This returns the root URL for CDN Cloud Files API queries (not yet implemented).
-
.default_headers ⇒ Object
These are default headers we need to use on all requests.
-
.delete(url, headers = {}) ⇒ Object
This performs a basic DELETE request using the supplied URL and headers.
-
.get(url, headers = {}) ⇒ Object
This performs a basic GET request using the supplied URL and headers.
-
.http(method, *args) ⇒ Object
This will perform an HTTP call with the specified method, and arguments It will also pick up if the response is that the request was unauthorized, and will attempt the same request again after re-authenticating (in case the auth token has expired).
-
.init(user, key, version = "v1.0") ⇒ Object
This initializes the Rackspace environment with the data necessary to make API calls.
-
.initialized? ⇒ Boolean
This returns whether or not we’ve been initialized yet.
-
.post(url, payload = {}, headers = {}) ⇒ Object
This performs a basic POST request using the supplied URL, payload and headers.
-
.put(url, payload = {}, headers = {}) ⇒ Object
This performs a basic PUT request using the supplied URL, payload and headers.
-
.server_management_url ⇒ Object
This returns the root URL for Cloud Servers API queries.
-
.storage_url ⇒ Object
This returns the root URL for Cloud Files API queries (not yet implemented).
-
.versions ⇒ Object
This returns the available versions of the API.
Class Method Details
.api_key ⇒ Object
This returns the API key being used for calls
21 22 23 |
# File 'lib/rackspace/connection.rb', line 21 def api_key @key end |
.api_user ⇒ Object
This returns the API user being used for calls
16 17 18 |
# File 'lib/rackspace/connection.rb', line 16 def api_user @user end |
.api_version ⇒ Object
This returns the API version being used for calls
26 27 28 |
# File 'lib/rackspace/connection.rb', line 26 def api_version @version end |
.auth_response ⇒ Object
This caches the authentication response for subsequent usage
53 54 55 |
# File 'lib/rackspace/connection.rb', line 53 def auth_response @auth_response ||= self.authenticate end |
.auth_token ⇒ Object
This is the auth token provided by Rackspace after a successful authentication
58 59 60 |
# File 'lib/rackspace/connection.rb', line 58 def auth_token self.auth_response[:auth_token] end |
.authenticate ⇒ Object
This authenticates with Rackspace and returns the information necessary to make subsequent authenticated calls to the API
36 37 38 39 40 |
# File 'lib/rackspace/connection.rb', line 36 def authenticate raise Rackspace::NotInitialized unless self.initialized? headers = RestClient::Request.execute(:method => :get, :url => "#{AUTH_URL}/#{self.api_version}", :headers => {"X-Auth-User" => self.api_user, "X-Auth-Key" => self.api_key}, :raw_response => true).headers {:auth_token => headers[:x_auth_token], :storage_url => headers[:x_storage_url], :server_management_url => headers[:x_server_management_url], :cdn_management_url => headers[:x_cdn_management_url]} end |
.cdn_management_url ⇒ Object
This returns the root URL for CDN Cloud Files API queries (not yet implemented)
73 74 75 |
# File 'lib/rackspace/connection.rb', line 73 def cdn_management_url self.auth_response[:cdn_management_url] end |
.default_headers ⇒ Object
These are default headers we need to use on all requests
43 44 45 |
# File 'lib/rackspace/connection.rb', line 43 def default_headers {:accept => "application/json", :content_type => "application/json"} end |
.delete(url, headers = {}) ⇒ Object
This performs a basic DELETE request using the supplied URL and headers
93 94 95 |
# File 'lib/rackspace/connection.rb', line 93 def delete(url, headers = {}) http :delete, "#{url}.json", headers end |
.get(url, headers = {}) ⇒ Object
This performs a basic GET request using the supplied URL and headers
78 79 80 |
# File 'lib/rackspace/connection.rb', line 78 def get(url, headers = {}) http :get, "#{url}.json", headers end |
.http(method, *args) ⇒ Object
This will perform an HTTP call with the specified method, and arguments It will also pick up if the response is that the request was unauthorized, and will attempt the same request again after re-authenticating (in case the auth token has expired)
100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/rackspace/connection.rb', line 100 def http(method, *args) args.last.merge!(self.default_headers).merge!("X-Auth-Token" => self.auth_token) response = RestClient.send(method, *args) @retried = false response rescue RestClient::Unauthorized @auth_response = nil if @retried raise else @retried = true retry end end |
.init(user, key, version = "v1.0") ⇒ Object
This initializes the Rackspace environment with the data necessary to make API calls
7 8 9 10 11 12 13 |
# File 'lib/rackspace/connection.rb', line 7 def init(user, key, version = "v1.0") @user = user @key = key @version = version raise Rackspace::InvalidVersion unless self.versions.include?(version) @initialized = true end |
.initialized? ⇒ Boolean
This returns whether or not we’ve been initialized yet
31 32 33 |
# File 'lib/rackspace/connection.rb', line 31 def initialized? @initialized || false end |
.post(url, payload = {}, headers = {}) ⇒ Object
This performs a basic POST request using the supplied URL, payload and headers
83 84 85 |
# File 'lib/rackspace/connection.rb', line 83 def post(url, payload = {}, headers = {}) http :post, "#{url}.json", payload.to_json, headers end |
.put(url, payload = {}, headers = {}) ⇒ Object
This performs a basic PUT request using the supplied URL, payload and headers
88 89 90 |
# File 'lib/rackspace/connection.rb', line 88 def put(url, payload = {}, headers = {}) http :put, "#{url}.json", payload.to_json, headers end |
.server_management_url ⇒ Object
This returns the root URL for Cloud Servers API queries
68 69 70 |
# File 'lib/rackspace/connection.rb', line 68 def server_management_url self.auth_response[:server_management_url] end |
.storage_url ⇒ Object
This returns the root URL for Cloud Files API queries (not yet implemented)
63 64 65 |
# File 'lib/rackspace/connection.rb', line 63 def storage_url self.auth_response[:storage_url] end |
.versions ⇒ Object
This returns the available versions of the API
48 49 50 |
# File 'lib/rackspace/connection.rb', line 48 def versions JSON.parse(RestClient.get("#{VERSION_URL}/.json", self.default_headers))["versions"].collect { |v| v["id"] }.uniq end |