Class: Mediaflux::Request
- Inherits:
-
Object
- Object
- Mediaflux::Request
- Defined in:
- app/models/mediaflux/request.rb
Direct Known Subclasses
ActorSelfDescribeRequest, AssetCreateRequest, AssetDestroyRequest, AssetExistRequest, AssetMetadataRequest, IteratorDestroyRequest, IteratorRequest, LogonRequest, LogoutRequest, NamespaceDescribeRequest, NamespaceDestroyRequest, NamespaceListRequest, ProjectCreateServiceRequest, ProjectListRequest, ProjectQuotaRequest, ProjectReport, ProjectUpdateRequest, ProjectUserAddRequest, QueryRequest, SchemaFetchRequest, ScriptFileListInitRequest, ScriptFileListIterateRequest, ScriptMakeExecutableRequest, ScriptUploadRequest, ServiceExecuteRequest, StoreListRequest, StringReverse, TestAssetCreateRequest, TigerdataDescribeRequest, VersionRequest
Instance Attribute Summary collapse
-
#session_token ⇒ Object
readonly
Returns the value of attribute session_token.
Class Method Summary collapse
-
.build_post_request ⇒ Net::HTTP::Post
Constructs a new HTTP POST request for usage with the Mediaflux API.
-
.default_xml_namespace ⇒ String
The default XML namespace which should be used for building the XML Document transmitted in the body of the HTTP request.
- .default_xml_namespace_uri ⇒ Object
-
.find_or_create_http_client ⇒ Object
Constructs and memoizes a new instance of the Net::HTTP::Persistent object at the level of the Class.
-
.format_date_for_mediaflux(iso8601_date) ⇒ Object
This method is used for transforming iso8601 dates to dates that MediaFlux likes Take a string like “2024-02-26T10:33:11-05:00” and convert this string to “22-FEB-2024 13:57:19”.
-
.new_project_request_path ⇒ String
The default request URL path for the Mediaflux API.
-
.service ⇒ Object
As this is an abstract class, this should be overridden to specify the Mediaflux API service.
- .uri ⇒ Object
Instance Method Summary collapse
- #error? ⇒ Boolean
-
#initialize(file: nil, session_token: nil, http_client: nil) ⇒ Request
constructor
Constructor.
-
#resolve ⇒ Net::HTTP
Resolves the HTTP request against the Mediaflux API.
-
#resolved? ⇒ Boolean
Determines whether or not the request has been resolved.
-
#response_body ⇒ Object
The response body of the mediaflux call.
- #response_error ⇒ Object
-
#response_xml ⇒ Nokogiri::XML::Document
Resolves the HTTP request, and returns the XML Document parsed from the response body.
- #xml_payload(name: self.class.service) ⇒ Object
-
#xtoshell_xml(name: self.class.service) ⇒ String
The output of this routine can be passed to xtoshell in aterm.
Constructor Details
#initialize(file: nil, session_token: nil, http_client: nil) ⇒ Request
Constructor
53 54 55 56 57 |
# File 'app/models/mediaflux/request.rb', line 53 def initialize(file: nil, session_token: nil, http_client: nil) @http_client = http_client || self.class.find_or_create_http_client @file = file @session_token = session_token end |
Instance Attribute Details
#session_token ⇒ Object (readonly)
Returns the value of attribute session_token.
47 48 49 |
# File 'app/models/mediaflux/request.rb', line 47 def session_token @session_token end |
Class Method Details
.build_post_request ⇒ Net::HTTP::Post
Constructs a new HTTP POST request for usage with the Mediaflux API
26 27 28 |
# File 'app/models/mediaflux/request.rb', line 26 def self.build_post_request Net::HTTP::Post.new(new_project_request_path) end |
.default_xml_namespace ⇒ String
The default XML namespace which should be used for building the XML
Document transmitted in the body of the HTTP request
33 34 35 |
# File 'app/models/mediaflux/request.rb', line 33 def self.default_xml_namespace "tigerdata" end |
.default_xml_namespace_uri ⇒ Object
37 38 39 |
# File 'app/models/mediaflux/request.rb', line 37 def self.default_xml_namespace_uri "http://tigerdata.princeton.edu" end |
.find_or_create_http_client ⇒ Object
Constructs and memoizes a new instance of the Net::HTTP::Persistent object at the level of the Class
43 44 45 |
# File 'app/models/mediaflux/request.rb', line 43 def self.find_or_create_http_client HttpConnection.instance.http_client end |
.format_date_for_mediaflux(iso8601_date) ⇒ Object
This method is used for transforming iso8601 dates to dates that MediaFlux likes Take a string like “2024-02-26T10:33:11-05:00” and convert this string to “22-FEB-2024 13:57:19”
129 130 131 132 |
# File 'app/models/mediaflux/request.rb', line 129 def self.format_date_for_mediaflux(iso8601_date) return if iso8601_date.nil? Time.format_date_for_mediaflux(iso8601_date) end |
.new_project_request_path ⇒ String
The default request URL path for the Mediaflux API
15 16 17 |
# File 'app/models/mediaflux/request.rb', line 15 def self.new_project_request_path "/__mflux_svc__" end |
.service ⇒ Object
As this is an abstract class, this should be overridden to specify the Mediaflux API service
9 10 11 |
# File 'app/models/mediaflux/request.rb', line 9 def self.service raise(NotImplementedError, "#{self} is an abstract class, please override #{self}.service") end |
.uri ⇒ Object
19 20 21 22 |
# File 'app/models/mediaflux/request.rb', line 19 def self.uri # Setting the URI to the Ansible build or Docker build of mediaflux depending on the environment URI("#{Connection.transport}://#{Connection.host}:#{Connection.port}#{new_project_request_path}") end |
Instance Method Details
#error? ⇒ Boolean
96 97 98 |
# File 'app/models/mediaflux/request.rb', line 96 def error? response_error.present? end |
#resolve ⇒ Net::HTTP
Resolves the HTTP request against the Mediaflux API
61 62 63 64 65 66 67 68 69 |
# File 'app/models/mediaflux/request.rb', line 61 def resolve start_time = ::Time.zone.now @http_response = @http_client.request self.class.uri, http_request log_elapsed(start_time) if response_error.present? Rails.logger.error "Mediaflux error: #{response_error[:title]}, #{response_error[:message]}" end @http_response end |
#resolved? ⇒ Boolean
Determines whether or not the request has been resolved
73 74 75 |
# File 'app/models/mediaflux/request.rb', line 73 def resolved? @http_response.present? end |
#response_body ⇒ Object
The response body of the mediaflux call
92 93 94 |
# File 'app/models/mediaflux/request.rb', line 92 def response_body @response_body ||= http_response.body end |
#response_error ⇒ Object
100 101 102 103 104 105 106 107 108 |
# File 'app/models/mediaflux/request.rb', line 100 def response_error xml = response_xml return nil if xml.xpath("/response/reply/error").count == 0 error = { title: xml.xpath("/response/reply/error").text, message: xml.xpath("/response/reply/message").text } error end |
#response_xml ⇒ Nokogiri::XML::Document
Resolves the HTTP request, and returns the XML Document parsed from the response body
79 80 81 82 83 84 85 86 87 88 89 |
# File 'app/models/mediaflux/request.rb', line 79 def response_xml resolve unless resolved? Rails.logger.debug(response_body) @response_xml ||= Nokogiri::XML.parse(response_body) Rails.logger.debug(@response_xml) if @response_xml.xpath("//message").text == "session is not valid" raise Mediaflux::SessionExpired, "Session expired for token #{session_token}" end @response_xml end |
#xml_payload(name: self.class.service) ⇒ Object
112 113 114 115 |
# File 'app/models/mediaflux/request.rb', line 112 def xml_payload( name: self.class.service) body = build_http_request_body(name: ) xml_payload = body.to_xml end |
#xtoshell_xml(name: self.class.service) ⇒ String
The output of this routine can be passed to xtoshell in aterm. The output of which can be sent to service.execute
120 121 122 123 124 125 |
# File 'app/models/mediaflux/request.rb', line 120 def xtoshell_xml( name: self.class.service) xml_builder = build_http_request_body(name: ) xml_builder.doc.xpath("//request/service/@session").remove xml = xml_builder.to_xml(:save_with => Nokogiri::XML::Node::SaveOptions::AS_XML | Nokogiri::XML::Node::SaveOptions::NO_DECLARATION) xml.strip.gsub("\"","'").gsub("<args>","").gsub("</args>","") end |