Skip to content

Commit

Permalink
Merge pull request #2101 from microsoft/feature/middleware-ruby
Browse files Browse the repository at this point in the history
feature/middleware ruby
  • Loading branch information
baywet authored Dec 28, 2022
2 parents 218953c + b3305ce commit 0071def
Show file tree
Hide file tree
Showing 37 changed files with 353 additions and 126 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added

- Added support for auto-registration of serializers in Ruby. [#478](https://github.com/microsoft/kiota/issues/478)
- Added support for middleware infrastructure in Ruby. [#1650](https://github.com/microsoft/kiota/issues/1650)

### Changed

Expand Down
10 changes: 0 additions & 10 deletions abstractions/ruby/microsoft_kiota_abstractions/Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,3 @@ source 'https://rubygems.org'

# Specify your gem's dependencies in microsoft_kiota_abstractions.gemspec
gemspec

gem 'rake', '~> 13.0'

gem 'rspec', '~> 3.0'

gem 'rubocop', require: false

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

gem 'iso8601', '~> 0.13.0'
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
require_relative "microsoft_kiota_abstractions/http_method"
require_relative "microsoft_kiota_abstractions/api_client_builder"
require_relative "microsoft_kiota_abstractions/request_adapter"
require_relative "microsoft_kiota_abstractions/request_option"
require_relative "microsoft_kiota_abstractions/request_information"
require_relative "microsoft_kiota_abstractions/response_handler"
require_relative "microsoft_kiota_abstractions/version"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ def authenticate_request(request, 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? || token.empty?
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,46 @@ def uri
end
end

def add_request_options(request_options_to_add)
unless request_options_to_add.nil? then
@request_options ||= Hash.new
unless request_options_to_add.kind_of?(Array) then
request_options_to_add = [request_options_to_add]
end
request_options_to_add.each do |request_option|
key = request_option.get_key
@request_options[key] = request_option
end
end
end

def get_request_options()
if @request_options.nil? then
return []
else
return @request_options.values
end
end

def get_request_option(key)
if @request_options.nil? || key.nil? || key.empty? then
return nil
else
return @request_options[key]
end
end

def remove_request_options(keys)
unless keys.nil? || @request_options.nil? then
unless keys.kind_of?(Array) then
keys = [keys]
end
keys.each do |key|
@request_options.delete(key)
end
end
end

def http_method=(method)
@http_method = HttpMethod::HTTP_METHOD[method]
end
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module MicrosoftKiotaAbstractions
module RequestOption
def get_key()
raise NotImplementedError.new
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.4.0"
VERSION = "0.5.0"
end
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ 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 'addressable', '~> 2.7', '>= 2.7.0'
spec.add_dependency 'iso8601', '~> 0.13.0'
spec.add_runtime_dependency 'addressable', '~> 2.7', '>= 2.7.0'
spec.add_runtime_dependency 'iso8601', '~> 0.13.0'
spec.add_development_dependency 'rake', '~> 13.0'
spec.add_development_dependency 'rspec', '~> 3.0'
spec.add_development_dependency 'rubocop'
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
require 'microsoft_kiota_abstractions'
require_relative 'request_option_mock'

RSpec.describe MicrosoftKiotaAbstractions do

it "adds a request option to the request information" do
request_info = MicrosoftKiotaAbstractions::RequestInformation.new
mock_option = RequestOptionMock.new
mock_option.value = "value"
request_info.add_request_options(mock_option)
expect(request_info.get_request_options().length).to eq(1)
expect(request_info.get_request_option("key")).to eq(mock_option)
end

it "removes a request option from the request information" do
request_info = MicrosoftKiotaAbstractions::RequestInformation.new
mock_option = RequestOptionMock.new
mock_option.value = "value"
request_info.add_request_options(mock_option)
expect(request_info.get_request_options().length).to eq(1)
request_info.remove_request_options("key")
expect(request_info.get_request_options().length).to eq(0)
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
require 'microsoft_kiota_abstractions'
class RequestOptionMock
include MicrosoftKiotaAbstractions::RequestOption
attr_accessor :value
def get_key()
return "key"
end
end
Original file line number Diff line number Diff line change
@@ -1,22 +1,10 @@
# frozen_string_literal: true

source 'https://rubygems.org'
source "https://rubygems.pkg.github.com/microsoft"

# Specify your gem's dependencies in microsoft_kiota_authentication.gemspec
gemspec

# git_source(:github) { |repo_name| "https://rubygems.pkg.github.com/microsoft" }
# gem "microsoft_kiota_abstractions", path: "../../../../abstractions/ruby/microsoft_kiota_abstractions"

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

gem 'rake', '~> 13.0'

gem 'rspec', '~> 3.0'

gem 'rubocop', require: false

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

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

module MicrosoftKiotaAuthenticationOAuth
VERSION = "0.3.0"
VERSION = "0.4.0"
end
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ Gem::Specification.new do |spec|
spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
spec.require_paths = ['lib']

spec.add_dependency 'microsoft_kiota_abstractions'
spec.add_dependency 'oauth2', '~> 2.0'
spec.add_runtime_dependency 'microsoft_kiota_abstractions', '~> 0.5.0', '>= 0.5.0'
spec.add_runtime_dependency 'oauth2', '~> 2.0'
spec.add_development_dependency 'rake', '~> 13.0'
spec.add_development_dependency 'rspec', '~> 3.0'
spec.add_development_dependency 'rubocop'
end
8 changes: 8 additions & 0 deletions http/ruby/faraday/microsoft_kiota_faraday/Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# frozen_string_literal: true

source "https://rubygems.org"
source 'https://rubygems.pkg.github.com/microsoft'

gemspec

# gem "microsoft_kiota_abstractions", path: "../../../../abstractions/ruby/microsoft_kiota_abstractions"
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# MicrosoftKiotaNethttp
# MicrosoftKiotaFaraday

![Ruby](https://github.com/microsoft/kiota/actions/workflows/http-ruby-nethttp.yml/badge.svg)

Expand Down Expand Up @@ -29,7 +29,7 @@ Add this line to your application's Gemfile:

```ruby
source "https://rubygems.pkg.github.com/microsoft" do
gem "microsoft_kiota_nethttplibrary", "0.1.0"
gem "microsoft_kiota_nethttplibrary", "0.4.0"
end
```

Expand All @@ -42,7 +42,7 @@ bundle install
Or install it yourself as:

```shell
gem install microsoft_kiota_nethttplibrary --version "0.1.0" --source "https://{USERNAME}{PASSWORD/TOKEN}rubygems.pkg.github.com/microsoft"
gem install microsoft_kiota_faraday --version "0.4.0" --source "https://{USERNAME}{PASSWORD/TOKEN}rubygems.pkg.github.com/microsoft"
```

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

require_relative 'microsoft_kiota_faraday/version'
require_relative 'microsoft_kiota_faraday/middleware/parameters_name_decoding_option'
require_relative 'microsoft_kiota_faraday/middleware/parameters_name_decoding_handler'
require_relative 'microsoft_kiota_faraday/kiota_client_factory'
require_relative 'microsoft_kiota_faraday/faraday_request_adapter'

module MicrosoftKiotaFaraday
end
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
require 'microsoft_kiota_abstractions'
require 'net/https'
require 'faraday'
require 'net/http'
require_relative 'kiota_client_factory'

module MicrosoftKiotaNethttplibrary
class NetHttpRequestAdapter
module MicrosoftKiotaFaraday
class FaradayRequestAdapter
include MicrosoftKiotaAbstractions::RequestAdapter

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

def initialize(authentication_provider, parse_node_factory=MicrosoftKiotaAbstractions::ParseNodeFactoryRegistry.default_instance, serialization_writer_factory=MicrosoftKiotaAbstractions::SerializationWriterFactoryRegistry.default_instance, client = Net::HTTP)
def initialize(authentication_provider, parse_node_factory=MicrosoftKiotaAbstractions::ParseNodeFactoryRegistry.default_instance, serialization_writer_factory=MicrosoftKiotaAbstractions::SerializationWriterFactoryRegistry.default_instance, client = KiotaClientFactory::get_default_http_client)

if !authentication_provider
raise StandardError , 'authentication provider cannot be null'
Expand All @@ -24,6 +25,9 @@ def initialize(authentication_provider, parse_node_factory=MicrosoftKiotaAbstrac
@serialization_writer_factory = MicrosoftKiotaAbstractions::SerializationWriterFactoryRegistry.default_instance
end
@client = client
if @client.nil?
@client = KiotaClientFactory::get_default_http_client
end
@base_url = ''
end

Expand All @@ -47,12 +51,7 @@ def send_async(request_info, type, response_handler)
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
response = http.request(request)
response = @client.run_request(request.http_method, request.path, request.body, request.headers)

if response_handler
response_handler.handle_response_async(response).resume;
Expand All @@ -73,38 +72,46 @@ def get_request_from_request_info(request_info)
request_info.path_parameters['baseurl'] = @base_url
case request_info.http_method
when :GET
request = @client::Get.new(request_info.uri.request_uri)
request = @client.build_request(:get)
when :POST
request = @client::Post.new(request_info.uri.request_uri)
request = @client.build_request(:post)
when :PATCH
request = @client::Patch.new(request_info.uri.request_uri)
request = @client.build_request(:patch)
when :DELETE
request = @client::Delete.new(request_info.uri.request_uri)
request = @client.build_request(:delete)
when :OPTIONS
request = @client::Options.new(request_info.uri.request_uri)
request = @client.build_request(:options)
when :CONNECT
request = @client::Connect.new(request_info.uri.request_uri)
request = @client.build_request(:connect)
when :PUT
request = @client::Put.new(request_info.uri.request_uri)
request = @client.build_request(:put)
when :TRACE
request = @client::Trace.new(request_info.uri.request_uri)
request = @client.build_request(:trace)
when :HEAD
request = @client::Head.new(request_info.uri.request_uri)
request = @client.build_request(:head)
else
raise StandardError, 'unsupported http method'
end
request.path = request_info.uri
if request_info.headers.instance_of? Hash
request_info.headers.select{|k,v| request[k] = v }
request.headers = Faraday::Utils::Headers.new
request_info.headers.select{|k,v| request.headers[k] = v }
end
if request_info.content != nil
request.body = request_info.content # the json serialization writer returns a string at the moment, change to body_stream when this is fixed
request.body = request_info.content unless request_info.content.nil? || request_info.content.empty?
# TODO the json serialization writer returns a string at the moment, change to body_stream when this is fixed
request_options = request_info.get_request_options
if !request_options.nil? && !request_options.empty? then
request.options = Faraday::RequestOptions.new
request_options.each do |value|
request.options.context[value.key] = value
end
end
request
end

def get_response_content_type(response)
begin
response['content-type'].split(';')[0].downcase()
response.headers['content-type'].split(';')[0].downcase()
rescue
return nil
end
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
require 'net/https'
require 'faraday'
require_relative 'middleware/parameters_name_decoding_handler'
module MicrosoftKiotaFaraday
class KiotaClientFactory
def self.get_default_middleware()
return [
MicrosoftKiotaFaraday::Middleware::ParametersNameDecodingHandler
]
end

def self.get_default_http_client(middleware=nil)
if middleware.nil? #empty is fine in case the user doesn't want to use any middleware
middleware = self.get_default_middleware()
end
conn = Faraday::Connection.new do |builder|
builder.adapter Faraday.default_adapter
builder.ssl.verify = true
builder.ssl.verify_mode = OpenSSL::SSL::VERIFY_PEER
middleware.each do |middleware|
builder.use middleware
end
end
conn
end
end
end
Loading

0 comments on commit 0071def

Please sign in to comment.