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

pulibrary / tigerdata-app / 416a33de-2f78-406d-a29e-a1c816eb7413

10 Nov 2025 09:50PM UTC coverage: 91.04% (-0.2%) from 91.201%
416a33de-2f78-406d-a29e-a1c816eb7413

Pull #2162

circleci

leefaisonr
adding icons to storage links on wizard
Pull Request #2162: Made "select HPC clusters" & "Globus" clickable and both link open new tabs

2835 of 3114 relevant lines covered (91.04%)

539.79 hits per line

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

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

5
      # As this is an abstract class, this should be overridden to specify the Mediaflux API service
6
      def self.service
5✔
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
5✔
13
        "/__mflux_svc__"
9,763✔
14
      end
15

16
      def self.uri
5✔
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}")
4,894✔
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
5✔
24
        Net::HTTP::Post.new(request_path)
4,869✔
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
5✔
31
        "tigerdata"
83✔
32
      end
33

34
      def self.default_xml_namespace_uri
5✔
35
        "http://tigerdata.princeton.edu"
83✔
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
5✔
41
        HttpConnection.instance.http_client
4,897✔
42
      end
43

44
      attr_reader :session_token
5✔
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)
5✔
51
        @http_client = http_client || self.class.find_or_create_http_client
4,897✔
52
        @file = file
4,897✔
53
        @session_token = session_token
4,897✔
54
      end
55

56
      # Resolves the HTTP request against the Mediaflux API
57
      # @return [Net::HTTP]
58
      def resolve
5✔
59
        start_time = ::Time.zone.now
4,875✔
60
        @http_response = @http_client.request self.class.uri, http_request
4,875✔
61
        log_elapsed(start_time)
4,874✔
62
        @http_response
4,874✔
63
      end
64

65
      # Determines whether or not the request has been resolved
66
      # @return [Boolean]
67
      def resolved?
5✔
68
        @http_response.present?
6,292✔
69
      end
70

71
      # Resolves the HTTP request, and returns the XML Document parsed from the response body
72
      # @return [Nokogiri::XML::Document]
73
      def response_xml
5✔
74
        resolve unless resolved?
6,290✔
75
        Rails.logger.debug(response_body)
6,290✔
76
        @response_xml ||= Nokogiri::XML.parse(response_body)
6,290✔
77
        Rails.logger.debug(@response_xml)
6,290✔
78
        if @response_xml.xpath("//message").text == "session is not valid"
6,290✔
79
          raise Mediaflux::SessionExpired, "Session expired for token #{session_token}"
4✔
80
        end
81

82
        @response_xml
6,286✔
83
      end
84

85
      # The response body of the mediaflux call
86
      def response_body
5✔
87
        @response_body ||= http_response.body
11,011✔
88
      end
89

90
      def error?
5✔
91
        response_error.present?
1,296✔
92
      end
93

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

105
      delegate :to_s, to: :response_xml
5✔
106

107
      def xml_payload( name: self.class.service)
5✔
108
        body = build_http_request_body(name: )
9,740✔
109
        xml_payload = body.to_xml
9,740✔
110
      end
111

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

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

129
      private
5✔
130

131
        def http_request
5✔
132
          @http_request ||= build_http_request(name: self.class.service, form_file: @file)
4,875✔
133
        end
134

135
        def http_response
5✔
136
          @http_response ||= resolve
4,714✔
137
        end
138

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

145
          Nokogiri::XML::Builder.new(namespace_inheritance: false) do |xml|
9,741✔
146
            xml.request do
9,741✔
147
              xml.service(**args) do
9,741✔
148
                yield xml if block_given?
9,741✔
149
              end
150
            end
151
          end
152
        end
153

154
        # rubocop:disable Metrics/MethodLength
155
        def build_http_request(name:, form_file: nil)
5✔
156
          request = self.class.build_post_request
4,869✔
157

158
          log_xml_request(xml_payload)
4,869✔
159
          if form_file.nil?
4,869✔
160
            request["Content-Type"] = "text/xml; charset=utf-8"
4,868✔
161
            request.body = xml_payload(name:)
4,868✔
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
4,869✔
172
        end
173
      # rubocop:enable Metrics/MethodLength
174

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

185
      # Logs as warning Mediaflux requests that take longer than 3 seconds.
186
      #
187
      # We could eventually also send to Honeybadger long requests but
188
      # let's wait until we have a benchmark of what is considered slow
189
      # in Mediaflux.
190
      def log_elapsed(start_time)
5✔
191
        elapsed_time = ::Time.zone.now - start_time
4,874✔
192
        timing_info = "#{format('%.2f', elapsed_time)} s"
4,874✔
193
        if elapsed_time > 3.0
4,874✔
194
          Rails.logger.warn "Slow Mediaflux request: #{self.class}, #{timing_info}"
×
195
        end
196
      end
197
    end
198
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