Class: MicrosoftKiotaFaraday::FaradayRequestAdapter

Inherits:
Object
  • Object
show all
Includes:
MicrosoftKiotaAbstractions::RequestAdapter
Defined in:
lib/microsoft_kiota_faraday/faraday_request_adapter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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.



13
14
15
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 13

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'
  end
  @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
  if @client.nil?
    @client = KiotaClientFactory::get_default_http_client
  end
  @base_url = ''
end

Instance Attribute Details

#authentication_providerObject

Returns the value of attribute authentication_provider.



11
12
13
# File 'lib/microsoft_kiota_faraday/faraday_request_adapter.rb', line 11

def authentication_provider
  @authentication_provider
end

#clientObject

Returns the value of attribute client.



11
12
13
# File 'lib/microsoft_kiota_faraday/faraday_request_adapter.rb', line 11

def client
  @client
end

#content_type_header_keyObject

Returns the value of attribute content_type_header_key.



11
12
13
# File 'lib/microsoft_kiota_faraday/faraday_request_adapter.rb', line 11

def content_type_header_key
  @content_type_header_key
end

#parse_node_factoryObject

Returns the value of attribute parse_node_factory.



11
12
13
# File 'lib/microsoft_kiota_faraday/faraday_request_adapter.rb', line 11

def parse_node_factory
  @parse_node_factory
end

#serialization_writer_factoryObject

Returns the value of attribute serialization_writer_factory.



11
12
13
# File 'lib/microsoft_kiota_faraday/faraday_request_adapter.rb', line 11

def serialization_writer_factory
  @serialization_writer_factory
end

Instance Method Details

#convert_to_native_request_async(request_info) ⇒ Object

Raises:

  • (StandardError)


153
154
155
156
157
158
159
160
# File 'lib/microsoft_kiota_faraday/faraday_request_adapter.rb', line 153

def convert_to_native_request_async(request_info)
  raise StandardError, 'request_info cannot be null' unless request_info
  return Fiber.new do
    set_base_url_for_request_information(request_info)
    @authentication_provider.authenticate_request(request_info).resume
    return self.get_request_from_request_info(request_info)
  end
end

#get_base_urlObject



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



96
97
98
99
100
101
102
103
104
105
106
107
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
# File 'lib/microsoft_kiota_faraday/faraday_request_adapter.rb', line 96

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? then
    request.headers = Faraday::Utils::Headers.new
    request_info.headers.get_all.select{|k,v|
      if v.kind_of? Array then
        request.headers[k] = v.join(',')
      elsif v.kind_of? String then
        request.headers[k] = v
      else
        request.headers[k] = v.to_s
      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_options = request_info.get_request_options
  if !request_options.nil? && !request_options.empty? then
    request.options = Faraday::RequestOptions.new if request.options.nil?
    request_options.each do |value|
      request.options.context[value.get_key] = value
    end
  end
  request
end

#get_response_content_type(response) ⇒ Object



145
146
147
148
149
150
151
# File 'lib/microsoft_kiota_faraday/faraday_request_adapter.rb', line 145

def get_response_content_type(response)
  begin
    response.headers['content-type'].split(';')[0].downcase()
  rescue
    return nil
  end
end

#get_response_handler(request_info) ⇒ Object



65
66
67
68
# File 'lib/microsoft_kiota_faraday/faraday_request_adapter.rb', line 65

def get_response_handler(request_info)
  option = request_info.get_request_option(MicrosoftKiotaFaraday::Middleware::ResponseHandlerOption::RESPONSE_HANDLER_KEY) unless request_info.nil?
  return option.async_callback unless !option || option.nil?
end

#get_root_parse_node(response) ⇒ Object

Raises:

  • (StandardError)


70
71
72
73
74
75
76
# File 'lib/microsoft_kiota_faraday/faraday_request_adapter.rb', line 70

def get_root_parse_node(response)
  raise StandardError, 'response cannot be null' unless response
  response_content_type = self.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?
  return @parse_node_factory.get_parse_node(response_content_type, response.body)
end

#get_serialization_writer_factoryObject



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

Raises:

  • (StandardError)


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 = self.get_request_from_request_info(request_info)
    response = @client.run_request(request.http_method, request.path, request.body, request.headers)

    response_handler = self.get_response_handler(request_info)
    response_handler.call(response).resume unless response_handler.nil?
    self.throw_if_failed_reponse(response, errors_mapping)
    root_node = self.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

Raises:

  • (StandardError)


78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/microsoft_kiota_faraday/faraday_request_adapter.rb', line 78

def throw_if_failed_reponse(response, errors_mapping)
  raise StandardError, 'response cannot be null' unless response

  status_code = response.status;
  if status_code < 400 then
    return
  end
  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
  error_factory = errors_mapping['5XX'] unless !error_factory.nil? || errors_mapping.nil? || status_code < 500 || status_code > 600
  error_factory = errors_mapping['XXX'] unless !error_factory.nil? || errors_mapping.nil? || status_code < 400 || status_code > 600
  raise MicrosoftKiotaAbstractions::ApiError, 'The server returned an unexpected status code and no error factory is registered for this code:' + status_code.to_s if error_factory.nil?
  root_node = self.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.to_s
end