• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

pulibrary / tigerdata-app / 88c82417-71e1-4f84-8337-7bf4bd1a5618

29 Jul 2025 08:15PM UTC coverage: 75.423% (-5.2%) from 80.666%
88c82417-71e1-4f84-8337-7bf4bd1a5618

Pull #1656

circleci

hectorcorrea
Marked a bunch of tests as integration since they create projects in Mediaflux
Pull Request #1656: Added error handling to the request to project workflow

12 of 17 new or added lines in 4 files covered. (70.59%)

157 existing lines in 19 files now uncovered.

2274 of 3015 relevant lines covered (75.42%)

344.11 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

91.86
/app/models/mediaflux/request.rb
1
# frozen_string_literal: true
2
module Mediaflux
1✔
3
    class Request
1✔
4

5
      # As this is an abstract class, this should be overridden to specify the Mediaflux API service
6
      def self.service
1✔
7
        raise(NotImplementedError, "#{self} is an abstract class, please override #{self}.service")
1✔
8
      end
9

10
      # The default request URL path for the Mediaflux API
11
      # @return [String]
12
      def self.request_path
1✔
13
        "/__mflux_svc__"
1,562✔
14
      end
15

16
      def self.uri
1✔
17
        # Setting the URI to the Ansible build or Docker build of mediaflux depending on the environment
18
        URI("#{Connection.transport}://#{Connection.host}:#{Connection.port}#{request_path}")
785✔
19
      end
20

21
      # Constructs a new HTTP POST request for usage with the Mediaflux API
22
      # @return [Net::HTTP::Post]
23
      def self.build_post_request
1✔
24
        Net::HTTP::Post.new(request_path)
777✔
25
      end
26

27
      # The default XML namespace which should be used for building the XML
28
      #   Document transmitted in the body of the HTTP request
29
      # @return [String]
30
      def self.default_xml_namespace
1✔
31
        "tigerdata"
1✔
32
      end
33

34
      def self.default_xml_namespace_uri
1✔
35
        "http://tigerdata.princeton.edu"
1✔
36
      end
37

38
      # Constructs and memoizes a new instance of the Net::HTTP::Persistent object at the level of the Class
39
      # @returns http_client [Net::HTTP::Persistent] HTTP client for transmitting requests to the Mediaflux server API
40
      def self.find_or_create_http_client
1✔
41
        HttpConnection.instance.http_client
780✔
42
      end
43

44
      attr_reader :session_token
1✔
45

46
      # Constructor
47
      # @param file [File] any upload file required for the POST request
48
      # @param session_token [String] the API token for the authenticated session
49
      # @param http_client [Net::HTTP::Persistent] HTTP client for transmitting requests to the Mediaflux server API
50
      def initialize(file: nil, session_token: nil, http_client: nil, session_user: nil)
1✔
51
        @http_client = http_client || self.class.find_or_create_http_client
780✔
52
        @file = file
780✔
53
        @session_token = session_token
780✔
54
        @session_user = session_user
780✔
55
      end
56

57
      # Resolves the HTTP request against the Mediaflux API
58
      # @return [Net::HTTP]
59
      def resolve
1✔
60
        @http_response = @http_client.request self.class.uri, http_request
776✔
61
      end
62

63
      # Determines whether or not the request has been resolved
64
      # @return [Boolean]
65
      def resolved?
1✔
66
        @http_response.present?
1,383✔
67
      end
68

69
      # Resolves the HTTP request, and returns the XML Document parsed from the response body
70
      # @return [Nokogiri::XML::Document]
71
      def response_xml
1✔
72
        resolve unless resolved?
1,383✔
73
        Rails.logger.debug(response_body)
1,383✔
74
        @response_xml ||= Nokogiri::XML.parse(response_body)
1,383✔
75
        Rails.logger.debug(@response_xml)
1,383✔
76
        if @response_xml.xpath("//message").text == "session is not valid"
1,383✔
UNCOV
77
          raise Mediaflux::SessionExpired, session_token
×
78
        end
79

80
        @response_xml
1,383✔
81
      end
82

83
      # The response body of the mediaflux call
84
      def response_body
1✔
85
        @response_body ||= http_response.body
2,151✔
86
      end
87

88
      def error?
1✔
89
        response_error.present?
615✔
90
      end
91

92
      def response_error
1✔
93
        xml = response_xml
620✔
94
        return nil if xml.xpath("/response/reply/error").count == 0
620✔
95
        error = {
96
          title: xml.xpath("/response/reply/error").text,
9✔
97
          message: xml.xpath("/response/reply/message").text
98
        }
99
        Rails.logger.error "MediaFlux error: #{error[:title]}, #{error[:message]}"
9✔
100
        error
9✔
101
      end
102

103
      delegate :to_s, to: :response_xml
1✔
104

105
      def xml_payload( name: self.class.service)
1✔
106
        body = build_http_request_body(name: )
1,555✔
107
        xml_payload = body.to_xml
1,555✔
108
      end
109

110
      # The output of this routine can be passed to xtoshell in aterm.  The output of which can be sent to service.execute
111
      # @param [String] name name of the service this request will send
112
      # @return [String] xml that can be passed to xtoshell without manipulation
113
      def xtoshell_xml( name: self.class.service)
1✔
114
        xml_builder = build_http_request_body(name: )
×
115
        xml_builder.doc.xpath("//request/service/@session").remove
×
116
        xml = xml_builder.to_xml(:save_with => Nokogiri::XML::Node::SaveOptions::AS_XML | Nokogiri::XML::Node::SaveOptions::NO_DECLARATION)
×
117
        xml.strip.gsub("\"","'").gsub("<args>","").gsub("</args>","")
×
118
      end
119

120
      # This method is used for transforming iso8601 dates to dates that MediaFlux likes
121
      # Take a string like "2024-02-26T10:33:11-05:00" and convert this string to "22-FEB-2024 13:57:19"
122
      def self.format_date_for_mediaflux(iso8601_date)
1✔
123
        return if iso8601_date.nil?
×
124
        Time.format_date_for_mediaflux(iso8601_date)
×
125
      end
126

127
      private
1✔
128

129
        def http_request
1✔
130
          @http_request ||= build_http_request(name: self.class.service, form_file: @file)
778✔
131
        end
132

133
        def http_response
1✔
134
          @http_response ||= resolve
766✔
135
        end
136

137
        def build_http_request_body(name:)
1✔
138
          args = { name: name }
1,555✔
139
                                                # must use @session_token here instead of session_token
140
                                                #  for login to not go into an infinaite loop
141
          args[:session] = session_token unless @session_token.nil?
1,555✔
142

143
          Nokogiri::XML::Builder.new(namespace_inheritance: false) do |xml|
1,555✔
144
            xml.request do
1,555✔
145
              xml.service(**args) do
1,555✔
146
                yield xml if block_given?
1,555✔
147
              end
148
            end
149
          end
150
        end
151

152
        # rubocop:disable Metrics/MethodLength
153
        def build_http_request(name:, form_file: nil)
1✔
154
          request = self.class.build_post_request
777✔
155

156
          log_xml_request(xml_payload)
777✔
157
          set_authentication_headers(request)
777✔
158

159
          if form_file.nil?
777✔
160
            request["Content-Type"] = "text/xml; charset=utf-8"
776✔
161
            request.body = xml_payload(name:)
776✔
162
          else
163
            request["Content-Type"] = "multipart/form-data"
1✔
164
            request.set_form({ "request" => xml_payload,
1✔
165
                               "nb-data-attachments" => "1",
166
                               "file_0" => form_file },
167
                          "multipart/form-data",
168
                          "charset" => "UTF-8")
169
          end
170

171
          request
777✔
172
        end
173
      # rubocop:enable Metrics/MethodLength
174

175
      def log_xml_request(xml_payload)
1✔
176
        password_element = xml_payload.match(/\<password\>.*\<\/password\>/)
777✔
177
        if password_element.nil?
777✔
178
          Rails.logger.debug(xml_payload)
158✔
179
        else
180
          # Don't log the password
181
          Rails.logger.debug(xml_payload.gsub(password_element.to_s, "<password>***</password>"))
619✔
182
        end
183
      end
184

185
      # Authentication code to push a few custom HTTP headers to Mediaflux
186
      # Eventually the `session_user` will need to be an object that provides the timeout value.
187
      def set_authentication_headers(request)
1✔
188
        return if @session_user.nil?
777✔
189

190
        request["mediaflux.sso.user"] = @session_user.uid
1✔
191
      end
192
    end
193
end
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc