Class: MicrosoftKiotaFaraday::FaradayRequestAdapter
- Inherits:
-
Object
- Object
- MicrosoftKiotaFaraday::FaradayRequestAdapter
- Includes:
- MicrosoftKiotaAbstractions::RequestAdapter
- Defined in:
- lib/microsoft_kiota_faraday/faraday_request_adapter.rb
Instance Attribute Summary collapse
-
#authentication_provider ⇒ Object
Returns the value of attribute authentication_provider.
-
#client ⇒ Object
Returns the value of attribute client.
-
#content_type_header_key ⇒ Object
Returns the value of attribute content_type_header_key.
-
#parse_node_factory ⇒ Object
Returns the value of attribute parse_node_factory.
-
#serialization_writer_factory ⇒ Object
Returns the value of attribute serialization_writer_factory.
Instance Method Summary collapse
- #convert_to_native_request_async(request_info) ⇒ Object
- #get_base_url ⇒ Object
- #get_request_from_request_info(request_info) ⇒ Object
- #get_response_content_type(response) ⇒ Object
- #get_response_handler(request_info) ⇒ Object
- #get_root_parse_node(response) ⇒ Object
- #get_serialization_writer_factory ⇒ Object
-
#initialize(authentication_provider, parse_node_factory = MicrosoftKiotaAbstractions::ParseNodeFactoryRegistry.default_instance, serialization_writer_factory = MicrosoftKiotaAbstractions::SerializationWriterFactoryRegistry.default_instance, client = KiotaClientFactory.get_default_http_client) ⇒ FaradayRequestAdapter
constructor
A new instance of FaradayRequestAdapter.
- #send_async(request_info, factory, errors_mapping) ⇒ Object
- #set_base_url(base_url) ⇒ Object
- #throw_if_failed_reponse(response, errors_mapping) ⇒ Object
Constructor Details
#initialize(authentication_provider, parse_node_factory = MicrosoftKiotaAbstractions::ParseNodeFactoryRegistry.default_instance, serialization_writer_factory = MicrosoftKiotaAbstractions::SerializationWriterFactoryRegistry.default_instance, client = KiotaClientFactory.get_default_http_client) ⇒ FaradayRequestAdapter
Returns a new instance of FaradayRequestAdapter.
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/microsoft_kiota_faraday/faraday_request_adapter.rb', line 16 def initialize(authentication_provider, parse_node_factory = MicrosoftKiotaAbstractions::ParseNodeFactoryRegistry.default_instance, serialization_writer_factory = MicrosoftKiotaAbstractions::SerializationWriterFactoryRegistry.default_instance, client = KiotaClientFactory.get_default_http_client) raise StandardError, 'authentication provider cannot be null' unless authentication_provider @authentication_provider = authentication_provider @content_type_header_key = 'Content-Type' @parse_node_factory = parse_node_factory if @parse_node_factory.nil? @parse_node_factory = MicrosoftKiotaAbstractions::ParseNodeFactoryRegistry.default_instance end @serialization_writer_factory = serialization_writer_factory if @serialization_writer_factory.nil? @serialization_writer_factory = MicrosoftKiotaAbstractions::SerializationWriterFactoryRegistry.default_instance end @client = client @client = KiotaClientFactory.get_default_http_client if @client.nil? @base_url = '' end |
Instance Attribute Details
#authentication_provider ⇒ Object
Returns the value of attribute authentication_provider.
13 14 15 |
# File 'lib/microsoft_kiota_faraday/faraday_request_adapter.rb', line 13 def authentication_provider @authentication_provider end |
#client ⇒ Object
Returns the value of attribute client.
13 14 15 |
# File 'lib/microsoft_kiota_faraday/faraday_request_adapter.rb', line 13 def client @client end |
#content_type_header_key ⇒ Object
Returns the value of attribute content_type_header_key.
13 14 15 |
# File 'lib/microsoft_kiota_faraday/faraday_request_adapter.rb', line 13 def content_type_header_key @content_type_header_key end |
#parse_node_factory ⇒ Object
Returns the value of attribute parse_node_factory.
13 14 15 |
# File 'lib/microsoft_kiota_faraday/faraday_request_adapter.rb', line 13 def parse_node_factory @parse_node_factory end |
#serialization_writer_factory ⇒ Object
Returns the value of attribute serialization_writer_factory.
13 14 15 |
# File 'lib/microsoft_kiota_faraday/faraday_request_adapter.rb', line 13 def serialization_writer_factory @serialization_writer_factory end |
Instance Method Details
#convert_to_native_request_async(request_info) ⇒ Object
163 164 165 166 167 168 169 170 171 |
# File 'lib/microsoft_kiota_faraday/faraday_request_adapter.rb', line 163 def convert_to_native_request_async(request_info) raise StandardError, 'request_info cannot be null' unless request_info Fiber.new do set_base_url_for_request_information(request_info) @authentication_provider.authenticate_request(request_info).resume return get_request_from_request_info(request_info) end end |
#get_base_url ⇒ Object
39 40 41 |
# File 'lib/microsoft_kiota_faraday/faraday_request_adapter.rb', line 39 def get_base_url @base_url end |
#get_request_from_request_info(request_info) ⇒ Object
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/microsoft_kiota_faraday/faraday_request_adapter.rb', line 108 def get_request_from_request_info(request_info) set_base_url_for_request_information(request_info) case request_info.http_method when :GET request = @client.build_request(:get) when :POST request = @client.build_request(:post) when :PATCH request = @client.build_request(:patch) when :DELETE request = @client.build_request(:delete) when :OPTIONS request = @client.build_request(:options) when :CONNECT request = @client.build_request(:connect) when :PUT request = @client.build_request(:put) when :TRACE request = @client.build_request(:trace) when :HEAD request = @client.build_request(:head) else raise StandardError, 'unsupported http method' end request.path = request_info.uri unless request_info.headers.nil? request.headers = Faraday::Utils::Headers.new request_info.headers.get_all.select do |k, v| request.headers[k] = if v.is_a? Array v.join(',') elsif v.is_a? String v else v.to_s end end end 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_info. if !.nil? && !.empty? request. = Faraday::RequestOptions.new if request..nil? .each do |value| request..context[value.get_key] = value end end request end |
#get_response_content_type(response) ⇒ Object
157 158 159 160 161 |
# File 'lib/microsoft_kiota_faraday/faraday_request_adapter.rb', line 157 def get_response_content_type(response) response.headers['content-type'].split(';')[0].downcase rescue StandardError nil end |
#get_response_handler(request_info) ⇒ Object
65 66 67 68 69 70 |
# File 'lib/microsoft_kiota_faraday/faraday_request_adapter.rb', line 65 def get_response_handler(request_info) unless request_info.nil? option = request_info.get_request_option(MicrosoftKiotaFaraday::Middleware::ResponseHandlerOption::RESPONSE_HANDLER_KEY) end option.async_callback unless !option || option.nil? end |
#get_root_parse_node(response) ⇒ Object
72 73 74 75 76 77 78 79 80 |
# File 'lib/microsoft_kiota_faraday/faraday_request_adapter.rb', line 72 def get_root_parse_node(response) raise StandardError, 'response cannot be null' unless response response_content_type = get_response_content_type(response) raise StandardError, 'no response content type found for deserialization' unless response_content_type return if response.body.nil? || response.body.empty? @parse_node_factory.get_parse_node(response_content_type, response.body) end |
#get_serialization_writer_factory ⇒ Object
43 44 45 |
# File 'lib/microsoft_kiota_faraday/faraday_request_adapter.rb', line 43 def get_serialization_writer_factory @serialization_writer_factory end |
#send_async(request_info, factory, errors_mapping) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/microsoft_kiota_faraday/faraday_request_adapter.rb', line 47 def send_async(request_info, factory, errors_mapping) raise StandardError, 'request_info cannot be null' unless request_info raise StandardError, 'factory cannot be null' unless factory Fiber.new do set_base_url_for_request_information(request_info) @authentication_provider.authenticate_request(request_info).resume request = get_request_from_request_info(request_info) response = @client.run_request(request.http_method, request.path, request.body, request.headers) response_handler = get_response_handler(request_info) response_handler&.call(response)&.resume throw_if_failed_reponse(response, errors_mapping) root_node = get_root_parse_node(response) root_node.get_object_value(factory) end end |
#set_base_url(base_url) ⇒ Object
35 36 37 |
# File 'lib/microsoft_kiota_faraday/faraday_request_adapter.rb', line 35 def set_base_url(base_url) @base_url = base_url end |
#throw_if_failed_reponse(response, errors_mapping) ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/microsoft_kiota_faraday/faraday_request_adapter.rb', line 82 def throw_if_failed_reponse(response, errors_mapping) raise StandardError, 'response cannot be null' unless response status_code = response.status return if status_code < 400 error_factory = errors_mapping[status_code] unless errors_mapping.nil? error_factory = errors_mapping['4XX'] unless !error_factory.nil? || errors_mapping.nil? || status_code > 500 unless !error_factory.nil? || errors_mapping.nil? || status_code < 500 || status_code > 600 error_factory = errors_mapping['5XX'] end unless !error_factory.nil? || errors_mapping.nil? || status_code < 400 || status_code > 600 error_factory = errors_mapping['XXX'] end if error_factory.nil? raise MicrosoftKiotaAbstractions::ApiError, "The server returned an unexpected status code and no error factory is registered for this code:#{status_code}" end root_node = get_root_parse_node(response) error = root_node.get_object_value(error_factory) unless root_node.nil? raise error unless error.nil? raise MicrosoftKiotaAbstractions::ApiError, "The server returned an unexpected status code:#{status_code}" end |