Module: Trophonius::DatabaseRequest

Includes:
DebugPrinter
Defined in:
lib/connectors/database_request.rb

Class Method Summary collapse

Methods included from DebugPrinter

print_debug

Class Method Details

.auth_header_bearer(id) ⇒ String

Gets a valid auth header containing the access token

Returns:

  • (String)

    a valid auth header containing the access token



106
107
108
# File 'lib/connectors/database_request.rb', line 106

def self.auth_header_bearer(id)
  Trophonius.connection_manager.enqueue(id)
end

.get_layout_field_names(layout_name) ⇒ JSON

Retrieves the fieldnames of a layout

Returns:

  • (JSON)

    The fieldnames of a layout



122
123
124
125
126
127
128
# File 'lib/connectors/database_request.rb', line 122

def self.get_layout_field_names(layout_name)
  make_request("layouts/#{layout_name}", 'get', '{}')['response']['fieldMetaData'].map { |field| field['name'] }
rescue StandardError => e
  puts e
  puts e.backtrace
  Error.throw_error('1631')
end

.make_request(url_path, method, body, params = '', bypass_queue_with: '') ⇒ JSON

Crafts and runs a HTTP request of any type

Parameters:

  • url_path: (URI)

    the url to make the request to

  • method: (String)

    the type of HTTP request to make (i.e. get)

  • body: (JSONString)

    the body of the HTTP request

  • params: (String)

    optional parameters added to the request

  • bypass_queue_with: (String) (defaults to: '')

    optional way to bypass the ConnectionManager

Returns:

  • (JSON)

    parsed json of the response



28
29
30
31
32
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
# File 'lib/connectors/database_request.rb', line 28

def self.make_request(url_path, method, body, params = '', bypass_queue_with: '')
  ssl_verifyhost = Trophonius.config.local_network ? 0 : 2
  ssl_verifypeer = !Trophonius.config.local_network
  base_url = "http#{Trophonius.config.ssl == true ? 's' : ''}://#{Trophonius.config.host}/fmi/data/v1/databases/#{Trophonius.config.database}"
  uri = URI::RFC2396_Parser.new
  url =
    URI(
      uri.escape("#{base_url}/#{url_path}")
    )

  id = SecureRandom.uuid
  auth = bypass_queue_with.empty? ? auth_header_bearer(id) : bypass_queue_with

  request =
    Typhoeus::Request.new(
      url,
      method: method.to_sym,
      body: body,
      params: params,
      ssl_verifyhost: ssl_verifyhost,
      ssl_verifypeer: ssl_verifypeer,
      headers: { 'Content-Type' => 'application/json', Authorization: auth }
    )

  DebugPrinter.print_debug('USED URL', url)
  DebugPrinter.print_debug('SENT BODY', body)

  temp = request.run

  Trophonius.connection_manager.dequeue(id) if bypass_queue_with.empty?

  begin
    response_body = JSON.parse(temp.response_body)
    DebugPrinter.print_debug('RECEIVED BODY', JSON.pretty_generate(response_body))
    response_body
  rescue StandardError => e
    puts e
    puts e.backtrace
    Error.throw_error('1631')
  end
end

.retrieve_all(layout_name, sort) ⇒ JSON

Retrieves the 10000000 records from FileMaker

Returns:

  • (JSON)

    The first 10000000 records from FileMaker



142
143
144
145
146
147
148
149
# File 'lib/connectors/database_request.rb', line 142

def self.retrieve_all(layout_name, sort)
  path = "layouts/#{layout_name}/records?_limit=10000000#{
    Trophonius.config.count_result_script == '' ? '' : "&script=#{Trophonius.config.count_result_script}"
  }"
  path += "&_sort=#{sort_order}" if sort.present?
  path += "&script=#{Trophonius.config.count_result_script}" if Trophonius.config.count_result_script.present?
  make_request(path, 'get', '{}')
end

.retrieve_first(layout_name) ⇒ JSON

Retrieves the first record from FileMaker

Returns:

  • (JSON)

    The first record from FileMaker



114
115
116
# File 'lib/connectors/database_request.rb', line 114

def self.retrieve_first(layout_name)
  make_request("layouts/#{layout_name}/records?_limit=1", 'get', '{}')
end

.run_script(script, scriptparameter, layout_name) ⇒ JSON

Runs a FileMaker script

Returns:

  • (JSON)

    The script result from FileMaker



134
135
136
# File 'lib/connectors/database_request.rb', line 134

def self.run_script(script, scriptparameter, layout_name)
  make_request("/layouts/#{layout_name}/records?_limit=1&script=#{script}&script.param=#{scriptparameter}", 'get', '{}')
end

.upload_file_request(url_param, file) ⇒ JSON

Crafts and runs a HTTP request for uploading a file to a container

Parameters:

  • urlparam: (URI)

    the url to make the request to

  • auth: (String)

    the authentication required for the request

  • file: (Tempfile or File)

    file to upload

Returns:

  • (JSON)

    parsed json of the response



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/connectors/database_request.rb', line 80

def self.upload_file_request(url_param, file)
  base_url = "http#{Trophonius.config.ssl == true ? 's' : ''}://#{Trophonius.config.host}/fmi/data/v1/databases/#{Trophonius.config.database}"
  url = URI("#{base_url}/#{url_param}")

  https = Net::HTTP.new(url.host, url.port)
  https.use_ssl = true

  id = SecureRandom.uuid
  request = Net::HTTP::Post.new(url)
  request['Authorization'] = auth_header_bearer(id)
  request['Content-Type'] = 'multipart/form-data;'
  form_data = [['upload', file]]
  request.set_form form_data, 'multipart/form-data'
  response = https.request(request)
  Trophonius.connection_manager.dequeue(id)
  begin
    JSON.parse(response.read_body)
  rescue StandardError
    Error.throw_error('1631')
  end
end