diff --git a/CHANGELOG.md b/CHANGELOG.md index a499b98121..78f0534320 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/abstractions/ruby/microsoft_kiota_abstractions/Gemfile b/abstractions/ruby/microsoft_kiota_abstractions/Gemfile index afd0aa90e0..c6f618d98e 100644 --- a/abstractions/ruby/microsoft_kiota_abstractions/Gemfile +++ b/abstractions/ruby/microsoft_kiota_abstractions/Gemfile @@ -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' diff --git a/abstractions/ruby/microsoft_kiota_abstractions/lib/microsoft_kiota_abstractions.rb b/abstractions/ruby/microsoft_kiota_abstractions/lib/microsoft_kiota_abstractions.rb index b7ec55a530..673ad33615 100644 --- a/abstractions/ruby/microsoft_kiota_abstractions/lib/microsoft_kiota_abstractions.rb +++ b/abstractions/ruby/microsoft_kiota_abstractions/lib/microsoft_kiota_abstractions.rb @@ -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" diff --git a/abstractions/ruby/microsoft_kiota_abstractions/lib/microsoft_kiota_abstractions/authentication/base_bearer_token_authentication_provider.rb b/abstractions/ruby/microsoft_kiota_abstractions/lib/microsoft_kiota_abstractions/authentication/base_bearer_token_authentication_provider.rb index 148c5b1c74..7db2a63d3d 100644 --- a/abstractions/ruby/microsoft_kiota_abstractions/lib/microsoft_kiota_abstractions/authentication/base_bearer_token_authentication_provider.rb +++ b/abstractions/ruby/microsoft_kiota_abstractions/lib/microsoft_kiota_abstractions/authentication/base_bearer_token_authentication_provider.rb @@ -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 diff --git a/abstractions/ruby/microsoft_kiota_abstractions/lib/microsoft_kiota_abstractions/request_information.rb b/abstractions/ruby/microsoft_kiota_abstractions/lib/microsoft_kiota_abstractions/request_information.rb index 587c0eef28..2d6e96acd4 100644 --- a/abstractions/ruby/microsoft_kiota_abstractions/lib/microsoft_kiota_abstractions/request_information.rb +++ b/abstractions/ruby/microsoft_kiota_abstractions/lib/microsoft_kiota_abstractions/request_information.rb @@ -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 diff --git a/abstractions/ruby/microsoft_kiota_abstractions/lib/microsoft_kiota_abstractions/request_option.rb b/abstractions/ruby/microsoft_kiota_abstractions/lib/microsoft_kiota_abstractions/request_option.rb new file mode 100644 index 0000000000..ce47d694d5 --- /dev/null +++ b/abstractions/ruby/microsoft_kiota_abstractions/lib/microsoft_kiota_abstractions/request_option.rb @@ -0,0 +1,7 @@ +module MicrosoftKiotaAbstractions + module RequestOption + def get_key() + raise NotImplementedError.new + end + end +end \ No newline at end of file diff --git a/abstractions/ruby/microsoft_kiota_abstractions/lib/microsoft_kiota_abstractions/version.rb b/abstractions/ruby/microsoft_kiota_abstractions/lib/microsoft_kiota_abstractions/version.rb index 303b999607..1bec356fe8 100644 --- a/abstractions/ruby/microsoft_kiota_abstractions/lib/microsoft_kiota_abstractions/version.rb +++ b/abstractions/ruby/microsoft_kiota_abstractions/lib/microsoft_kiota_abstractions/version.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true module MicrosoftKiotaAbstractions - VERSION = "0.4.0" + VERSION = "0.5.0" end diff --git a/abstractions/ruby/microsoft_kiota_abstractions/microsoft_kiota_abstractions.gemspec b/abstractions/ruby/microsoft_kiota_abstractions/microsoft_kiota_abstractions.gemspec index 1db84e1e23..38fbf26e94 100644 --- a/abstractions/ruby/microsoft_kiota_abstractions/microsoft_kiota_abstractions.gemspec +++ b/abstractions/ruby/microsoft_kiota_abstractions/microsoft_kiota_abstractions.gemspec @@ -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 diff --git a/abstractions/ruby/microsoft_kiota_abstractions/spec/request_information_spec.rb b/abstractions/ruby/microsoft_kiota_abstractions/spec/request_information_spec.rb new file mode 100644 index 0000000000..b1de60ba06 --- /dev/null +++ b/abstractions/ruby/microsoft_kiota_abstractions/spec/request_information_spec.rb @@ -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 \ No newline at end of file diff --git a/abstractions/ruby/microsoft_kiota_abstractions/spec/request_option_mock.rb b/abstractions/ruby/microsoft_kiota_abstractions/spec/request_option_mock.rb new file mode 100644 index 0000000000..4b91552212 --- /dev/null +++ b/abstractions/ruby/microsoft_kiota_abstractions/spec/request_option_mock.rb @@ -0,0 +1,8 @@ +require 'microsoft_kiota_abstractions' +class RequestOptionMock + include MicrosoftKiotaAbstractions::RequestOption + attr_accessor :value + def get_key() + return "key" + end +end \ No newline at end of file diff --git a/authentication/ruby/oauth/microsoft_kiota_authentication_oauth/Gemfile b/authentication/ruby/oauth/microsoft_kiota_authentication_oauth/Gemfile index dddc79ff87..08a1a04a7b 100644 --- a/authentication/ruby/oauth/microsoft_kiota_authentication_oauth/Gemfile +++ b/authentication/ruby/oauth/microsoft_kiota_authentication_oauth/Gemfile @@ -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" diff --git a/authentication/ruby/oauth/microsoft_kiota_authentication_oauth/lib/microsoft_kiota_authentication_oauth/version.rb b/authentication/ruby/oauth/microsoft_kiota_authentication_oauth/lib/microsoft_kiota_authentication_oauth/version.rb index 6df5ef48ff..3cfde86393 100644 --- a/authentication/ruby/oauth/microsoft_kiota_authentication_oauth/lib/microsoft_kiota_authentication_oauth/version.rb +++ b/authentication/ruby/oauth/microsoft_kiota_authentication_oauth/lib/microsoft_kiota_authentication_oauth/version.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true module MicrosoftKiotaAuthenticationOAuth - VERSION = "0.3.0" + VERSION = "0.4.0" end diff --git a/authentication/ruby/oauth/microsoft_kiota_authentication_oauth/microsoft_kiota_authentication_oauth.gemspec b/authentication/ruby/oauth/microsoft_kiota_authentication_oauth/microsoft_kiota_authentication_oauth.gemspec index ac9205ce28..27852abaaa 100644 --- a/authentication/ruby/oauth/microsoft_kiota_authentication_oauth/microsoft_kiota_authentication_oauth.gemspec +++ b/authentication/ruby/oauth/microsoft_kiota_authentication_oauth/microsoft_kiota_authentication_oauth.gemspec @@ -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 diff --git a/http/ruby/nethttp/microsoft_kiota_nethttplibrary/.gitignore b/http/ruby/faraday/microsoft_kiota_faraday/.gitignore similarity index 100% rename from http/ruby/nethttp/microsoft_kiota_nethttplibrary/.gitignore rename to http/ruby/faraday/microsoft_kiota_faraday/.gitignore diff --git a/http/ruby/nethttp/microsoft_kiota_nethttplibrary/.rspec b/http/ruby/faraday/microsoft_kiota_faraday/.rspec similarity index 100% rename from http/ruby/nethttp/microsoft_kiota_nethttplibrary/.rspec rename to http/ruby/faraday/microsoft_kiota_faraday/.rspec diff --git a/http/ruby/faraday/microsoft_kiota_faraday/Gemfile b/http/ruby/faraday/microsoft_kiota_faraday/Gemfile new file mode 100644 index 0000000000..39a2dc74c9 --- /dev/null +++ b/http/ruby/faraday/microsoft_kiota_faraday/Gemfile @@ -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" diff --git a/http/ruby/nethttp/microsoft_kiota_nethttplibrary/README.md b/http/ruby/faraday/microsoft_kiota_faraday/README.md similarity index 79% rename from http/ruby/nethttp/microsoft_kiota_nethttplibrary/README.md rename to http/ruby/faraday/microsoft_kiota_faraday/README.md index 9f629bd7ab..e4195590ec 100644 --- a/http/ruby/nethttp/microsoft_kiota_nethttplibrary/README.md +++ b/http/ruby/faraday/microsoft_kiota_faraday/README.md @@ -1,4 +1,4 @@ -# MicrosoftKiotaNethttp +# MicrosoftKiotaFaraday ![Ruby](https://github.com/microsoft/kiota/actions/workflows/http-ruby-nethttp.yml/badge.svg) @@ -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 ``` @@ -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 diff --git a/http/ruby/nethttp/microsoft_kiota_nethttplibrary/Rakefile b/http/ruby/faraday/microsoft_kiota_faraday/Rakefile similarity index 100% rename from http/ruby/nethttp/microsoft_kiota_nethttplibrary/Rakefile rename to http/ruby/faraday/microsoft_kiota_faraday/Rakefile diff --git a/http/ruby/faraday/microsoft_kiota_faraday/lib/microsoft_kiota_faraday.rb b/http/ruby/faraday/microsoft_kiota_faraday/lib/microsoft_kiota_faraday.rb new file mode 100644 index 0000000000..0eef630cc8 --- /dev/null +++ b/http/ruby/faraday/microsoft_kiota_faraday/lib/microsoft_kiota_faraday.rb @@ -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 diff --git a/http/ruby/nethttp/microsoft_kiota_nethttplibrary/lib/microsoft_kiota_nethttplibrary/net_http_request_adapter.rb b/http/ruby/faraday/microsoft_kiota_faraday/lib/microsoft_kiota_faraday/faraday_request_adapter.rb similarity index 63% rename from http/ruby/nethttp/microsoft_kiota_nethttplibrary/lib/microsoft_kiota_nethttplibrary/net_http_request_adapter.rb rename to http/ruby/faraday/microsoft_kiota_faraday/lib/microsoft_kiota_faraday/faraday_request_adapter.rb index 338eb498dc..2fafc217a1 100644 --- a/http/ruby/nethttp/microsoft_kiota_nethttplibrary/lib/microsoft_kiota_nethttplibrary/net_http_request_adapter.rb +++ b/http/ruby/faraday/microsoft_kiota_faraday/lib/microsoft_kiota_faraday/faraday_request_adapter.rb @@ -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' @@ -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 @@ -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; @@ -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 diff --git a/http/ruby/faraday/microsoft_kiota_faraday/lib/microsoft_kiota_faraday/kiota_client_factory.rb b/http/ruby/faraday/microsoft_kiota_faraday/lib/microsoft_kiota_faraday/kiota_client_factory.rb new file mode 100644 index 0000000000..78628b297c --- /dev/null +++ b/http/ruby/faraday/microsoft_kiota_faraday/lib/microsoft_kiota_faraday/kiota_client_factory.rb @@ -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 \ No newline at end of file diff --git a/http/ruby/faraday/microsoft_kiota_faraday/lib/microsoft_kiota_faraday/middleware/parameters_name_decoding_handler.rb b/http/ruby/faraday/microsoft_kiota_faraday/lib/microsoft_kiota_faraday/middleware/parameters_name_decoding_handler.rb new file mode 100644 index 0000000000..23a27cc294 --- /dev/null +++ b/http/ruby/faraday/microsoft_kiota_faraday/lib/microsoft_kiota_faraday/middleware/parameters_name_decoding_handler.rb @@ -0,0 +1,38 @@ +require 'faraday' +require_relative 'parameters_name_decoding_option' +module MicrosoftKiotaFaraday + module Middleware + class ParametersNameDecodingHandler < Faraday::Middleware + def initialize(app = nil, options = MicrosoftKiotaFaraday::Middleware::ParametersNameDecodingOption.new) + if options.nil? then + raise ArgumentError, 'options cannot be nil' + end + #assigning options isn't necessary as the parent constructor does it + super(app, options) + end + + def call(request_env) + request_option = request_env[:request][:context][@options.get_key] unless request_env[:request].nil? || request_env[:request][:context].nil? + if request_option.nil? then + request_option = @options + end + if !request_env[:url].nil? && request_option.enabled && !request_option.characters_to_decode.nil? && !request_option.characters_to_decode.empty? then + request_url = request_env[:url].to_s + request_option.characters_to_decode.each do |character| + request_url = request_url.gsub(get_regex_for_character(character), character) + end + request_env[:url] = URI.parse(request_url) + end + @app.call(request_env) unless app.nil? + end + + def get_regex_for_character(character) + @regex_cache ||= Hash.new + if @regex_cache[character].nil? then + @regex_cache[character] = Regexp.new("%#{character.ord.to_s(16)}", true) + end + return @regex_cache[character] + end + end + end +end \ No newline at end of file diff --git a/http/ruby/faraday/microsoft_kiota_faraday/lib/microsoft_kiota_faraday/middleware/parameters_name_decoding_option.rb b/http/ruby/faraday/microsoft_kiota_faraday/lib/microsoft_kiota_faraday/middleware/parameters_name_decoding_option.rb new file mode 100644 index 0000000000..4c971e422e --- /dev/null +++ b/http/ruby/faraday/microsoft_kiota_faraday/lib/microsoft_kiota_faraday/middleware/parameters_name_decoding_option.rb @@ -0,0 +1,19 @@ +# frozen_string_literal: true +require 'microsoft_kiota_abstractions' +module MicrosoftKiotaFaraday + module Middleware + class ParametersNameDecodingOption + include MicrosoftKiotaAbstractions::RequestOption + attr_accessor :enabled, :characters_to_decode + + def initialize(enabled = true, characters_to_decode = ['$', '.', '-', '~']) + @enabled = enabled + @characters_to_decode = characters_to_decode + end + + def get_key() + "parametersNameDecoding" + end + end + end +end \ No newline at end of file diff --git a/http/ruby/faraday/microsoft_kiota_faraday/lib/microsoft_kiota_faraday/version.rb b/http/ruby/faraday/microsoft_kiota_faraday/lib/microsoft_kiota_faraday/version.rb new file mode 100644 index 0000000000..159f7fdb81 --- /dev/null +++ b/http/ruby/faraday/microsoft_kiota_faraday/lib/microsoft_kiota_faraday/version.rb @@ -0,0 +1,5 @@ +# frozen_string_literal: true + +module MicrosoftKiotaFaraday + VERSION = '0.4.0' +end diff --git a/http/ruby/nethttp/microsoft_kiota_nethttplibrary/microsoft_kiota_nethttplibrary.gemspec b/http/ruby/faraday/microsoft_kiota_faraday/microsoft_kiota_faraday.gemspec similarity index 63% rename from http/ruby/nethttp/microsoft_kiota_nethttplibrary/microsoft_kiota_nethttplibrary.gemspec rename to http/ruby/faraday/microsoft_kiota_faraday/microsoft_kiota_faraday.gemspec index dfbabc76e4..138cfd46b6 100644 --- a/http/ruby/nethttp/microsoft_kiota_nethttplibrary/microsoft_kiota_nethttplibrary.gemspec +++ b/http/ruby/faraday/microsoft_kiota_faraday/microsoft_kiota_faraday.gemspec @@ -1,14 +1,14 @@ # frozen_string_literal: true -require_relative "lib/microsoft_kiota_nethttplibrary/version" +require_relative "lib/microsoft_kiota_faraday/version" Gem::Specification.new do |spec| - spec.name = "microsoft_kiota_nethttplibrary" - spec.version = MicrosoftKiotaNethttplibrary::VERSION + spec.name = "microsoft_kiota_faraday" + spec.version = MicrosoftKiotaFaraday::VERSION spec.authors = 'Microsoft Corporation' spec.email = 'graphsdkpub@microsoft.com' - spec.description = 'Kiota HttpCore implementation with net/http' - spec.summary = "Microsoft Kiota nethttp - Kiota Ruby http core library for running requests" + spec.description = 'Kiota HttpCore implementation with Faraday' + spec.summary = "Microsoft Kiota Faraday - Kiota Ruby http request adapter for running requests" spec.homepage = 'https://microsoft.github.io/kiota/' spec.license = 'MIT' spec.metadata = { @@ -28,4 +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_runtime_dependency 'microsoft_kiota_abstractions', '~> 0.5.0', '>= 0.5.0' + spec.add_runtime_dependency 'faraday', '~> 2.7', '>= 2.7.2' + spec.add_development_dependency 'rake', '~> 13.0' + spec.add_development_dependency 'rspec', '~> 3.0' + spec.add_development_dependency 'rubocop' end diff --git a/http/ruby/nethttp/microsoft_kiota_nethttplibrary/spec/authentication_provider.rb b/http/ruby/faraday/microsoft_kiota_faraday/spec/authentication_provider.rb similarity index 70% rename from http/ruby/nethttp/microsoft_kiota_nethttplibrary/spec/authentication_provider.rb rename to http/ruby/faraday/microsoft_kiota_faraday/spec/authentication_provider.rb index 6ca02c5488..0a65a84ce9 100644 --- a/http/ruby/nethttp/microsoft_kiota_nethttplibrary/spec/authentication_provider.rb +++ b/http/ruby/faraday/microsoft_kiota_faraday/spec/authentication_provider.rb @@ -1,3 +1,5 @@ +require 'microsoft_kiota_abstractions' + class AuthenticationProvider include MicrosoftKiotaAbstractions::AuthenticationProvider end \ No newline at end of file diff --git a/http/ruby/nethttp/microsoft_kiota_nethttplibrary/spec/microsoft_kiota_nethttp_spec.rb b/http/ruby/faraday/microsoft_kiota_faraday/spec/microsoft_kiota_faraday.rb similarity index 61% rename from http/ruby/nethttp/microsoft_kiota_nethttplibrary/spec/microsoft_kiota_nethttp_spec.rb rename to http/ruby/faraday/microsoft_kiota_faraday/spec/microsoft_kiota_faraday.rb index 4f9551abe4..c3479b22d9 100644 --- a/http/ruby/nethttp/microsoft_kiota_nethttplibrary/spec/microsoft_kiota_nethttp_spec.rb +++ b/http/ruby/faraday/microsoft_kiota_faraday/spec/microsoft_kiota_faraday.rb @@ -1,9 +1,9 @@ # frozen_string_literal: true require_relative 'authentication_provider' -RSpec.describe MicrosoftKiotaNethttplibrary do +RSpec.describe MicrosoftKiotaFaraday do it "has a version number" do - expect(MicrosoftKiotaNethttplibrary::VERSION).not_to be nil + expect(MicrosoftKiotaFaraday::VERSION).not_to be nil end it "does something useful" do @@ -12,7 +12,7 @@ it "can instantiate a request adapter" do auth_provider = AuthenticationProvider.new() - adapter = MicrosoftKiotaNethttplibrary::NetHttpRequestAdapter.new(auth_provider) + adapter = MicrosoftKiotaFaraday::FaradayRequestAdapter.new(auth_provider) expect(adapter).to_not be nil end end diff --git a/http/ruby/faraday/microsoft_kiota_faraday/spec/parameters_name_decoding_handler_spec.rb b/http/ruby/faraday/microsoft_kiota_faraday/spec/parameters_name_decoding_handler_spec.rb new file mode 100644 index 0000000000..bb4fa370f9 --- /dev/null +++ b/http/ruby/faraday/microsoft_kiota_faraday/spec/parameters_name_decoding_handler_spec.rb @@ -0,0 +1,69 @@ +require 'faraday' + +# frozen_string_literal: true +RSpec.describe MicrosoftKiotaFaraday do + it "decodes encoded query parameters" do + values = Hash.new + values["?%24select=diplayName&api%2Dversion=2"] = "?$select=diplayName&api-version=2" + values["?%24select=diplayName&api%7Eversion=2"] = "?$select=diplayName&api~version=2" + values["?%24select=diplayName&api%2Eversion=2"] = "?$select=diplayName&api.version=2" + values["/api-version/?%24select=diplayName&api%2Eversion=2"] = "/api-version/?$select=diplayName&api.version=2" + values[""] = "" + + handler = MicrosoftKiotaFaraday::Middleware::ParametersNameDecodingHandler.new() + values.each do |key, value| + env = { + url: URI.parse("https://graph.microsoft.com/v1.0/users#{key}") + } + handler.call(env) + expect(env[:url].to_s).to eq("https://graph.microsoft.com/v1.0/users#{value}") + end + end + + it "doesn't decode when disabled on handler" do + values = Hash.new + values["?%24select=diplayName&api%2Dversion=2"] = "?%24select=diplayName&api%2Dversion=2" + values["?%24select=diplayName&api%7Eversion=2"] = "?%24select=diplayName&api%7Eversion=2" + values["?%24select=diplayName&api%2Eversion=2"] = "?%24select=diplayName&api%2Eversion=2" + values["/api-version/?%24select=diplayName&api%2Eversion=2"] = "/api-version/?%24select=diplayName&api%2Eversion=2" + values[""] = "" + + option = MicrosoftKiotaFaraday::Middleware::ParametersNameDecodingOption.new(false) + handler = MicrosoftKiotaFaraday::Middleware::ParametersNameDecodingHandler.new(nil, option) + values.each do |key, value| + env = { + url: URI.parse("https://graph.microsoft.com/v1.0/users#{key}"), + request: { + context: { + } + } + } + handler.call(env) + expect(env[:url].to_s).to eq("https://graph.microsoft.com/v1.0/users#{value}") + end + end + + it "doesn't decode when disabled on request" do + values = Hash.new + values["?%24select=diplayName&api%2Dversion=2"] = "?%24select=diplayName&api%2Dversion=2" + values["?%24select=diplayName&api%7Eversion=2"] = "?%24select=diplayName&api%7Eversion=2" + values["?%24select=diplayName&api%2Eversion=2"] = "?%24select=diplayName&api%2Eversion=2" + values["/api-version/?%24select=diplayName&api%2Eversion=2"] = "/api-version/?%24select=diplayName&api%2Eversion=2" + values[""] = "" + + option = MicrosoftKiotaFaraday::Middleware::ParametersNameDecodingOption.new(false) + handler = MicrosoftKiotaFaraday::Middleware::ParametersNameDecodingHandler.new() + values.each do |key, value| + env = { + url: URI.parse("https://graph.microsoft.com/v1.0/users#{key}"), + request: { + context: { + option.get_key => option + } + } + } + handler.call(env) + expect(env[:url].to_s).to eq("https://graph.microsoft.com/v1.0/users#{value}") + end + end +end diff --git a/http/ruby/nethttp/microsoft_kiota_nethttplibrary/spec/spec_helper.rb b/http/ruby/faraday/microsoft_kiota_faraday/spec/spec_helper.rb similarity index 91% rename from http/ruby/nethttp/microsoft_kiota_nethttplibrary/spec/spec_helper.rb rename to http/ruby/faraday/microsoft_kiota_faraday/spec/spec_helper.rb index 9a238ba17e..3f2bbab23c 100644 --- a/http/ruby/nethttp/microsoft_kiota_nethttplibrary/spec/spec_helper.rb +++ b/http/ruby/faraday/microsoft_kiota_faraday/spec/spec_helper.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -require "microsoft_kiota_nethttplibrary" +require "microsoft_kiota_faraday" Dir["#{File.dirname(__FILE__)}/./../lib/*.rb"].each { |f| load(f) } diff --git a/http/ruby/nethttp/microsoft_kiota_nethttplibrary/Gemfile b/http/ruby/nethttp/microsoft_kiota_nethttplibrary/Gemfile deleted file mode 100644 index 0003fc6acc..0000000000 --- a/http/ruby/nethttp/microsoft_kiota_nethttplibrary/Gemfile +++ /dev/null @@ -1,17 +0,0 @@ -# frozen_string_literal: true - -source 'https://rubygems.org' - -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.4.0' -end - -gem 'rake', '~> 13.0' - -gem 'rspec', '~> 3.0' - -gem 'rubocop', require: false diff --git a/http/ruby/nethttp/microsoft_kiota_nethttplibrary/lib/microsoft_kiota_nethttplibrary.rb b/http/ruby/nethttp/microsoft_kiota_nethttplibrary/lib/microsoft_kiota_nethttplibrary.rb deleted file mode 100644 index 9cebd10341..0000000000 --- a/http/ruby/nethttp/microsoft_kiota_nethttplibrary/lib/microsoft_kiota_nethttplibrary.rb +++ /dev/null @@ -1,7 +0,0 @@ -# frozen_string_literal: true - -require_relative 'microsoft_kiota_nethttplibrary/version' -require_relative 'microsoft_kiota_nethttplibrary/net_http_request_adapter' - -module MicrosoftKiotaNethttplibrary -end diff --git a/http/ruby/nethttp/microsoft_kiota_nethttplibrary/lib/microsoft_kiota_nethttplibrary/version.rb b/http/ruby/nethttp/microsoft_kiota_nethttplibrary/lib/microsoft_kiota_nethttplibrary/version.rb deleted file mode 100644 index 8ca6a692bd..0000000000 --- a/http/ruby/nethttp/microsoft_kiota_nethttplibrary/lib/microsoft_kiota_nethttplibrary/version.rb +++ /dev/null @@ -1,5 +0,0 @@ -# frozen_string_literal: true - -module MicrosoftKiotaNethttplibrary - VERSION = '0.3.0' -end diff --git a/serialization/ruby/json/microsoft_kiota_serialization/Gemfile b/serialization/ruby/json/microsoft_kiota_serialization/Gemfile index b540f10094..bbaf55e9e7 100644 --- a/serialization/ruby/json/microsoft_kiota_serialization/Gemfile +++ b/serialization/ruby/json/microsoft_kiota_serialization/Gemfile @@ -1,19 +1,9 @@ # frozen_string_literal: true source "https://rubygems.org" +source "https://rubygems.pkg.github.com/microsoft" 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 'uuidtools' diff --git a/serialization/ruby/json/microsoft_kiota_serialization/lib/microsoft_kiota_serialization/version.rb b/serialization/ruby/json/microsoft_kiota_serialization/lib/microsoft_kiota_serialization/version.rb index 46d3922c29..b80563375b 100644 --- a/serialization/ruby/json/microsoft_kiota_serialization/lib/microsoft_kiota_serialization/version.rb +++ b/serialization/ruby/json/microsoft_kiota_serialization/lib/microsoft_kiota_serialization/version.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true module MicrosoftKiotaSerialization - VERSION = "0.4.0" + VERSION = "0.5.0" end diff --git a/serialization/ruby/json/microsoft_kiota_serialization/microsoft_kiota_serialization.gemspec b/serialization/ruby/json/microsoft_kiota_serialization/microsoft_kiota_serialization.gemspec index 9b090ca98f..ce96566387 100644 --- a/serialization/ruby/json/microsoft_kiota_serialization/microsoft_kiota_serialization.gemspec +++ b/serialization/ruby/json/microsoft_kiota_serialization/microsoft_kiota_serialization.gemspec @@ -29,6 +29,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 'uuidtools' + spec.add_runtime_dependency 'microsoft_kiota_abstractions', '~> 0.5.0', '>= 0.5.0' + spec.add_runtime_dependency 'uuidtools' + spec.add_development_dependency 'rake', '~> 13.0' + spec.add_development_dependency 'rspec', '~> 3.0' + spec.add_development_dependency 'rubocop' end diff --git a/src/Kiota.Builder/Writers/Ruby/CodeMethodWriter.cs b/src/Kiota.Builder/Writers/Ruby/CodeMethodWriter.cs index 223a7e4672..3dcc1b52fc 100644 --- a/src/Kiota.Builder/Writers/Ruby/CodeMethodWriter.cs +++ b/src/Kiota.Builder/Writers/Ruby/CodeMethodWriter.cs @@ -208,14 +208,16 @@ private void WriteRequestGeneratorBody(CodeMethod codeElement, RequestParams req { var queryString = requestParams.QueryParameters; var headers = requestParams.Headers; - // TODO add options handling + var options = requestParams.Options; if(headers != null || queryString != null) { writer.WriteLine($"unless {requestParams.requestConfiguration.Name.ToSnakeCase()}.nil?"); writer.IncreaseIndent(); if(headers != null) writer.WriteLine($"request_info.set_headers_from_raw_object({requestParams.requestConfiguration.Name.ToSnakeCase()}.{headers.Name.ToSnakeCase()})"); if(queryString != null) - writer.WriteLines($"request_info.set_query_string_parameters_from_raw_object({requestParams.requestConfiguration.Name.ToSnakeCase()}.{queryString.Name.ToSnakeCase()})"); + writer.WriteLine($"request_info.set_query_string_parameters_from_raw_object({requestParams.requestConfiguration.Name.ToSnakeCase()}.{queryString.Name.ToSnakeCase()})"); + if (options != null) + writer.WriteLine($"request_info.add_request_options({requestParams.requestConfiguration.Name.ToSnakeCase()}.{options.Name.ToSnakeCase()})"); writer.CloseBlock("end"); } if(requestParams.requestBody != null) { diff --git a/tests/Kiota.Builder.Tests/Writers/Ruby/CodeMethodWriterTests.cs b/tests/Kiota.Builder.Tests/Writers/Ruby/CodeMethodWriterTests.cs index ec59ab95d2..c2008e30a0 100644 --- a/tests/Kiota.Builder.Tests/Writers/Ruby/CodeMethodWriterTests.cs +++ b/tests/Kiota.Builder.Tests/Writers/Ruby/CodeMethodWriterTests.cs @@ -34,17 +34,21 @@ public CodeMethodWriterTests() Name = "parentClass" }; root.AddClass(parentClass); - method = new CodeMethod { + method = new CodeMethod + { Name = MethodName, + ReturnType = new CodeType + { + Name = ReturnTypeName + } }; - method.ReturnType = new CodeType { - Name = ReturnTypeName - }; - voidMethod = new CodeMethod { + voidMethod = new CodeMethod + { Name = MethodName, - }; - voidMethod.ReturnType = new CodeType { - Name = "void" + ReturnType = new CodeType + { + Name = "void" + } }; parentClass.AddMethod(voidMethod); parentClass.AddMethod(method); @@ -211,6 +215,7 @@ public void WritesRequestGeneratorBody() { Assert.Contains("request_info.url_template", result); Assert.Contains("http_method = :GET", result); Assert.Contains("set_query_string_parameters_from_raw_object", result); + Assert.Contains("add_request_options", result); Assert.Contains("set_content_from_parsable", result); Assert.Contains("return request_info", result); } @@ -464,13 +469,15 @@ public void WritesApiConstructorWithBackingStore() { Kind = CodeParameterKind.RequestAdapter, Type = coreProp.Type, }); - var backingStoreParam = new CodeParameter { + var backingStoreParam = new CodeParameter + { Name = "backingStore", Kind = CodeParameterKind.BackingStore, - }; - backingStoreParam.Type = new CodeType { - Name = "BackingStore", - IsExternal = true, + Type = new CodeType + { + Name = "BackingStore", + IsExternal = true, + } }; method.AddParameter(backingStoreParam); var tempWriter = LanguageWriter.GetLanguageWriter(GenerationLanguage.Java, DefaultPath, DefaultName);