lib/bubbles/config.rb in bubbles-rest-client-0.4.1 vs lib/bubbles/config.rb in bubbles-rest-client-0.5.0

- old
+ new

@@ -41,21 +41,23 @@ def initialize @environment_scheme = 'http' @environment_host = '127.0.0.1' @environment_port = '1234' @environment_api_key = nil + @environment_api_key_name = 'X-API-Key' @endpoints = Hash.new end ## # Retrieve the {RestEnvironment} object defined as part of this Configuration. # # Note that this constructs a new +RestEnvironment+ and returns it, rather than returning an existing object. # def environment - RestEnvironment.new(@environment_scheme, @environment_host, @environment_port, @environment_api_key) + RestEnvironment.new(@environment_scheme, @environment_host, @environment_port, @environment_api_key, + @environment_api_key_name) end ## # Set the current environment. # @@ -64,19 +66,26 @@ # @example In app/config/environments/staging.rb: # Bubbles.configure do |config| # config.environment = { # :scheme => 'https', # :host => 'stage.api.somehost.com', - # :port => '443' + # :port => '443', + # :api_key => 'something', + # :api_key_name => 'X-API-Key' # Optional # } # end # def environment=(env) @environment_scheme = env[:scheme] @environment_host = env[:host] @environment_port = env[:port] @environment_api_key = env[:api_key] + if env.has_key? :api_key_name + @environment_api_key_name = env[:api_key_name] + else + @environment_api_key_name = 'X-API-Key' + end end ## # Retrieve the list of +Endpoint+s configured in this +Configuration+ object. # @@ -127,180 +136,148 @@ if endpoint.method == :get if endpoint.authenticated? Bubbles::RestEnvironment.class_exec do if endpoint.has_uri_params? define_method(endpoint_name_as_sym) do |auth_token, uri_params| - RestClientResources.execute_get_authenticated self, endpoint, auth_token, uri_params, endpoint.additional_headers + RestClientResources.execute_get_authenticated self, endpoint, auth_token, uri_params, endpoint.additional_headers, self.get_api_key_if_needed(endpoint), self.api_key_name end else define_method(endpoint_name_as_sym) do |auth_token| - RestClientResources.execute_get_authenticated self, endpoint, auth_token,{}, endpoint.additional_headers + RestClientResources.execute_get_authenticated self, endpoint, auth_token, {}, endpoint.additional_headers, self.get_api_key_if_needed(endpoint), self.api_key_name end end end else Bubbles::RestEnvironment.class_exec do - define_method(endpoint_name_as_sym) do - RestClientResources.execute_get_unauthenticated self, endpoint, endpoint.additional_headers + if endpoint.has_uri_params? + define_method(endpoint_name_as_sym) do |uri_params| + RestClientResources.execute_get_unauthenticated self, endpoint, uri_params, endpoint.additional_headers, self.get_api_key_if_needed(endpoint), self.api_key_name + end + else + define_method(endpoint_name_as_sym) do + RestClientResources.execute_get_unauthenticated self, endpoint, {}, endpoint.additional_headers, self.get_api_key_if_needed(endpoint), self.api_key_name + end end end end elsif endpoint.method == :post if endpoint.authenticated? Bubbles::RestEnvironment.class_exec do define_method(endpoint_name_as_sym) do |auth_token, data| - RestClientResources.execute_post_authenticated self, endpoint, auth_token, data, endpoint.additional_headers + RestClientResources.execute_post_authenticated self, endpoint, auth_token, data, endpoint.additional_headers, self.get_api_key_if_needed(endpoint), self.api_key_name end end else - if endpoint.api_key_required? - Bubbles::RestEnvironment.class_exec do - define_method(endpoint_name_as_sym) do |data| - additional_headers = endpoint.additional_headers - if endpoint.encode_authorization_header? - count = 0 - auth_value = '' - endpoint.encode_authorization.each { |auth_key| - if data[auth_key] - if count > 0 - auth_value = auth_value + ':' + data[auth_key] - else - auth_value = data[auth_key] - end - - count = count + 1 - - data.delete(auth_key) - end - } - - additional_headers[:Authorization] = 'Basic ' + Base64.strict_encode64(auth_value) - end - - RestClientResources.execute_post_with_api_key self, endpoint, self.api_key, data, additional_headers + Bubbles::RestEnvironment.class_exec do + define_method(endpoint_name_as_sym) do |data| + composite_headers = endpoint.additional_headers + if endpoint.encode_authorization_header? + auth_value = RestClientResources.get_encoded_authorization(endpoint, data) + composite_headers = RestClientResources.build_composite_headers(endpoint.additional_headers, { + :Authorization => 'Basic ' + Base64.strict_encode64(auth_value) + }) end + + RestClientResources.execute_post_unauthenticated self, endpoint, data, composite_headers, self.get_api_key_if_needed(endpoint), self.api_key_name end - else - raise 'Unauthenticated POST requests without an API key are not allowed' end end elsif endpoint.method == :delete - if endpoint.authenticated? - Bubbles::RestEnvironment.class_exec do - if endpoint.has_uri_params? + if endpoint.has_uri_params? + if endpoint.authenticated? + Bubbles::RestEnvironment.class_exec do define_method(endpoint_name_as_sym) do |auth_token, uri_params| - RestClientResources.execute_delete_authenticated self, endpoint, auth_token, uri_params, endpoint.additional_headers + RestClientResources.execute_delete_authenticated self, endpoint, auth_token, uri_params, endpoint.additional_headers, self.get_api_key_if_needed(endpoint), self.api_key_name end - else - # NOTE: While MDN states that DELETE requests with a body are allowed, it seems that a number of - # documentation sites discourage its use. Thus, it's possible that, depending on the server API - # framework, the DELETE request could be rejected. As such, we're disallowing it here, BUT if we - # get feedback from users that it should be supported, we can add support for it. - raise 'DELETE requests without URI parameters are not allowed' - # define_method(endpoint_name_as_sym) do |auth_token| - # RestClientResources.execute_delete_authenticated self, endpoint, auth_token, {} - # end end + else + Bubbles::RestEnvironment.class_exec do + define_method(endpoint_name_as_sym) do |uri_params| + RestClientResources.execute_delete_unauthenticated self, endpoint, uri_params, endpoint.additional_headers, self.get_api_key_if_needed(endpoint), self.api_key_name + end + end end else - raise 'Unauthenticated DELETE requests are not allowed' - # Bubbles::RestEnvironment.class_exec do - # define_method(endpoint_name_as_sym) do - # RestClientResources.execute_delete_unauthenticated self, endpoint - # end - # end + # XXX_jwir3: While MDN states that DELETE requests with a body are allowed, it seems that a number of + # documentation sites discourage its use. Thus, it's possible that, depending on the server API + # framework, the DELETE request could be rejected. In addition, RestClient doesn't seem to support DELETE + # requests with a body, so we're a bit stuck on this one, even if we wanted to support it. + raise 'DELETE requests without URI parameters are not allowed' end elsif endpoint.method == :patch if endpoint.authenticated? Bubbles::RestEnvironment.class_exec do if endpoint.has_uri_params? define_method(endpoint_name_as_sym) do |auth_token, uri_params, data| - RestClientResources.execute_patch_authenticated self, endpoint, auth_token, uri_params, data, endpoint.additional_headers + RestClientResources.execute_patch_authenticated self, endpoint, auth_token, uri_params, data, endpoint.additional_headers, self.get_api_key_if_needed(endpoint), self.api_key_name end else define_method(endpoint_name_as_sym) do |auth_token, data| - # TODO: Nothing tests this case. We need something to test this case or we run the risk of - # it having bugs (uri_params was nil previously!) - RestClientResources.execute_patch_authenticated self, endpoint, auth_token, {}, data, endpoint.additional_headers + RestClientResources.execute_patch_authenticated self, endpoint, auth_token, {}, data, endpoint.additional_headers, self.get_api_key_if_needed(endpoint), self.api_key_name end end end else Bubbles::RestEnvironment.class_exec do if endpoint.has_uri_params? define_method(endpoint_name_as_sym) do |uri_params, data| - RestClientResources.execute_patch_unauthenticated self, endpoint, uri_params, data, endpoint.additional_headers + RestClientResources.execute_patch_unauthenticated self, endpoint, uri_params, data, endpoint.additional_headers, self.get_api_key_if_needed(endpoint), self.api_key_name end else define_method(endpoint_name_as_sym) do |data| - RestClientResources.execute_patch_unauthenticated self, endpoint, {}, data, endpoint.additional_headers + RestClientResources.execute_patch_unauthenticated self, endpoint, {}, data, endpoint.additional_headers, self.get_api_key_if_needed(endpoint), self.api_key_name end end end end elsif endpoint.method == :put if endpoint.authenticated? Bubbles::RestEnvironment.class_exec do if endpoint.has_uri_params? define_method(endpoint_name_as_sym) do |auth_token, uri_params, data| - RestClientResources.execute_put_authenticated self, endpoint, auth_token, uri_params, data, endpoint.additional_headers + RestClientResources.execute_put_authenticated self, endpoint, auth_token, uri_params, data, endpoint.additional_headers, self.get_api_key_if_needed(endpoint), self.api_key_name end else define_method(endpoint_name_as_sym) do |auth_token, data| - # TODO: Nothing tests this case. We need something to test this case or we run the risk of - # it having bugs (uri_params was nil previously!) - RestClientResources.execute_put_authenticated self, endpoint, auth_token, {}, data, endpoint.additional_headers + RestClientResources.execute_put_authenticated self, endpoint, auth_token, {}, data, endpoint.additional_headers, self.get_api_key_if_needed(endpoint), self.api_key_name end end end else - # raise 'Unauthenticated PUT requests are not implemented' Bubbles::RestEnvironment.class_exec do if endpoint.has_uri_params? define_method(endpoint_name_as_sym) do |uri_params, data| - RestClientResources.execute_put_unauthenticated self, endpoint, uri_params, data, endpoint.additional_headers + RestClientResources.execute_put_unauthenticated self, endpoint, uri_params, data, endpoint.additional_headers, self.get_api_key_if_needed(endpoint), self.api_key_name end else define_method(endpoint_name_as_sym) do |data| - RestClientResources.execute_put_unauthenticated self, endpoint, {}, data, endpoint.additional_headers + RestClientResources.execute_put_unauthenticated self, endpoint, {}, data, endpoint.additional_headers, self.get_api_key_if_needed(endpoint), self.api_key_name end end end end elsif endpoint.method == :head if endpoint.authenticated? Bubbles::RestEnvironment.class_exec do if endpoint.has_uri_params? define_method(endpoint_name_as_sym) do |auth_token, uri_params| - RestClientResources.execute_head_authenticated self, endpoint, auth_token, uri_params, endpoint.additional_headers + RestClientResources.execute_head_authenticated self, endpoint, auth_token, uri_params, endpoint.additional_headers, self.get_api_key_if_needed(endpoint), self.api_key_name end else define_method(endpoint_name_as_sym) do |auth_token| - RestClientResources.execute_head_authenticated self, endpoint, auth_token, {}, endpoint.additional_headers + RestClientResources.execute_head_authenticated self, endpoint, auth_token, {}, endpoint.additional_headers, self.get_api_key_if_needed(endpoint), self.api_key_name end end end - elsif endpoint.api_key_required? - Bubbles::RestEnvironment.class_exec do - if endpoint.has_uri_params? - define_method(endpoint_name_as_sym) do |uri_params| - RestClientResources.execute_head_unauthenticated_with_uri_params self, endpoint, self.api_key, uri_params, endpoint.additional_headers - end - else - define_method(endpoint_name_as_sym) do - RestClientResources.execute_head_unauthenticated self, endpoint, self.api_key, endpoint.additional_headers - end - end - end else Bubbles::RestEnvironment.class_exec do if endpoint.has_uri_params? define_method(endpoint_name_as_sym) do |uri_params| - RestClientResources.execute_head_unauthenticated self, endpoint, uri_params, endpoint.additional_headers + RestClientResources.execute_head_unauthenticated self, endpoint, uri_params, endpoint.additional_headers, self.get_api_key_if_needed(endpoint), self.api_key_name end else define_method(endpoint_name_as_sym) do - RestClientResources.execute_head_unauthenticated self, endpoint, {}, endpoint.additional_headers + RestClientResources.execute_head_unauthenticated self, endpoint, {}, endpoint.additional_headers, self.get_api_key_if_needed(endpoint), self.api_key_name end end end end end