Class: Ropenstack::Rest
- Inherits:
-
Object
- Object
- Ropenstack::Rest
- Defined in:
- lib/ropenstack/common/rest.rb
Overview
-
Name: Rest
* Description: A generic wrapper for basic rest functions with JSON data which return ruby hashes. * Author: Sam “Tehsmash” Betts * Date: 12/18/2012
Direct Known Subclasses
Instance Method Summary collapse
-
#build_headers(token) ⇒ Object
Builds headers for requests which send JSON data, if a keystone token is supplied it adds the X-Auth-Token field with the keystone token.
-
#build_http(uri, timeout) ⇒ Object
Build a HTTP object having been given a timeout and a URI object Returns Net::HTTP object.
-
#delete_request(uri, token = nil, manage_errors = true) ⇒ Object
Wrapper function for delete requests, just provide a uri and it will return you a hash with the result data.
-
#do_request(uri, request, manage_errors = true, timeout = 10) ⇒ Object
The function which you call to perform a http request using the request object given in the parameters.
-
#error_manager(uri, response) ⇒ Object
All responses from openstack where any errors need to be caught are passed through this function.
-
#get_request(uri, token = nil, manage_errors = true) ⇒ Object
Wrapper function for a get request, just provide a uri and it will return you a hash with the result data.
-
#post_request(uri, body, token = nil, manage_errors = true) ⇒ Object
Wrapper function for a put request, just provide a uri and a hash of the data to send, then it will return you a hash with the result data.
-
#put_request(uri, body, token = nil, manage_errors = true) ⇒ Object
Wrapper function for a put request, just provide a uri and a hash of the data to send, then it will return you a hash with the result data.
Instance Method Details
#build_headers(token) ⇒ Object
Builds headers for requests which send JSON data, if a keystone token is supplied
it adds the X-Auth-Token field with the keystone token. Returns a hash which represents the http headers in a format accepted by Net::HTTP.
61 62 63 64 65 66 67 |
# File 'lib/ropenstack/common/rest.rb', line 61 def build_headers(token) headers = {'Content-Type' =>'application/json'} unless token.nil? headers['X-Auth-Token'] = token end return headers end |
#build_http(uri, timeout) ⇒ Object
Build a HTTP object having been given a timeout and a URI object Returns Net::HTTP object.
17 18 19 20 21 22 23 24 |
# File 'lib/ropenstack/common/rest.rb', line 17 def build_http(uri, timeout) http = Net::HTTP.new(uri.host, uri.port) if(timeout > 0) http.open_timeout = timeout http.read_timeout = timeout end return http end |
#delete_request(uri, token = nil, manage_errors = true) ⇒ Object
Wrapper function for delete requests, just provide a uri and it will return you a hash with the result data. For authenticated transactions a token can be provided. Implemented using the do_request method.
108 109 110 111 |
# File 'lib/ropenstack/common/rest.rb', line 108 def delete_request(uri, token = nil, manage_errors = true) request = Net::HTTP::Delete.new(uri.request_uri, initheader = build_headers(token)) return do_request(uri, request, manage_errors) end |
#do_request(uri, request, manage_errors = true, timeout = 10) ⇒ Object
The function which you call to perform a http request using the request object given in the parameters. By default manage errors is true, so all responses are passed through the error manager which converts the into Ropenstack errors.
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/ropenstack/common/rest.rb', line 75 def do_request(uri, request, manage_errors = true, timeout = 10) begin http = build_http(uri, timeout) if(manage_errors) return error_manager(uri, http.request(request)) else http.request(request) return { "Success" => true } end rescue Timeout::Error raise Ropenstack::TimeoutError, "It took longer than #{timeout} to connect to #{uri.to_s}" rescue Errno::ECONNREFUSED raise Ropenstack::TimeoutError, "It took longer than #{timeout} to connect to #{uri.to_s}" end end |
#error_manager(uri, response) ⇒ Object
All responses from openstack where any errors need to be caught are passed through this function. Unless a successful response is passed it will throw a Ropenstack error. If successful returns a hash of response body, unless response body is nil then it returns an empty hash.
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/ropenstack/common/rest.rb', line 33 def error_manager(uri, response) case response when Net::HTTPSuccess then # This covers cases where the response may not validate as JSON. begin data = JSON.parse(response.body) rescue data = {} end ## Get the Headers out of the response object data['headers'] = response.to_hash() return data when Net::HTTPBadRequest raise Ropenstack::MalformedRequestError, response.body when Net::HTTPNotFound raise Ropenstack::NotFoundError, "URI: #{uri} \n" + response.body when Net::HTTPUnauthorized raise Ropenstack::UnauthorisedError, response.body else raise Ropenstack::RopenstackError, response.body end end |
#get_request(uri, token = nil, manage_errors = true) ⇒ Object
Wrapper function for a get request, just provide a uri and it will return you a hash with the result data. For authenticated transactions a token can be provided. Implemented using the do_request method.
97 98 99 100 |
# File 'lib/ropenstack/common/rest.rb', line 97 def get_request(uri, token = nil, manage_errors = true) request = Net::HTTP::Get.new(uri.request_uri, initheader = build_headers(token)) return do_request(uri, request, manage_errors) end |
#post_request(uri, body, token = nil, manage_errors = true) ⇒ Object
Wrapper function for a put request, just provide a uri and a hash of the data to send, then it will return you a hash with the result data. For authenticated transactions a token can be provided.
132 133 134 135 136 |
# File 'lib/ropenstack/common/rest.rb', line 132 def post_request(uri, body, token = nil, manage_errors = true) request = Net::HTTP::Post.new(uri.request_uri, initheader = build_headers(token)) request.body = body.to_json return do_request(uri, request, manage_errors) end |
#put_request(uri, body, token = nil, manage_errors = true) ⇒ Object
Wrapper function for a put request, just provide a uri and a hash of the data to send, then it will return you a hash with the result data. For authenticated transactions a token can be provided. Implemented using the do_request method
120 121 122 123 124 |
# File 'lib/ropenstack/common/rest.rb', line 120 def put_request(uri, body, token = nil, manage_errors = true) request = Net::HTTP::Put.new(uri.request_uri, initheader = build_headers(token)) request.body = body.to_json return do_request(uri, request, manage_errors) end |