diff options
| author | Luc Donnet | 2015-04-22 10:38:49 +0200 |
|---|---|---|
| committer | Luc Donnet | 2015-04-22 10:38:49 +0200 |
| commit | 42c570ba88f073d0082b948ced1e3b6f57868afe (patch) | |
| tree | 9221feb52585362b8ec6a53baef4a7254747e178 /lib/ievkit/client.rb | |
| parent | 75b5d7d86f6b5a26b7f88eb7c6b788d59163ef5b (diff) | |
| download | chouette-core-42c570ba88f073d0082b948ced1e3b6f57868afe.tar.bz2 | |
Update import use
Diffstat (limited to 'lib/ievkit/client.rb')
| -rw-r--r-- | lib/ievkit/client.rb | 45 |
1 files changed, 44 insertions, 1 deletions
diff --git a/lib/ievkit/client.rb b/lib/ievkit/client.rb index 53eb61074..c304a029d 100644 --- a/lib/ievkit/client.rb +++ b/lib/ievkit/client.rb @@ -66,6 +66,15 @@ module Ievkit def post(url, options = {}) request :post, url, options end + + # Make a HTTP POST request + # + # @param url [String] The path, relative to {#api_endpoint} + # @param options [Hash] Body and header params for request + # @return [Sawyer::Resource] + def multipart_post(url, options = {}) + multipart_request :post, url, options + end # Make a HTTP PUT request # @@ -140,6 +149,27 @@ module Ievkit # Hypermedia agent for the Iev API # # @return [Sawyer::Agent] + def multipart_agent + @agent ||= Sawyer::Agent.new(api_endpoint, sawyer_options) do |http| + http.headers[:accept] = default_media_type + http.headers[:content_type] = "multipart/form-data" + http.headers[:user_agent] = user_agent + + # Activate if authentication is needed + # + # if basic_authenticated? + # http.basic_auth(@login, @password) + # elsif token_authenticated? + # http.authorization 'token', @access_token + # elsif application_authenticated? + # http.params = http.params.merge application_authentication + # end + end + end + + # Hypermedia agent for the Iev API + # + # @return [Sawyer::Agent] def agent @agent ||= Sawyer::Agent.new(api_endpoint, sawyer_options) do |http| http.headers[:accept] = default_media_type @@ -253,6 +283,19 @@ module Ievkit @agent = nil end + def multipart_request(method, path, data, options = {}) + if data.is_a?(Hash) + options[:query] = data.delete(:query) || {} + options[:headers] = data.delete(:headers) || {} + if accept = data.delete(:accept) + options[:headers][:accept] = accept + end + end + + @last_response = response = multipart_agent.call(method, URI::Parser.new.escape(path.to_s), data, options) + response.data + end + def request(method, path, data, options = {}) if data.is_a?(Hash) options[:query] = data.delete(:query) || {} @@ -261,7 +304,7 @@ module Ievkit options[:headers][:accept] = accept end end - + @last_response = response = agent.call(method, URI::Parser.new.escape(path.to_s), data, options) response.data end |
