swift_client 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in swift_client.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Benjamin Vetter
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,27 @@
1
+ # SwiftClient
2
+
3
+ Small but powerful client to interact with OpenStack Swift.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'swift_client'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install swift_client
20
+
21
+ ## Contributing
22
+
23
+ 1. Fork it ( https://github.com/mrkamel/swift_client/fork )
24
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
25
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
26
+ 4. Push to the branch (`git push origin my-new-feature`)
27
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,9 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "lib"
6
+ t.pattern = "test/**/*_test.rb"
7
+ t.verbose = true
8
+ end
9
+
@@ -0,0 +1,5 @@
1
+
2
+ class SwiftClient
3
+ VERSION = "0.0.1"
4
+ end
5
+
@@ -0,0 +1,143 @@
1
+
2
+ require "swift_client/version"
3
+
4
+ require "httparty"
5
+ require "mime-types"
6
+
7
+ class SwiftClient
8
+ class AuthenticationError < StandardError; end
9
+ class OptionError < StandardError; end
10
+ class EmptyNameError < StandardError; end
11
+
12
+ class ResponseError < StandardError
13
+ attr_accessor :code, :message
14
+
15
+ def initialize(code, message)
16
+ self.code = code
17
+ self.message = message
18
+ end
19
+
20
+ def to_s
21
+ "#{code} #{message}"
22
+ end
23
+ end
24
+
25
+ attr_accessor :options, :auth_token, :storage_url
26
+
27
+ def initialize(options = {})
28
+ [:auth_url, :username, :api_key].each do |key|
29
+ raise(OptionError, "#{key} is missing") unless options.key?(key)
30
+ end
31
+
32
+ self.options = options
33
+
34
+ authenticate
35
+ end
36
+
37
+ def get_containers(query = {})
38
+ request :get, "/", :query => query
39
+ end
40
+
41
+ def get_container(name, query = {})
42
+ raise(EmptyNameError) if name.empty?
43
+
44
+ request :get, "/#{name}", :query => query
45
+ end
46
+
47
+ def head_container(name)
48
+ raise(EmptyNameError) if name.empty?
49
+
50
+ request :head, "/#{name}"
51
+ end
52
+
53
+ def put_container(name, headers = {})
54
+ raise(EmptyNameError) if name.empty?
55
+
56
+ request :put, "/#{name}", :headers => headers
57
+ end
58
+
59
+ def post_container(name, headers = {})
60
+ raise(EmptyNameError) if name.empty?
61
+
62
+ request :post, "/#{name}", :headers => headers
63
+ end
64
+
65
+ def delete_container(name)
66
+ raise(EmptyNameError) if name.empty?
67
+
68
+ request :delete, "/#{name}"
69
+ end
70
+
71
+ def put_object(name, data_or_io, container, headers = {})
72
+ raise(EmptyNameError) if name.empty? || container.empty?
73
+
74
+ mime_type = MIME::Types.of(name).first
75
+
76
+ extended_headers = headers.dup
77
+ extended_headers["Content-Type"] ||= mime_type.content_type if mime_type
78
+
79
+ request :put, "/#{container}/#{name}", :body => data_or_io.respond_to?(:read) ? data_or_io.read : data_or_io, :headers => extended_headers
80
+ end
81
+
82
+ def post_object(name, container, headers = {})
83
+ raise(EmptyNameError) if name.empty? || container.empty?
84
+
85
+ request :post, "/#{container}/#{name}", :headers => headers
86
+ end
87
+
88
+ def get_object(name, container)
89
+ raise(EmptyNameError) if name.empty? || container.empty?
90
+
91
+ request :get, "/#{container}/#{name}"
92
+ end
93
+
94
+ def head_object(name, container)
95
+ raise(EmptyNameError) if name.empty? || container.empty?
96
+
97
+ request :head, "/#{container}/#{name}"
98
+ end
99
+
100
+ def delete_object(name, container)
101
+ raise(EmptyNameError) if name.empty? || container.empty?
102
+
103
+ request :delete, "/#{container}/#{name}"
104
+ end
105
+
106
+ def get_objects(container, query = {})
107
+ raise(EmptyNameError) if container.empty?
108
+
109
+ request :get, "/#{container}", :query => query
110
+ end
111
+
112
+ private
113
+
114
+ def request(method, path, opts = {})
115
+ opts[:headers] ||= {}
116
+ opts[:headers]["X-Auth-Token"] = auth_token
117
+ opts[:headers]["Accept"] = "application/json"
118
+
119
+ response = HTTParty.send(method, "#{storage_url}#{path}", opts)
120
+
121
+ if response.code == 401
122
+ authenticate
123
+
124
+ return request(method, path, opts)
125
+ end
126
+
127
+ raise(ResponseError.new(response.code, response.message)) unless response.success?
128
+
129
+ response
130
+ end
131
+
132
+ def authenticate
133
+ response = HTTParty.get(options[:auth_url], :headers => { "X-Auth-User" => options[:username], "X-Auth-Key" => options[:api_key] })
134
+
135
+ raise(AuthenticationError, "#{response.code}: #{response.message}") unless response.success?
136
+
137
+ self.auth_token = response.headers["X-Auth-Token"]
138
+ self.storage_url = response.headers["X-Storage-Url"]
139
+
140
+ storage_url.gsub!(/^http:/, "https:") if options[:auth_url] =~ /^https:/
141
+ end
142
+ end
143
+
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'swift_client/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "swift_client"
8
+ spec.version = SwiftClient::VERSION
9
+ spec.authors = ["Benjamin Vetter"]
10
+ spec.email = ["vetter@plainpicture.de"]
11
+ spec.summary = %q{Small but powerful client to interact with OpenStack Swift}
12
+ spec.description = %q{Small but powerful client to interact with OpenStack Swift}
13
+ spec.homepage = "https://github.com/mrkamel/swift_client"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "httparty"
22
+ spec.add_dependency "mime-types"
23
+
24
+ spec.add_development_dependency "bundler"
25
+ spec.add_development_dependency "rake"
26
+ spec.add_development_dependency "minitest"
27
+ spec.add_development_dependency "webmock"
28
+ end
@@ -0,0 +1,118 @@
1
+
2
+ require File.expand_path("../test_helper", __FILE__)
3
+
4
+ class SwiftClientTest < MiniTest::Test
5
+ def setup
6
+ stub_request(:get, "https://example.com/auth/v1.0").with(:headers => { "X-Auth-Key" => "secret", "X-Auth-User" => "account:username" }).to_return(:status => 200, :body => "", :headers => { "X-Auth-Token" => "Token", "X-Storage-Url" => "https://example.com/v1/AUTH_account" })
7
+
8
+ @swift_client = SwiftClient.new(:auth_url => "https://example.com/auth/v1.0", :username => "account:username", :api_key => "secret")
9
+
10
+ assert_equal "Token", @swift_client.auth_token
11
+ assert_equal "https://example.com/v1/AUTH_account", @swift_client.storage_url
12
+ end
13
+
14
+ def test_get_containers
15
+ containers = [
16
+ { "count" => 1, "bytes" => 1, "name" => "container-1" },
17
+ { "count" => 1, "bytes" => 1, "name" => "container-2" }
18
+ ]
19
+
20
+ stub_request(:get, "https://example.com/v1/AUTH_account/").with(:headers => { "Accept" => "application/json", "X-Auth-Token" => "Token" }).to_return(:status => 200, :body => JSON.dump(containers), :headers => { "Content-Type" => "application/json" })
21
+
22
+ assert_equal containers, @swift_client.get_containers.parsed_response
23
+ end
24
+
25
+ def test_get_container
26
+ objects = [
27
+ { "hash" => "Hash", "last_modified" => "Last modified", "bytes" => 1, "name" => "object-2", "content_type" => "Content type" },
28
+ { "hash" => "Hash", "last_modified" => "Last modified", "bytes" => 1, "name" => "object-3", "content_type" => "Content type" }
29
+ ]
30
+
31
+ stub_request(:get, "https://example.com/v1/AUTH_account/container-1?limit=2&marker=object-2").with(:headers => { "Accept" => "application/json", "X-Auth-Token" => "Token" }).to_return(:status => 200, :body => JSON.dump(objects), :headers => { "Content-Type" => "application/json" })
32
+
33
+ assert_equal objects, @swift_client.get_container("container-1", :limit => 2, :marker => "object-2").parsed_response
34
+ end
35
+
36
+ def test_head_container
37
+ stub_request(:head, "https://example.com/v1/AUTH_account/container-1").with(:headers => { "Accept" => "application/json", "X-Auth-Token" => "Token" }).to_return(:status => 204, :body => "", :headers => { "Content-Type" => "application/json" })
38
+
39
+ assert 204, @swift_client.head_container("container-1").code
40
+ end
41
+
42
+ def test_put_container
43
+ stub_request(:put, "https://example.com/v1/AUTH_account/container").with(:headers => { "Accept" => "application/json", "X-Auth-Token" => "Token", "X-Container-Read" => ".r:*" }).to_return(:status => 201, :body => "", :headers => {})
44
+
45
+ assert_equal 201, @swift_client.put_container("container", "X-Container-Read" => ".r:*").code
46
+ end
47
+
48
+ def test_post_container
49
+ stub_request(:post, "https://example.com/v1/AUTH_account/container").with(:headers => { "Accept" => "application/json", "X-Auth-Token" => "Token", "X-Container-Read" => ".r:*" }).to_return(:status => 204, :body => "", :headers => {})
50
+
51
+ assert_equal 204, @swift_client.post_container("container", "X-Container-Read" => ".r:*").code
52
+ end
53
+
54
+ def test_delete_container
55
+ stub_request(:delete, "https://example.com/v1/AUTH_account/container").with(:headers => { "Accept" => "application/json", "X-Auth-Token" => "Token" }).to_return(:status => 204, :body => "", :headers => {})
56
+
57
+ assert_equal 204, @swift_client.delete_container("container").code
58
+ end
59
+
60
+ def test_put_object
61
+ stub_request(:put, "https://example.com/v1/AUTH_account/container/object").with(:body => "data", :headers => { "Accept" => "application/json", "X-Auth-Token" => "Token", "X-Object-Meta-Test" => "Test" }).to_return(:status => 201, :body => "", :headers => {})
62
+
63
+ assert_equal 201, @swift_client.put_object("object", "data", "container", "X-Object-Meta-Test" => "Test").code
64
+ end
65
+
66
+ def test_put_object_with_io
67
+ stub_request(:put, "https://example.com/v1/AUTH_account/container/object").with(:body => "data", :headers => { "Accept" => "application/json", "X-Auth-Token" => "Token", "X-Object-Meta-Test" => "Test" }).to_return(:status => 201, :body => "", :headers => {})
68
+
69
+ assert_equal 201, @swift_client.put_object("object", StringIO.new("data"), "container", "X-Object-Meta-Test" => "Test").code
70
+ end
71
+
72
+ def test_post_object
73
+ stub_request(:post, "https://example.com/v1/AUTH_account/container/object").with(:headers => { "Accept" => "application/json", "X-Auth-Token" => "Token", "X-Object-Meta-Test" => "Test" }).to_return(:status => 201, :body => "", :headers => {})
74
+
75
+ assert_equal 201, @swift_client.post_object("object", "container", "X-Object-Meta-Test" => "Test").code
76
+ end
77
+
78
+ def test_get_object
79
+ stub_request(:get, "https://example.com/v1/AUTH_account/container/object").with(:headers => { "Accept" => "application/json", "X-Auth-Token" => "Token" }).to_return(:status => 200, :body => "Body", :headers => {})
80
+
81
+ assert_equal "Body", @swift_client.get_object("object", "container").body
82
+ end
83
+
84
+ def test_head_object
85
+ stub_request(:head, "https://example.com/v1/AUTH_account/container/object").with(:headers => { "Accept" => "application/json", "X-Auth-Token" => "Token" }).to_return(:status => 200, :body => "", :headers => {})
86
+
87
+ assert_equal 200, @swift_client.head_object("object", "container").code
88
+ end
89
+
90
+ def test_delete_object
91
+ stub_request(:delete, "https://example.com/v1/AUTH_account/container/object").with(:headers => { "Accept" => "application/json", "X-Auth-Token" => "Token" }).to_return(:status => 204, :body => "", :headers => {})
92
+
93
+ assert_equal 204, @swift_client.delete_object("object", "container").code
94
+ end
95
+
96
+ def test_get_objects
97
+ objects = [
98
+ { "hash" => "Hash", "last_modified" => "Last modified", "bytes" => 1, "name" => "object-2", "content_type" => "Content type" },
99
+ { "hash" => "Hash", "last_modified" => "Last modified", "bytes" => 1, "name" => "object-3", "content_type" => "Content type" }
100
+ ]
101
+
102
+ stub_request(:get, "https://example.com/v1/AUTH_account/container-1?limit=2&marker=object-2").with(:headers => { "Accept" => "application/json", "X-Auth-Token" => "Token" }).to_return(:status => 200, :body => JSON.dump(objects), :headers => { "Content-Type" => "application/json" })
103
+
104
+ assert_equal objects, @swift_client.get_container("container-1", :limit => 2, :marker => "object-2").parsed_response
105
+ end
106
+
107
+ def test_not_found
108
+ stub_request(:get, "https://example.com/v1/AUTH_account/container/object").with(:headers => { "Accept" => "application/json", "X-Auth-Token" => "Token" }).to_return(:status => [404, "Not Found"], :body => "", :headers => { "Content-Type" => "application/json" })
109
+
110
+ begin
111
+ @swift_client.get_object("object", "container")
112
+ rescue SwiftClient::ResponseError => e
113
+ assert_equal 404, e.code
114
+ assert_equal "Not Found", e.message
115
+ end
116
+ end
117
+ end
118
+
@@ -0,0 +1,6 @@
1
+
2
+ require "swift_client"
3
+ require "minitest"
4
+ require "minitest/autorun"
5
+ require "webmock/minitest"
6
+
metadata ADDED
@@ -0,0 +1,154 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: swift_client
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Benjamin Vetter
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2014-12-19 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: httparty
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: mime-types
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: bundler
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: rake
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: minitest
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ - !ruby/object:Gem::Dependency
95
+ name: webmock
96
+ requirement: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ type: :development
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ description: Small but powerful client to interact with OpenStack Swift
111
+ email:
112
+ - vetter@plainpicture.de
113
+ executables: []
114
+ extensions: []
115
+ extra_rdoc_files: []
116
+ files:
117
+ - .gitignore
118
+ - Gemfile
119
+ - LICENSE.txt
120
+ - README.md
121
+ - Rakefile
122
+ - lib/swift_client.rb
123
+ - lib/swift_client/version.rb
124
+ - swift_client.gemspec
125
+ - test/swift_client_test.rb
126
+ - test/test_helper.rb
127
+ homepage: https://github.com/mrkamel/swift_client
128
+ licenses:
129
+ - MIT
130
+ post_install_message:
131
+ rdoc_options: []
132
+ require_paths:
133
+ - lib
134
+ required_ruby_version: !ruby/object:Gem::Requirement
135
+ none: false
136
+ requirements:
137
+ - - ! '>='
138
+ - !ruby/object:Gem::Version
139
+ version: '0'
140
+ required_rubygems_version: !ruby/object:Gem::Requirement
141
+ none: false
142
+ requirements:
143
+ - - ! '>='
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ requirements: []
147
+ rubyforge_project:
148
+ rubygems_version: 1.8.23
149
+ signing_key:
150
+ specification_version: 3
151
+ summary: Small but powerful client to interact with OpenStack Swift
152
+ test_files:
153
+ - test/swift_client_test.rb
154
+ - test/test_helper.rb