Skip to content

Commit

Permalink
Remove multipart-post dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
nachojorge committed Feb 7, 2025
1 parent 963f88b commit 8168f24
Show file tree
Hide file tree
Showing 38 changed files with 459 additions and 721 deletions.
41 changes: 41 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
Metrics/BlockLength:
Enabled: false

Metrics/MethodLength:
Enabled: false

Metrics/CyclomaticComplexity:
Enabled: false

Style/Documentation:
Enabled: false

Metrics/AbcSize:
Enabled: false

Style/OptionalBooleanParameter:
Enabled: false

Lint/RescueException:
Enabled: false

Metrics/ModuleLength:
Enabled: false

Metrics/PerceivedComplexity:
Enabled: false

Metrics/ParameterLists:
Enabled: false

Lint/InheritException:
Enabled: false

Naming/FileName:
Enabled: false

Style/EvalWithLocation:
Enabled: false

Naming/HeredocDelimiterNaming:
Enabled: false
4 changes: 3 additions & 1 deletion .simplecov
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# frozen_string_literal: true

SimpleCov.start do
add_filter '/spec/'
add_filter '.simplecov'

add_filter 'lib/angus/unmarshalling'
end
end
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

source 'https://rubygems.org'

# Specify your gem's dependencies in angus-remote.gemspec
Expand Down
17 changes: 11 additions & 6 deletions angus-remote.gemspec
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

lib = File.expand_path('lib', __dir__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'angus/remote/version'
Expand All @@ -6,6 +8,7 @@ Gem::Specification.new do |spec|
spec.name = 'angus-remote'
spec.version = Angus::Remote::VERSION
spec.platform = Gem::Platform::RUBY

spec.authors = ['Adrian Gomez', 'Gianfranco Zas']
spec.email = %w[[email protected]]
spec.summary = 'Client for building service objects.'
Expand All @@ -15,20 +18,22 @@ Gem::Specification.new do |spec|
spec.homepage = 'http://mooveit.github.io/angus-remote'
spec.license = 'MIT'

spec.required_ruby_version = '>= 2.6.0'

spec.files = Dir.glob('{lib}/**/*')
spec.test_files = Dir.glob('{spec/angus}/**/*')
spec.require_paths = %w[lib]

spec.add_dependency('angus-sdoc', '~> 0.0', '>= 0.0.6')
spec.add_dependency('multipart-post', '>= 2.2')
spec.add_dependency('persistent_http', '~> 2.0.3')

spec.add_development_dependency('ci_reporter', '~> 2.0.0')
spec.add_development_dependency('ci_reporter')
spec.add_development_dependency('fakefs')
spec.add_development_dependency('rake')
spec.add_development_dependency('rspec', '~> 3.11.0')
spec.add_development_dependency('rspec')
spec.add_development_dependency('rspec-its')
spec.add_development_dependency('simplecov', '~> 0.17.1')
spec.add_development_dependency('simplecov-rcov', '~> 0.3.1')
spec.add_development_dependency('simplecov-rcov-text', '~> 0.0.3')
spec.add_development_dependency('rubocop')
spec.add_development_dependency('simplecov')
spec.add_development_dependency('simplecov-rcov')
spec.add_development_dependency('simplecov-rcov-text')
end
4 changes: 3 additions & 1 deletion lib/angus-remote.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# frozen_string_literal: true

require 'angus/remote/client'
require 'angus/remote/builder'
require 'angus/remote/proxy_client'
require 'angus/remote/response/serializer'
require 'angus/remote/service_directory'
require 'angus/remote/settings'
require 'angus/remote/settings'
17 changes: 9 additions & 8 deletions lib/angus/authentication/client.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
# frozen_string_literal: true

require 'digest'
require 'time'

module Angus
module Authentication
class Client

BAAS_VERSION = 1

AUTHENTICATION_HEADER = 'AUTHORIZATION'
Expand All @@ -18,12 +19,12 @@ def initialize(opts)
@public_key = opts[:public_key]
@private_key = opts[:private_key]

if disabled?
warn(
"No authentication info provided, angus-authentication has been disabled for: " \
"#{opts[:service_id]}"
)
end
return unless disabled?

warn(
'No authentication info provided, angus-authentication has been disabled for: ' \
"#{opts[:service_id]}"
)
end

def prepare_request(request, http_method, operation_path)
Expand All @@ -39,6 +40,7 @@ def prepare_request(request, http_method, operation_path)
end

private

def disabled?
!(@public_key || @private_key)
end
Expand All @@ -52,7 +54,6 @@ def request_signature(private_key, time, http_method, operation_path)

Digest::SHA1.hexdigest(plain_signature)
end

end
end
end
49 changes: 23 additions & 26 deletions lib/angus/remote/builder.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# frozen_string_literal: true

require_relative 'client'
require_relative 'response/builder'
require_relative 'settings'

module Angus
module Remote

module Builder

# Builds a client for a specific service.
#
# @param [String] code_name The service's name known to the service directory
Expand All @@ -23,20 +23,20 @@ def self.build(code_name, service_definition, api_url, options)
if service_definition.operations.is_a?(Hash)
service_definition.operations.each do |namespace, operations|
operations.each do |operation|
self.define_operation(remote_service_class, namespace, operation, code_name,
service_definition)
define_operation(remote_service_class, namespace, operation, code_name,
service_definition)
end
end
else
service_definition.operations.each do |operation|
self.define_operation(remote_service_class, code_name, operation, code_name,
service_definition)
define_operation(remote_service_class, code_name, operation, code_name,
service_definition)
end
end

service_definition.proxy_operations.each do |operation|
self.define_proxy_operation(remote_service_class, operation.service_name, operation,
code_name, service_definition)
define_proxy_operation(remote_service_class, operation.service_name, operation,
code_name, service_definition)
end

remote_service_class.new(api_url, Settings.default_timeout, options)
Expand All @@ -63,10 +63,10 @@ def self.define_operation(client_class, namespace, operation, service_code_name,
path_params, request_params)

Angus::Remote::Response::Builder.build_from_remote_response(response,
service_code_name,
service_definition.version,
namespace,
operation.code_name)
service_code_name,
service_definition.version,
namespace,
operation.code_name)
end
end

Expand All @@ -89,18 +89,18 @@ def self.define_proxy_operation(client_class, namespace, operation, service_code
encode_as_json = Angus::Remote::Builder.extract_var_arg!(args, TrueClass) || false

request_params = Angus::Remote::Builder.apply_glossary(service_definition.glossary,
request_params)
request_params)

request_params = Angus::Remote::Builder.escape_request_params(request_params)

response = make_request(operation.path, operation.http_method, encode_as_json,
path_params, request_params)

Angus::Remote::Response::Builder.build_from_remote_response(response,
service_code_name,
service_definition.version,
namespace,
operation.code_name)
service_code_name,
service_definition.version,
namespace,
operation.code_name)
end
end

Expand All @@ -126,7 +126,6 @@ def self.to_s
remote_service_class
end


# Applies glossary to params.
#
# Converts the params that are long names to short names
Expand Down Expand Up @@ -163,25 +162,23 @@ def self.extract_var_arg!(args, klass)
arg_found = false

i = args.length
while !arg_found && i > 0
while !arg_found && i.positive?
i -= 1
arg = args[i]
arg_found = true if arg.is_a?(klass)
end

if arg_found
args.delete_at(i)
arg
end
return unless arg_found

args.delete_at(i)
arg
end

def self.escape_request_params(request_params)
encoded = {}
request_params.each do |name, value|
encoded_name = CGI.escape(name.to_s)
if value.is_a? Hash
value = self.escape_request_params(value)
end
value = escape_request_params(value) if value.is_a? Hash
encoded[encoded_name] = value
end

Expand Down
42 changes: 22 additions & 20 deletions lib/angus/remote/client.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'json'
require 'persistent_http'

Expand All @@ -9,29 +11,28 @@

module Angus
module Remote

# A client for service invocation
class Client
def initialize(api_url, timeout = nil, options = {})
api_url = api_url[0..-2] if api_url[-1] == '/'

@connection = PersistentHTTP.new(
:pool_size => options['pool_size'] || 10,
:pool_timeout => 10,
:warn_timeout => 0.25,
:force_retry => false,
:url => api_url,

:read_timeout => timeout,
:open_timeout => timeout
pool_size: options['pool_size'] || 10,
pool_timeout: 10,
warn_timeout: 0.25,
force_retry: false,
url: api_url,

read_timeout: timeout,
open_timeout: timeout
)

@api_base_path = @connection.default_path

store_namespace = "#{options['code_name']}.#{options['version']}"
client_settings = { :public_key => options['public_key'],
:private_key => options['private_key'],
:service_id => store_namespace }
client_settings = { public_key: options['public_key'],
private_key: options['private_key'],
service_id: store_namespace }

@authentication_client = Authentication::Client.new(client_settings)
end
Expand Down Expand Up @@ -63,13 +64,11 @@ def make_request(path, method, encode_as_json, path_params, request_params)

response = @connection.request(request)

if Utils.severe_error_response?(response)
raise RemoteSevereError.new(get_error_messages(response.body))
end
raise RemoteSevereError, get_error_messages(response.body) if Utils.severe_error_response?(response)

response
rescue Errno::ECONNREFUSED, PersistentHTTP::Error => e
raise RemoteConnectionError.new("#@api_base_path - #{e.class}: #{e.message}")
raise RemoteConnectionError, "#{@api_base_path} - #{e.class}: #{e.message}"
end
end

Expand All @@ -80,11 +79,14 @@ def to_s
private

def get_error_messages(response_body)
json_response = JSON(response_body) rescue { 'messages' => [] }
Response::Builder::build_messages(json_response['messages'])
end
json_response = begin
JSON(response_body)
rescue StandardError
{ 'messages' => [] }
end

Response::Builder.build_messages(json_response['messages'])
end
end

end
end
Loading

0 comments on commit 8168f24

Please sign in to comment.