From 06ad4a6ca627eee9b6b5ac598bdd814268f8e11c Mon Sep 17 00:00:00 2001 From: Paul Makepeace Date: Wed, 8 Jul 2020 00:49:02 -0700 Subject: [PATCH 1/2] Use `:format => "plain"` for newline-delimited output The OpenStack Object Storage API allows the response body format to be given via an Accept header or`format` parameter. So if the latter is given then don't set the Accept header. This replaces the `json: false` option. --- README.md | 4 +++- lib/swift_client.rb | 2 +- test/swift_client_test.rb | 6 +++--- 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index f90064d..17be7c3 100644 --- a/README.md +++ b/README.md @@ -151,7 +151,9 @@ SwiftClient offers the following requests: * `bulk_delete(entries, options = {}) # => entries` * `post_head(object_name, container_name, _headers = {}, options = {}) # => HTTParty::Response` -By default, the client instructs the Swift server to return JSON via an HTTP Accept header; to disable this pass `:json => false` in `options`. The rest of the `options` are passed directly to the internal [HTTParty](https://rubygems.org/gems/httparty) client. +By default, the client instructs the endpoint to return JSON via an HTTP Accept header; to specify another format pass, e.g., `:format => "plain"` in `query` ("xml" is also [supported in the Object Storage API](https://docs.openstack.org/api-ref/object-store/index.html?expanded=show-container-details-and-list-objects-detail#show-container-details-and-list-objects)). + +All `options` are passed directly to the internal [HTTParty](https://rubygems.org/gems/httparty) client. ### Getting large objects The `get_object` method with out a block is suitable for small objects that easily fit in memory. For larger objects, specify a block to process chunked data as it comes in. diff --git a/lib/swift_client.rb b/lib/swift_client.rb index f4c9206..0a2b02a 100644 --- a/lib/swift_client.rb +++ b/lib/swift_client.rb @@ -195,7 +195,7 @@ def find_header_key(headers, key) def request(method, path, opts = {}, &block) headers = (opts[:headers] || {}).dup headers["X-Auth-Token"] = auth_token - headers["Accept"] ||= "application/json" unless opts.delete(:json) == false + headers["Accept"] ||= "application/json" unless opts[:query] && opts[:query][:format] stream_pos = opts[:body_stream].pos if opts[:body_stream] diff --git a/test/swift_client_test.rb b/test/swift_client_test.rb index 0eac1fa..b3e6482 100644 --- a/test/swift_client_test.rb +++ b/test/swift_client_test.rb @@ -341,12 +341,12 @@ def test_get_objects assert_equal objects, @swift_client.get_objects("container-1", :limit => 2, :marker => "object-2").parsed_response end - def test_get_objects_no_json + def test_get_objects_format_plain response = "object-2\nobject-3\n" - stub_request(:get, "https://example.com/v1/AUTH_account/container-1?limit=2&marker=object-2").with(:headers => { "X-Auth-Token" => "Token" }).to_return(:status => 200, :body => response, :headers => { "Content-Type" => "text/html" }) + stub_request(:get, "https://example.com/v1/AUTH_account/container-1?format=plain&limit=2&marker=object-2").with(:headers => { "X-Auth-Token" => "Token" }).to_return(:status => 200, :body => response, :headers => { "Content-Type" => "text/plain" }) - assert_equal response, @swift_client.get_objects("container-1", { :limit => 2, :marker => "object-2" }, :json => false).body + assert_equal response, @swift_client.get_objects("container-1", :format => "plain", :limit => 2, :marker => "object-2").body end def test_paginate_objects From 3a82ed510eb296d295a505fed6c3310ed1e00a34 Mon Sep 17 00:00:00 2001 From: Benjamin Vetter Date: Mon, 18 Jan 2021 20:15:31 +0100 Subject: [PATCH 2/2] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 17be7c3..39c464b 100644 --- a/README.md +++ b/README.md @@ -151,7 +151,7 @@ SwiftClient offers the following requests: * `bulk_delete(entries, options = {}) # => entries` * `post_head(object_name, container_name, _headers = {}, options = {}) # => HTTParty::Response` -By default, the client instructs the endpoint to return JSON via an HTTP Accept header; to specify another format pass, e.g., `:format => "plain"` in `query` ("xml" is also [supported in the Object Storage API](https://docs.openstack.org/api-ref/object-store/index.html?expanded=show-container-details-and-list-objects-detail#show-container-details-and-list-objects)). +By default, the client instructs the endpoint to return JSON via an HTTP Accept header. To specify another format pass, e.g., `:format => "plain"` via the `query` parameter of the `get_*` and `paginate_*` methods. Other formats, like `"xml"` are supported as well. Checkout [Object Storage API](https://docs.openstack.org/api-ref/object-store/index.html?expanded=show-container-details-and-list-objects-detail#show-container-details-and-list-objects) for more details. All `options` are passed directly to the internal [HTTParty](https://rubygems.org/gems/httparty) client.