diff options
author | Luke Kanies <luke@madstop.com> | 2009-03-13 17:39:51 -0500 |
---|---|---|
committer | James Turnbull <james@lovedthanlost.net> | 2009-03-20 18:27:07 +1100 |
commit | 15740fe090365a81202fd44244e88b976f1d14d7 (patch) | |
tree | e94b35381a30b273f2ac80dbe9a4051f47bd0e3f /lib | |
parent | ef4fa681be07881bab3e1e920585cc4fbbf6f4d1 (diff) | |
download | puppet-15740fe090365a81202fd44244e88b976f1d14d7.tar.gz puppet-15740fe090365a81202fd44244e88b976f1d14d7.tar.xz puppet-15740fe090365a81202fd44244e88b976f1d14d7.zip |
Moving the REST API functions into a module
This module is now used by the client and
server side, rather than having a Handler module
that's 90% server functionality but also used by
the client.
While we don't automatically get api choice from this,
it at least provides a pattern for how we'll handle API
development over time.
Signed-off-by: Luke Kanies <luke@madstop.com>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/puppet/indirector/rest.rb | 7 | ||||
-rw-r--r-- | lib/puppet/network/http/api.rb | 4 | ||||
-rw-r--r-- | lib/puppet/network/http/api/v1.rb | 60 | ||||
-rw-r--r-- | lib/puppet/network/http/handler.rb | 61 |
4 files changed, 70 insertions, 62 deletions
diff --git a/lib/puppet/indirector/rest.rb b/lib/puppet/indirector/rest.rb index 38ecdb758..c3d689e93 100644 --- a/lib/puppet/indirector/rest.rb +++ b/lib/puppet/indirector/rest.rb @@ -2,11 +2,11 @@ require 'net/http' require 'uri' require 'puppet/network/http_pool' -require 'puppet/network/http/handler' +require 'puppet/network/http/api/v1' # Access objects via REST class Puppet::Indirector::REST < Puppet::Indirector::Terminus - include Puppet::Network::HTTP::Handler + include Puppet::Network::HTTP::API::V1 class << self attr_reader :server_setting, :port_setting @@ -64,9 +64,6 @@ class Puppet::Indirector::REST < Puppet::Indirector::Terminus end def find(request) - p model - p indirection - p indirection.model deserialize network(request).get(indirection2uri(request), headers) end diff --git a/lib/puppet/network/http/api.rb b/lib/puppet/network/http/api.rb new file mode 100644 index 000000000..8b1b747ac --- /dev/null +++ b/lib/puppet/network/http/api.rb @@ -0,0 +1,4 @@ +require 'puppet/network/http' + +class Puppet::Network::HTTP::API +end diff --git a/lib/puppet/network/http/api/v1.rb b/lib/puppet/network/http/api/v1.rb new file mode 100644 index 000000000..2ee1a815f --- /dev/null +++ b/lib/puppet/network/http/api/v1.rb @@ -0,0 +1,60 @@ +require 'puppet/network/http/api' + +module Puppet::Network::HTTP::API::V1 + # How we map http methods and the indirection name in the URI + # to an indirection method. + METHOD_MAP = { + "GET" => { + :plural => :search, + :singular => :find + }, + "PUT" => { + :singular => :save + }, + "DELETE" => { + :singular => :destroy + } + } + + def uri2indirection(http_method, uri, params) + environment, indirection, key = uri.split("/", 4)[1..-1] # the first field is always nil because of the leading slash + + raise ArgumentError, "The environment must be purely alphanumeric, not '%s'" % environment unless environment =~ /^\w+$/ + raise ArgumentError, "The indirection name must be purely alphanumeric, not '%s'" % indirection unless indirection =~ /^\w+$/ + + method = indirection_method(http_method, indirection) + + params[:environment] = environment + + raise ArgumentError, "No request key specified in %s" % uri if key == "" or key.nil? + + key = URI.unescape(key) + + Puppet::Indirector::Request.new(indirection, method, key, params) + end + + def indirection2uri(request) + indirection = request.method == :search ? request.indirection_name.to_s + "s" : request.indirection_name.to_s + "/#{request.environment.to_s}/#{indirection}/#{request.escaped_key}#{request.query_string}" + end + + def indirection_method(http_method, indirection) + unless METHOD_MAP[http_method] + raise ArgumentError, "No support for http method %s" % http_method + end + + unless method = METHOD_MAP[http_method][plurality(indirection)] + raise ArgumentError, "No support for plural %s operations" % http_method + end + + return method + end + + def plurality(indirection) + result = (indirection == handler.to_s + "s") ? :plural : :singular + + indirection.sub!(/s$/, '') if result + + result + end +end diff --git a/lib/puppet/network/http/handler.rb b/lib/puppet/network/http/handler.rb index 610aa0a3f..76f07ed73 100644 --- a/lib/puppet/network/http/handler.rb +++ b/lib/puppet/network/http/handler.rb @@ -1,22 +1,10 @@ module Puppet::Network::HTTP end -module Puppet::Network::HTTP::Handler +require 'puppet/network/http/api/v1' - # How we map http methods and the indirection name in the URI - # to an indirection method. - METHOD_MAP = { - "GET" => { - :plural => :search, - :singular => :find - }, - "PUT" => { - :singular => :save - }, - "DELETE" => { - :singular => :destroy - } - } +module Puppet::Network::HTTP::Handler + include Puppet::Network::HTTP::API::V1 attr_reader :model, :server, :handler @@ -54,51 +42,10 @@ module Puppet::Network::HTTP::Handler send("do_%s" % indirection_request.method, indirection_request, request, response) rescue Exception => e + puts e.backtrace return do_exception(response, e) end - def uri2indirection(http_method, uri, params) - environment, indirection, key = uri.split("/", 4)[1..-1] # the first field is always nil because of the leading slash - - raise ArgumentError, "The environment must be purely alphanumeric, not '%s'" % environment unless environment =~ /^\w+$/ - raise ArgumentError, "The indirection name must be purely alphanumeric, not '%s'" % indirection unless indirection =~ /^\w+$/ - - method = indirection_method(http_method, indirection) - - params[:environment] = environment - - raise ArgumentError, "No request key specified in %s" % uri if key == "" or key.nil? - - key = URI.unescape(key) - - Puppet::Indirector::Request.new(indirection, method, key, params) - end - - def indirection2uri(request) - indirection = request.method == :search ? request.indirection_name.to_s + "s" : request.indirection_name.to_s - "/#{request.environment.to_s}/#{indirection}/#{request.escaped_key}#{request.query_string}" - end - - def indirection_method(http_method, indirection) - unless METHOD_MAP[http_method] - raise ArgumentError, "No support for http method %s" % http_method - end - - unless method = METHOD_MAP[http_method][plurality(indirection)] - raise ArgumentError, "No support for plural %s operations" % http_method - end - - return method - end - - def plurality(indirection) - result = (indirection == handler.to_s + "s") ? :plural : :singular - - indirection.sub!(/s$/, '') if result - - result - end - # Set the response up, with the body and status. def set_response(response, body, status = 200) raise NotImplementedError |