Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Basic rest-client only post_file. #5

Merged
merged 1 commit into from Jul 5, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 10 additions & 1 deletion lib/rest/client.rb
Expand Up @@ -19,6 +19,8 @@ def initialize(msg=nil)
end
end

require_relative 'wrappers/base_wrapper'

def self.puts(s)
Kernel.puts("rest gem: #{s}")
end
Expand Down Expand Up @@ -49,7 +51,6 @@ def initialize(options={})
@wrapper = Rest::Wrappers::NetHttpPersistentWrapper.new(self)
Rest.puts "Using net-http-persistent gem."
else
require_relative 'wrappers/rest_client_wrapper'
@wrapper = Rest::Wrappers::RestClientWrapper.new
Rest.puts "Using rest-client gem. Please install 'typhoeus' or net-http-persistent gem for best performance."
end
Expand Down Expand Up @@ -134,6 +135,14 @@ def delete(url, req_hash={})
return res
end

def post_file(url, req_hash={})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the file passed in the param? What is the param name user should pass in? Comment would be good here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sample usage

def post_file(method, file_field, file, params_field, params = {})
  request_hash = {}
  request_hash[:headers] = common_headers.merge(@headers)
  request_hash[:body] = {params_field => params.to_json, file_field => file}

  @rest.post_file(url + method, request_hash)
end

  post_file("projects/#{@project_id}/codes", :file, File.new(file, 'rb'), :data, {:name => name, :runtime => runtime, :file_name => runner}.merge(options))

res = nil
perform_op do
res = @wrapper.post_file(url, req_hash)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool, can you add that to comments?

end
return res
end

def close
@wrapper.close
end
Expand Down
27 changes: 26 additions & 1 deletion lib/rest/wrappers/base_wrapper.rb
@@ -1,7 +1,32 @@
class BaseWrapper

def post_file(url, req_hash={})
response = nil
begin
if req_hash[:body]
req_hash = req_hash.merge(req_hash[:body])
req_hash.delete(:body)
end

headers = {}
if req_hash[:headers]
headers = req_hash[:headers]
req_hash.delete(:headers)
end

r2 = RestClient.post(url, req_hash, headers)
response = Rest::Wrappers::RestClientResponseWrapper.new(r2)
rescue RestClient::Exception => ex
raise Rest::Wrappers::RestClientExceptionWrapper.new(ex)
end
response
end

# if wrapper has a close/shutdown, override this
def close

end
end
end

# we need it for post_file, ok as gem already depends on it
require_relative 'rest_client_wrapper'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be at the top?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no it's ok. rest_client_wrapper inherits base_wrapper, so need to be included after it, but as base uses rest for post_file (actually, just needs exception classes) it's good to include it here.

3 changes: 1 addition & 2 deletions lib/rest/wrappers/net_http_persistent_wrapper.rb
@@ -1,5 +1,4 @@
require 'net/http/persistent'
require_relative 'base_wrapper'

module Rest

Expand Down Expand Up @@ -155,4 +154,4 @@ def close

end

end
end
4 changes: 2 additions & 2 deletions lib/rest/wrappers/rest_client_wrapper.rb
Expand Up @@ -25,7 +25,7 @@ def body

end

class RestClientWrapper
class RestClientWrapper < BaseWrapper

def default_headers
{}
Expand Down Expand Up @@ -97,4 +97,4 @@ def delete(url, req_hash={})

end

end
end
4 changes: 2 additions & 2 deletions lib/rest/wrappers/typhoeus_wrapper.rb
Expand Up @@ -11,7 +11,7 @@ def initialize(response)
end
end

class TyphoeusWrapper
class TyphoeusWrapper < BaseWrapper

def default_typhoeus_options
req_hash = {}
Expand Down Expand Up @@ -78,4 +78,4 @@ def delete(url, req_hash={})

end

end
end