Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Merge pull request #66 from arax/client-options

Fixes #63, using hash for client options
  • Loading branch information...
commit 97b8f4a8ab5a02c0fb2303297779147769faeaa4 2 parents e4e3e85 + 7ecf8c8
Florian Feldhaus ffeldhaus authored
22 README.md
Source Rendered
@@ -164,7 +164,16 @@ To include the DSL definitions in a class use
164 164
165 165 To connect to an OCCI endpoint/server (e.g. running on http://localhost:3300/ )
166 166
167   - connect(:http, 'http://localhost:3300',auth||=nil)
  167 + # defaults
  168 + options = {
  169 + :endpoint => "http://localhost:3300/",
  170 + :auth => {:type => "none"},
  171 + :log => {:out => STDERR, :level => Occi::Log::WARN, :logger => nil},
  172 + :auto_connect => "value", auto_connect => true,
  173 + :media_type => nil
  174 + }
  175 +
  176 + connect(:http, options ||= {})
168 177
169 178 To get the list of available resource, mixin, entity or link types use
170 179
@@ -232,7 +241,16 @@ at a time, you should use the OCCI client API directly.
232 241
233 242 To connect to an OCCI endpoint/server (e.g. running on http://localhost:3300/ )
234 243
235   - client = Occi::Api::Client::ClientHttp.new('http://localhost:3300',auth||=nil)
  244 + # defaults
  245 + options = {
  246 + :endpoint => "http://localhost:3300/",
  247 + :auth => {:type => "none"},
  248 + :log => {:out => STDERR, :level => Occi::Log::WARN, :logger => nil},
  249 + :auto_connect => "value", auto_connect => true,
  250 + :media_type => nil
  251 + }
  252 +
  253 + client = Occi::Api::Client::ClientHttp.new(options ||= {})
236 254
237 255 All available categories are automatically registered to the OCCI model during client initialization. You can get them via
238 256
4 bin/occi
@@ -60,7 +60,9 @@ end
60 60 # no effect on the non-interactive one
61 61 begin
62 62 Occi::Log.info "Establishing a connection to #{options.endpoint} ..."
63   - connect :http, options.endpoint, options.auth, options.log, true, options.media_type
  63 +
  64 + options.auto_connect = true
  65 + connect :http, options
64 66 rescue OpenSSL::SSL::SSLError => ssl_ex
65 67 # generic SSL error raised whilst establishing a connection
66 68 # possibly an untrusted server cert or invalid user credentials
23 examples/dsl_example.rb
@@ -17,13 +17,22 @@
17 17 ENDPOINT = 'https://localhost:3300'
18 18
19 19 ## establish a connection
20   -connect(:http, ENDPOINT,
21   - { :type => "x509",
22   - :user_cert => USER_CERT,
23   - :user_cert_password => USER_CERT_PASSWORD,
24   - :ca_path => CA_PATH },
25   - { :out => STDERR,
26   - :level => Occi::Log::DEBUG })
  20 +connect(
  21 + :http,
  22 + {
  23 + :endpoint => ENDPOINT,
  24 + :auth => {
  25 + :type => "x509",
  26 + :user_cert => USER_CERT,
  27 + :user_cert_password => USER_CERT_PASSWORD,
  28 + :ca_path => CA_PATH
  29 + },
  30 + :log => {
  31 + :out => STDERR,
  32 + :level => Occi::Log::DEBUG
  33 + }
  34 + }
  35 +)
27 36
28 37 puts "\n\nListing all available resource types:"
29 38 resource_types.each do |type|
20 examples/x509auth_example.rb
@@ -14,13 +14,19 @@
14 14 ENDPOINT = 'https://localhost:3300'
15 15
16 16 ## get an OCCI::Api::Client::ClientHttp instance
17   -client = Occi::Api::Client::ClientHttp.new(ENDPOINT,
18   - { :type => "x509",
19   - :user_cert => USER_CERT,
20   - :user_cert_password => USER_CERT_PASSWORD,
21   - :ca_path => CA_PATH },
22   - { :out => STDERR,
23   - :level => Occi::Log::DEBUG })
  17 +client = Occi::Api::Client::ClientHttp.new({
  18 + :endpoint => ENDPOINT,
  19 + :auth => {
  20 + :type => "x509",
  21 + :user_cert => USER_CERT,
  22 + :user_cert_password => USER_CERT_PASSWORD,
  23 + :ca_path => CA_PATH
  24 + },
  25 + :log => {
  26 + :out => STDERR,
  27 + :level => Occi::Log::DEBUG
  28 + }
  29 +})
24 30
25 31 puts "\n\nListing all available resource types:"
26 32 client.get_resource_types.each do |type|
18 features/common/step_definitions/common_steps.rb
@@ -17,16 +17,16 @@
17 17 end
18 18
19 19 Given /^have an initialize Client$/ do
20   - @client = Occi::Api::Client::ClientHttp.new(
21   - @endpoint, #141.5.99.69 #11.5.99.82
22   - { :type => "none" },
23   - { :out => "/dev/null",
24   - :level => Occi::Log::DEBUG },
25   - true,
26   - @accept_type#"text/plain,text/occi"
27   - )
  20 + @client = Occi::Api::Client::ClientHttp.new({
  21 + :endpoint => @endpoint, #141.5.99.69 #11.5.99.82
  22 + :auth => { :type => "none" },
  23 + :log => { :out => "/dev/null",
  24 + :level => Occi::Log::DEBUG },
  25 + :auto_connect => true,
  26 + :media_type => @accept_type#"text/plain,text/occi"
  27 + })
28 28 end
29 29
30 30 Then /^the Client should have the response code (.*)$/ do |response_code|
31 31 @client.last_response.code.should == response_code.to_i
32   -end
  32 +end
38 lib/occi/api/client/client_amqp.rb
@@ -67,28 +67,38 @@ class ClientAmqp
67 67 # from the server.
68 68 #
69 69 # @example
70   - # Occi::Api::Client::ClientAmqp.new # => #<Occi::Api::Client::ClientAmqp>
  70 + # options = {
  71 + # :endpoint => "http://localhost:3300/",
  72 + # :auth => {:type => "none"},
  73 + # :log => {:out => STDERR, :level => Occi::Log::WARN, :logger => nil},
  74 + # :media_type => "text/plain"
  75 + # }
71 76 #
72   - # @param [String] endpoint URI
73   - # @param [Hash] auth options in a hash
74   - # @param [Hash] logging options in a hash
75   - # @param [Boolean] enable autoconnect
76   - # @param [String] media type identifier
  77 + # Occi::Api::Client::ClientAmqp.new options # => #<Occi::Api::Client::ClientAmqp>
  78 + #
  79 + # @param [Hash] options, for available options and defaults see examples
77 80 # @return [Occi::Api::Client::ClientAmqp] client instance
78   - def initialize(endpoint = "http://localhost:3000/", auth_options = { :type => "none" },
79   - log_options = { :out => STDERR, :level => Occi::Log::WARN, :logger => nil },
80   - media_type = "text/plain")
  81 + def initialize(options = {})
  82 +
  83 + defaults = {
  84 + :endpoint => "http://localhost:3300/",
  85 + :auth => {:type => "none"},
  86 + :log => {:out => STDERR, :level => Occi::Log::WARN, :logger => nil},
  87 + :media_type => "text/plain"
  88 + }
  89 +
  90 + options = defaults.merge options
81 91
82 92 # check the validity and canonize the endpoint URI
83   - prepare_endpoint endpoint
  93 + prepare_endpoint options[:endpoint]
84 94
85 95 # set Occi::Log
86   - set_logger log_options
  96 + set_logger options[:log]
87 97
88 98 # pass auth options to HTTParty
89   - change_auth auth_options
  99 + change_auth options[:auth]
90 100
91   - @media_type = media_type
  101 + @media_type = options[:media_type]
92 102
93 103 Occi::Log.debug("Media Type: #{@media_type}")
94 104
@@ -96,7 +106,7 @@ def initialize(endpoint = "http://localhost:3000/", auth_options = { :type => "n
96 106
97 107 Thread.new { run }
98 108
99   - print "Waiting for connection amqp ..."
  109 + Occi::Log.debug("Waiting for connection amqp ...")
100 110
101 111 #TODO find a better solution for the thread issue
102 112 while(!@thread_error && !@connected)
45 lib/occi/api/client/client_http.rb
@@ -75,33 +75,46 @@ class ClientHttp
75 75 # from the server.
76 76 #
77 77 # @example
78   - # Occi::Api::Client::ClientHttp.new # => #<Occi::Api::Client::ClientHttp>
  78 + # options = {
  79 + # :endpoint => "http://localhost:3300/",
  80 + # :auth => {:type => "none"},
  81 + # :log => {:out => STDERR, :level => Occi::Log::WARN, :logger => nil},
  82 + # :auto_connect => "value", auto_connect => true,
  83 + # :media_type => nil
  84 + # }
79 85 #
80   - # @param [String] endpoint URI
81   - # @param [Hash] auth options in a hash
82   - # @param [Hash] logging options in a hash
83   - # @param [Boolean] enable autoconnect
84   - # @param [String] media type identifier
  86 + # Occi::Api::Client::ClientHttp.new options # => #<Occi::Api::Client::ClientHttp>
  87 + #
  88 + # @param [Hash] options, for available options and defaults see examples
85 89 # @return [Occi::Api::Client::ClientHttp] client instance
86   - def initialize(endpoint = "http://localhost:3000/", auth_options = {:type => "none"},
87   - log_options = {:out => STDERR, :level => Occi::Log::WARN, :logger => nil},
88   - auto_connect = true, media_type = nil)
  90 + def initialize(options = {})
  91 +
  92 + defaults = {
  93 + :endpoint => "http://localhost:3300/",
  94 + :auth => {:type => "none"},
  95 + :log => {:out => STDERR, :level => Occi::Log::WARN, :logger => nil},
  96 + :auto_connect => true,
  97 + :media_type => nil
  98 + }
  99 +
  100 + options = defaults.merge options
  101 +
89 102 # set Occi::Log
90   - set_logger log_options
  103 + set_logger options[:log]
91 104
92 105 # pass auth options to HTTParty
93   - change_auth auth_options
  106 + change_auth options[:auth]
94 107
95 108 # check the validity and canonize the endpoint URI
96   - prepare_endpoint endpoint
  109 + prepare_endpoint options[:endpoint]
97 110
98 111 # get accepted media types from HTTParty
99 112 set_media_type
100 113
101 114 # force media_type if provided
102   - if media_type
103   - self.class.headers 'Accept' => media_type
104   - @media_type = media_type
  115 + if options[:media_type]
  116 + self.class.headers 'Accept' => options[:media_type]
  117 + @media_type = options[:media_type]
105 118 end
106 119
107 120 Occi::Log.debug("Media Type: #{@media_type}")
@@ -112,7 +125,7 @@ def initialize(endpoint = "http://localhost:3000/", auth_options = {:type => "no
112 125 set_model
113 126
114 127 # auto-connect?
115   - @connected = auto_connect
  128 + @connected = options[:auto_connect]
116 129 end
117 130
118 131 # Creates a new resource instance, resource should be specified
9 spec/occi/api/client/client_amqp_spec.rb
@@ -23,9 +23,12 @@ module Client
23 23
24 24 =begin
25 25 before(:all) do
26   - @client = Occi::Api::Client::ClientAmqp.new("http://localhost:9292/", auth_options = { :type => "none" },
27   - log_options = { :out => STDERR, :level => Occi::Log::WARN, :logger => nil },
28   - media_type = "application/occi+json")
  26 + @client = Occi::Api::Client::ClientAmqp.new({
  27 + :endpoint => "http://localhost:9292/",
  28 + :auth => { :type => "none" },
  29 + :log => { :out => STDERR, :level => Occi::Log::WARN, :logger => nil },
  30 + :media_type => "application/occi+json"
  31 + })
29 32 end
30 33
31 34 it "initialize and connect client" do
32 spec/occi/api/client/client_http_spec.rb
@@ -11,14 +11,14 @@ module Client
11 11 context "using media type text/plain" do
12 12
13 13 before(:each) do
14   - @client = Occi::Api::Client::ClientHttp.new(
15   - 'https://localhost:3300',
16   - { :type => "none" },
17   - { :out => "/dev/null",
18   - :level => Occi::Log::DEBUG },
19   - true,
20   - "text/plain,text/occi"
21   - )
  14 + @client = Occi::Api::Client::ClientHttp.new({
  15 + :endpoint => 'https://localhost:3300',
  16 + :auth => { :type => "none" },
  17 + :log => { :out => "/dev/null",
  18 + :level => Occi::Log::DEBUG },
  19 + :auto_connect => true,
  20 + :media_type => "text/plain,text/occi"
  21 + })
22 22 end
23 23
24 24 after(:each) do
@@ -189,14 +189,14 @@ module Client
189 189 context "using media type application/occi+json" do
190 190
191 191 before(:each) do
192   - #@client = Occi::Api::ClientHttp.new(
193   - # 'https://localhost:3300',
194   - # { :type => "none" },
195   - # { :out => "/dev/null",
196   - # :level => Occi::Log::DEBUG },
197   - # true,
198   - # "application/occi+json"
199   - #)
  192 + #@client = Occi::Api::ClientHttp.new({
  193 + # :endpoint => 'https://localhost:3300',
  194 + # :auth => { :type => "none" },
  195 + # :log => { :out => "/dev/null",
  196 + # :level => Occi::Log::DEBUG },
  197 + # :auto_connect => true,
  198 + # :media_type => "application/occi+json"
  199 + #})
200 200 end
201 201
202 202 it "establishes connection"

0 comments on commit 97b8f4a

Please sign in to comment.
Something went wrong with that request. Please try again.