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

pulibrary / tigerdata-app / 62b73c47-d7c3-42d1-89ea-d5a910c801c1

29 Oct 2025 08:18PM UTC coverage: 87.65% (-3.7%) from 91.355%
62b73c47-d7c3-42d1-89ea-d5a910c801c1

push

circleci

web-flow
Upgrade to mediaflux_dev v0.17.0 (#2118)

Co-authored-by: Hector Correa <hector_correa@princeton.edu>

1 of 1 new or added line in 1 file covered. (100.0%)

941 existing lines in 40 files now uncovered.

2697 of 3077 relevant lines covered (87.65%)

426.44 hits per line

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

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

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

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

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

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

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

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

65
      # Determines whether or not the request has been resolved
66
      # @return [Boolean]
67
      def resolved?
4✔
68
        @http_response.present?
4,841✔
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
4✔
74
        resolve unless resolved?
4,839✔
75
        Rails.logger.debug(response_body)
4,839✔
76
        @response_xml ||= Nokogiri::XML.parse(response_body)
4,839✔
77
        Rails.logger.debug(@response_xml)
4,839✔
78
        if @response_xml.xpath("//message").text == "session is not valid"
4,839✔
UNCOV
79
          raise Mediaflux::SessionExpired, "Session expired for token #{session_token}"
4✔
80
        end
81

82
        @response_xml
4,835✔
83
      end
84

85
      # The response body of the mediaflux call
86
      def response_body
4✔
87
        @response_body ||= http_response.body
8,457✔
88
      end
89

90
      def error?
4✔
91
        response_error.present?
1,037✔
92
      end
93

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

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

107
      def xml_payload( name: self.class.service)
4✔
108
        body = build_http_request_body(name: )
7,460✔
109
        xml_payload = body.to_xml
7,460✔
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)
4✔
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)
4✔
125
        return if iso8601_date.nil?
×
126
        Time.format_date_for_mediaflux(iso8601_date)
×
127
      end
128

129
      private
4✔
130

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

135
        def http_response
4✔
136
          @http_response ||= resolve
3,611✔
137
        end
138

139
        def build_http_request_body(name:)
4✔
140
          args = { name: name }
7,461✔
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?
7,461✔
144

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

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

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

171
          request
3,729✔
172
        end
173
      # rubocop:enable Metrics/MethodLength
174

175
      def log_xml_request(xml_payload)
4✔
176
        password_element = xml_payload.match(/\<password\>.*\<\/password\>/)
3,729✔
177
        if password_element.nil?
3,729✔
178
          Rails.logger.debug(xml_payload)
1,999✔
179
        else
180
          # Don't log the password
181
          Rails.logger.debug(xml_payload.gsub(password_element.to_s, "<password>***</password>"))
1,730✔
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)
4✔
191
        elapsed_time = ::Time.zone.now - start_time
3,732✔
192
        timing_info = "#{format('%.2f', elapsed_time)} s"
3,732✔
193
        if elapsed_time > 3.0
3,732✔
194
          Rails.logger.warn "Slow Mediaflux request: #{self.class}, #{timing_info}"
2✔
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