+
+
+
+
+33
+34
+35
+36
+37
+38
+39
+40
+41
+42
+43
+44
+45
+46
+47
+48
+49
+50
+51
+52
+53
+54
+55
+56
+57
+58
+59
+60
+61
+62
+63
+64
+65
+66
+67
+68
+69
+70
+71
+72
+73
+74
+75
+76
+77
+78
+79
+80
+81
+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
+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
+144
+145
+146
+147
+148
+149
+150
+151
+152
+153
+154
+155
+156
+157
+158
+159
+160
+161
+162
+163
+164
+165
+166
+167
+168
+169
+170
+171
+172
+173
+174
+175
+176
+177
+178
+179
+180
+181
+182
+183
+184
+185
+186
+187
+188
+189
+190
+191
+192
+193
+194
+195
+196
+197
+198
+199
+200
+201
+202
+203
+204
+205
+206
+207
+208
+209
+210
+211
+212
+213
+214
+215
+216
+217
+218
+219
+220
+221
+222
+223
+224
+225
+226
+ |
+
+ # File 'lib/inferno/entities/request.rb', line 33
+
+class Request < Entity
+ ATTRIBUTES = [
+ :id, :index, :verb, :url, :direction, :name, :status,
+ :request_body, :response_body, :result_id, :test_session_id, :created_at,
+ :updated_at, :headers, :tags
+ ].freeze
+ SUMMARY_FIELDS = [
+ :id, :index, :url, :verb, :direction, :name, :status, :result_id, :created_at, :updated_at
+ ].freeze
+
+ include Attributes
+
+ def initialize(params)
+ super(params, ATTRIBUTES - [:headers, :name])
+
+ @name = params[:name]&.to_sym
+ @headers = params[:headers]&.map { || .is_a?(Hash) ? Header.new() : } || []
+ format_tags(params[:tags] || [])
+ end
+
+ def format_tags(raw_tags)
+ @tags = raw_tags.map do |tag|
+ case tag
+ when Hash
+ tag[:name]
+ when String
+ tag
+ end
+ end
+ end
+
+ def query_parameters
+ Addressable::URI.parse(url).query_values || {}
+ end
+
+ def (name)
+ .find { || .name.casecmp(name).zero? }
+ end
+
+ def (name)
+ .find { || .name.casecmp(name).zero? }
+ end
+
+ def
+ .select(&:request?)
+ end
+
+ def
+ .select(&:response?)
+ end
+
+ def request
+ {
+ verb:,
+ url:,
+ headers: ,
+ body: request_body
+ }
+ end
+
+ def response
+ {
+ status:,
+ headers: ,
+ body: response_body
+ }
+ end
+
+ def to_hash
+ {
+ id:,
+ verb:,
+ url:,
+ direction:,
+ status:,
+ name:,
+ request_body:,
+ response_body:,
+ result_id:,
+ test_session_id:,
+ request_headers: .map(&:to_hash),
+ response_headers: .map(&:to_hash),
+ tags:,
+ created_at:,
+ updated_at:
+ }.compact
+ end
+
+ def resource
+ FHIR.from_contents(response_body)
+ end
+
+ class << self
+ def from_hanami_request(request, name: nil, tags: [])
+ url = "#{request.base_url}#{request.path}"
+ url += "?#{request.query_string}" if request.query_string.present?
+ =
+ request.params.env
+ .select { |key, _| key.start_with? 'HTTP_' }
+ .transform_keys { |key| key.delete_prefix('HTTP_').tr('_', '-').downcase }
+ .map { |, value| Header.new(name: , value:, type: 'request') }
+
+ new(
+ verb: request.request_method.downcase,
+ url:,
+ direction: 'incoming',
+ name:,
+ request_body: request.body.string,
+ headers: ,
+ tags:
+ )
+ end
+
+ def from_http_response(response, test_session_id:, direction: 'outgoing', name: nil, tags: [])
+ =
+ response.env.
+ .map { |, value| Header.new(name: .downcase, value:, type: 'request') }
+ =
+ response.
+ .map { |, value| Header.new(name: .downcase, value:, type: 'response') }
+
+ new(
+ verb: response.env.method.downcase,
+ url: response.env.url.to_s,
+ direction:,
+ name:,
+ status: response.status,
+ request_body: response.env.request_body,
+ response_body: response.body,
+ test_session_id:,
+ headers: + ,
+ tags:
+ )
+ end
+
+ def from_fhir_client_reply(reply, test_session_id:, direction: 'outgoing', name: nil, tags: [])
+ request = reply.request
+ response = reply.response
+ = request[:headers]
+ .map { |, value| Header.new(name: .downcase, value:, type: 'request') }
+ = response[:headers]
+ .map { |, value| Header.new(name: .downcase, value:, type: 'response') }
+ request_body =
+ if request.dig(:headers, 'Content-Type')&.include?('application/x-www-form-urlencoded')
+ URI.encode_www_form(request[:payload])
+ else
+ request[:payload]
+ end
+
+ new(
+ verb: request[:method].downcase,
+ url: request[:url],
+ direction:,
+ name:,
+ status: response[:code].to_i,
+ request_body:,
+ response_body: response[:body],
+ test_session_id:,
+ headers: + ,
+ tags:
+ )
+ end
+ end
+end
+ |
+
+