Skip to content

Commit

Permalink
Merge pull request #2098 from microsoft/bugfix/ruby-true-async
Browse files Browse the repository at this point in the history
bugfix/ruby true async
  • Loading branch information
baywet authored Dec 28, 2022
2 parents e87456b + f199903 commit 218953c
Show file tree
Hide file tree
Showing 19 changed files with 48 additions and 65 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Fixed a bug where a missing baseURL would make search fail. [#2095](https://github.com/microsoft/kiota/issues/2095)
- Fixed a bug in Ruby where the request adapter URL would be overwritten by the client defaults. [#1647](https://github.com/microsoft/kiota/issues/1647)
- Replaced concurrent-ruby by Fibers in Ruby libraries to implement proper asynchronous execution of requests.

## [0.9.0] - 2022-12-19

Expand Down
2 changes: 0 additions & 2 deletions abstractions/ruby/microsoft_kiota_abstractions/Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ gem 'rspec', '~> 3.0'

gem 'rubocop', require: false

gem 'concurrent-ruby', '~> 1.1', '>= 1.1.9'

gem 'addressable', '~> 2.7', '>= 2.7.0'

gem 'iso8601', '~> 0.13.0'
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# frozen_string_literal: true

require 'concurrent'
require_relative 'allowed_hosts_validator'

module MicrosoftKiotaAbstractions
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
require 'concurrent'

module MicrosoftKiotaAbstractions
class AnonymousAuthenticationProvider
include MicrosoftKiotaAbstractions::AuthenticationProvider
include Concurrent::Async
def authenticate_request(request)
end
end
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
# frozen_string_literal: true

require 'concurrent'
require_relative './authentication_provider'
require_relative './access_token_provider'

module MicrosoftKiotaAbstractions
# Provides a base class for implementing AuthenticationProvider for Bearer token scheme
class BaseBearerTokenAuthenticationProvider
include MicrosoftKiotaAbstractions::AuthenticationProvider
include Concurrent::Async
def initialize(access_token_provider)
raise StandardError, 'access_token_provider parameter cannot be nil' if access_token_provider.nil?

Expand All @@ -17,13 +15,14 @@ def initialize(access_token_provider)

AUTHORIZATION_HEADER_KEY = 'Authorization'
def authenticate_request(request, additional_properties = {})

raise StandardError, 'Request cannot be null' if request.nil?
return if request.headers.key?(AUTHORIZATION_HEADER_KEY)

token = @access_token_provider.get_authorization_token(request.uri, additional_properties)
Fiber.new do
token = @access_token_provider.get_authorization_token(request.uri, additional_properties).resume

request.headers[AUTHORIZATION_HEADER_KEY] = "Bearer #{token}" unless token.nil?
request.headers[AUTHORIZATION_HEADER_KEY] = "Bearer #{token}" unless token.nil?
end
end
end
end
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module MicrosoftKiotaAbstractions
VERSION = "0.3.1"
VERSION = "0.4.0"
end
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Gem::Specification.new do |spec|
'source_code_uri' => 'https://github.com/microsoft/kiota',
'github_repo' => 'ssh://github.com/microsoft/kiota'
}
spec.required_ruby_version = '>= 2.4.0'
spec.required_ruby_version = '>= 2.7.0'

# Specify which files should be added to the gem when it is released.
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
Expand All @@ -28,7 +28,6 @@ Gem::Specification.new do |spec|
spec.bindir = 'bin'
spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
spec.require_paths = ['lib']
spec.add_dependency 'concurrent-ruby', '~> 1.1', '>= 1.1.9'
spec.add_dependency 'addressable', '~> 2.7', '>= 2.7.0'
spec.add_dependency 'iso8601', '~> 0.13.0'
end
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ gemspec
# git_source(:github) { |repo_name| "https://rubygems.pkg.github.com/microsoft" }

source "https://rubygems.pkg.github.com/microsoft" do
gem "microsoft_kiota_abstractions", "0.2.0"
gem "microsoft_kiota_abstractions", '0.4.0'
end

gem 'rake', '~> 13.0'
Expand All @@ -17,8 +17,6 @@ gem 'rspec', '~> 3.0'

gem 'rubocop', require: false

gem 'concurrent-ruby', '~> 1.1', '>= 1.1.9'

gem 'addressable', '~> 2.7', '>= 2.7.0'

gem "oauth2", "~> 2.0"
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# frozen_string_literal: true

require 'concurrent'
require 'microsoft_kiota_abstractions'
require 'oauth2'
require_relative 'extensions/oauth2_ext'
Expand All @@ -13,7 +12,6 @@
module MicrosoftKiotaAuthenticationOAuth
# Access Token Provider class implementation
class OAuthAccessTokenProvider
include Concurrent::Async
# This is the initializer for OAuthAccessTokenProvider.
# :params
# token_request_context: a instance of one of our token request context or a custom implementation
Expand Down Expand Up @@ -53,23 +51,24 @@ def get_authorization_token(uri, additional_properties = {})

raise StandardError, 'Only https is supported' if parsed_url.scheme != 'https'

if @cached_token
token = OAuth2::AccessToken.from_hash(@token_request_context.oauth_provider, @cached_token)
return token.token if !token.nil? && !token.expired?
Fiber.new do
if @cached_token
token = OAuth2::AccessToken.from_hash(@token_request_context.oauth_provider, @cached_token)
return token.token if !token.nil? && !token.expired?

if token.expired?
token = token.refresh!
@cached_token = token.to_hash
return token.token
if token.expired?
token = token.refresh!
@cached_token = token.to_hash
return token.token
end
end
end

token = nil
token = @token_request_context.get_token
token = nil
token = @token_request_context.get_token

@cached_token = token.to_hash unless token.nil?
token = token.token unless token.nil?
token
@cached_token = token.to_hash unless token.nil?
return token.token unless token.nil?
end
end

attr_reader :scopes, :host_validator
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module MicrosoftKiotaAuthenticationOAuth
VERSION = "0.2.0"
VERSION = "0.3.0"
end
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Gem::Specification.new do |spec|
'source_code_uri' => 'https://github.com/microsoft/kiota',
'github_repo' => 'ssh://github.com/microsoft/kiota'
}
spec.required_ruby_version = ">= 2.4.0"
spec.required_ruby_version = ">= 2.7.0"

# Specify which files should be added to the gem when it is released.
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
Expand All @@ -30,7 +30,6 @@ Gem::Specification.new do |spec|
spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
spec.require_paths = ['lib']

spec.add_dependency 'concurrent-ruby', '~> 1.1', '>= 1.1.9'
spec.add_dependency 'microsoft_kiota_abstractions'
spec.add_dependency 'oauth2', '~> 2.0'
end
4 changes: 1 addition & 3 deletions http/ruby/nethttp/microsoft_kiota_nethttplibrary/Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,11 @@ gemspec
git_source(:github) { |repo_name| 'https://rubygems.pkg.github.com/microsoft' }

source 'https://rubygems.pkg.github.com/microsoft' do
gem 'microsoft_kiota_abstractions', '0.3.1'
gem 'microsoft_kiota_abstractions', '0.4.0'
end

gem 'rake', '~> 13.0'

gem 'rspec', '~> 3.0'

gem 'rubocop', require: false

gem 'concurrent-ruby', '~> 1.1', '>= 1.1.9'
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
require 'microsoft_kiota_abstractions'
require 'net/https'
require 'net/http'
require 'concurrent'

module MicrosoftKiotaNethttplibrary
class NetHttpRequestAdapter
include MicrosoftKiotaAbstractions::RequestAdapter
include Concurrent::Async

attr_accessor :authentication_provider, :content_type_header_key, :parse_node_factory, :serialization_writer_factory, :client

Expand Down Expand Up @@ -46,27 +44,28 @@ def send_async(request_info, type, response_handler)
raise StandardError, 'requestInfo cannot be null'
end

request = self.get_request_from_request_info(request_info)
uri = request_info.uri
@authentication_provider.await.authenticate_request(request_info)
Fiber.new do
@authentication_provider.authenticate_request(request_info).resume
request = self.get_request_from_request_info(request_info)
uri = request_info.uri

http = @client.new(uri.host, uri.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_PEER
request.headers = request_info.headers
response = http.request(request)
http = @client.new(uri.host, uri.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_PEER
response = http.request(request)

if response_handler
return response_handler.await.handle_response_async(response);
else
payload = response.body
response_content_type = self.get_response_content_type(response);
if response_handler
response_handler.handle_response_async(response).resume;
else
payload = response.body
response_content_type = self.get_response_content_type(response);

unless response_content_type
raise StandardError, 'no response content type found for deserialization'
unless response_content_type
raise StandardError, 'no response content type found for deserialization'
end
root_node = @parse_node_factory.get_parse_node(response_content_type, payload)
root_node.get_object_value(type)
end
root_node = @parse_node_factory.get_parse_node(response_content_type, payload)
root_node.get_object_value(type)
end
end

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module MicrosoftKiotaNethttplibrary
VERSION = '0.2.1'
VERSION = '0.3.0'
end
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Gem::Specification.new do |spec|
'source_code_uri' => 'https://github.com/microsoft/kiota',
'github_repo' => 'ssh://github.com/microsoft/kiota'
}
spec.required_ruby_version = ">= 2.4.0"
spec.required_ruby_version = ">= 2.7.0"

# Specify which files should be added to the gem when it is released.
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
Expand All @@ -28,6 +28,4 @@ Gem::Specification.new do |spec|
spec.bindir = 'bin'
spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
spec.require_paths = ['lib']

spec.add_dependency 'concurrent-ruby', '~> 1.1', '>= 1.1.9'
end
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
class AuthenticationProvider
include MicrosoftKiotaAbstractions::AuthenticationProvider
include Concurrent::Async
end
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ gemspec
git_source(:github) { |repo_name| "https://rubygems.pkg.github.com/microsoft" }

source "https://rubygems.pkg.github.com/microsoft" do
gem "microsoft_kiota_abstractions", "0.2.0"
gem "microsoft_kiota_abstractions", '0.4.0'
end

gem 'rake', '~> 13.0'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module MicrosoftKiotaSerialization
VERSION = "0.3.0"
VERSION = "0.4.0"
end
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Gem::Specification.new do |spec|
'source_code_uri' => 'https://github.com/microsoft/kiota',
'github_repo' => 'ssh://github.com/microsoft/kiota'
}
spec.required_ruby_version = ">= 2.4.0"
spec.required_ruby_version = ">= 2.7.0"

# Specify which files should be added to the gem when it is released.
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
Expand Down

0 comments on commit 218953c

Please sign in to comment.