Skip to content

Commit

Permalink
sending post body if present
Browse files Browse the repository at this point in the history
  • Loading branch information
guilhermesilveira committed Oct 24, 2010
1 parent 0e2e391 commit 6d5ef6c
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 64 deletions.
7 changes: 6 additions & 1 deletion lib/restfulie/client/feature/base_request.rb
Expand Up @@ -19,7 +19,12 @@ def request!(method, host, path, request, flow, env)
cached = Restfulie::Client.cache_provider.get([host, path], http_request, method)
return cached if cached

response = http_request.send(method, path, request.headers)
if env[:body]
response = http_request.send(method, path, env[:body], request.headers)
else
response = http_request.send(method, path, request.headers)
end

rescue Exception => e
response = e
end
Expand Down
32 changes: 32 additions & 0 deletions lib/restfulie/client/feature/serialize_body.rb
@@ -0,0 +1,32 @@
class Restfulie::Client::Feature::SerializeBody

def execute(flow, request, response, env)

if should_have_payload?(request.method)

payload = env[:body]
if payload && !(payload.kind_of?(String) && payload.empty?)
type = headers['Content-Type']
raise Restfulie::Common::Error::RestfulieError, "Missing content type related to the data to be submitted" unless type

marshaller = Restfulie::Common::Converter.content_type_for(type)
raise Restfulie::Common::Error::RestfulieError, "Missing content type for #{type} related to the data to be submitted" unless marshaller

rel = request.respond_to?(:rel) ? request.rel : ""
env[:body] = marshaller.marshal(payload, { :rel => rel, :recipe => env[:recipe] })
end

end

flow.continue(request, response, env)
end

protected

PAYLOAD_METHODS = {:put=>true,:post=>true,:patch=>true}

def should_have_payload?(method)
PAYLOAD_METHODS[method]
end

end
63 changes: 0 additions & 63 deletions lib/restfulie/client/http/request_marshaller.rb
Expand Up @@ -11,70 +11,7 @@ def initialize(requester)
@requester.response_handler= Restfulie::Client::Response::CacheHandler.new(@requester.response_handler)
@requester.response_handler= Restfulie::Client::Response::CreatedRedirect.new(self, @requester.response_handler)
end

def accepts(media_types)
@default_representation = Restfulie::Common::Converter.content_type_for(media_types)
delegate(:accepts, media_types)
end

# Executes super if its a raw request, returning the content itself.
# otherwise tries to parse the content with a mediatype handler or returns the response itself.
def request!(method, path, *args)
if has_payload?(method, path, *args)
recipe = get_recipe(*args)

payload = get_payload(method, path, *args)
rel = self.respond_to?(:rel) ? self.rel : ""
type = headers['Content-Type']
raise Restfulie::Common::Error::RestfulieError, "Missing content type related to the data to be submitted" unless type
marshaller = Restfulie::Common::Converter.content_type_for(type)
raise Restfulie::Common::Error::RestfulieError, "Missing content type for #{type} related to the data to be submitted" unless marshaller
payload = marshaller.marshal(payload, { :rel => rel, :recipe => recipe }) unless payload.nil? || (payload.kind_of?(String) && payload.empty?)
args = set_marshalled_payload(method, path, payload, *args)
args = add_representation_headers(method, path, marshaller, *args)
end

if @acceptable_mediatypes
unmarshaller = Restfulie::Common::Converter.content_type_for(@acceptable_mediatypes)
args = add_representation_headers(method, path, unmarshaller, *args)
end

delegate(:request!, method, path, *args)

end

private

def get_recipe(*args)
headers_and_recipe = args.extract_options!
recipe = headers_and_recipe.delete(:recipe)
args << headers_and_recipe
recipe
end

def has_payload?(method, path, *args)
[:put,:post,:patch].include?(method)
end

def get_payload(method, path, *args)
args.extract_options! #remove header
args.shift #payload
end

def set_marshalled_payload(method, path, payload, *args)
headers = args.extract_options!
args.tap do |a|
a.shift #old payload
a << payload << headers
end
end

def add_representation_headers(method, path, representation, *args)
headers = args.extract_options!
headers = headers.merge(representation.headers[method] || {})
args << headers
args
end
end
end
end
Expand Down

0 comments on commit 6d5ef6c

Please sign in to comment.