Class: GoodData::Bricks::GoodDataMiddleware

Inherits:
Middleware show all
Defined in:
lib/gooddata/bricks/middleware/gooddata_middleware.rb

Overview

Connects to platform and enriches parameters with GoodData::Client

Constant Summary collapse

DEFAULT_PROTOCOL =
'https'
DEFAULT_HOSTNAME =
'secure.gooddata.com'

Instance Attribute Summary

Attributes inherited from Middleware

#app

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Middleware

#initialize, #load_defaults

Methods included from Utils

#returning

Constructor Details

This class inherits a constructor from GoodData::Bricks::Middleware

Class Method Details

.connect(server, verify_ssl, username, password, sst_token, execution_id) ⇒ Object

rubocop:disable Metrics/ParameterLists



110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/gooddata/bricks/middleware/gooddata_middleware.rb', line 110

def connect(server, verify_ssl, username, password, sst_token, execution_id) # rubocop:disable Metrics/ParameterLists
  if username.nil? || password.nil?
    GoodData.logger.info("Connecting with SST to server #{server}")
    raise 'SST (SuperSecureToken) not present in params' if sst_token.nil?
    conn = GoodData.connect(sst_token: sst_token, server: server, verify_ssl: verify_ssl, execution_id: execution_id)
  else
    GoodData.logger.info("Connecting as #{username} to server #{server}")
    conn = GoodData.connect(username, password, server: server, verify_ssl: verify_ssl, execution_id: execution_id)
  end
  conn.stats_on

  conn
end

Instance Method Details

#call(params) ⇒ Object



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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/gooddata/bricks/middleware/gooddata_middleware.rb', line 28

def call(params)
  # Generate brick execution id
  execution_id = GoodData.gd_logger.execution_id

  # Convert possible jruby hash to plain hash
  params = params.to_hash

  # Transform keys
  params = GoodData::Helpers.stringify_keys(params)
  # params = GoodData::Helpers.symbolize_keys(params)

  # Set parallelism
  max_concurrency = params['max_concurrency'] || params['MAX_CONCURRENCY']
  if max_concurrency && max_concurrency.to_i > 0
    $pmap_default_thread_count = max_concurrency.to_i # rubocop:disable GlobalVars
  end

  # Connect Client
  protocol = params['CLIENT_GDC_PROTOCOL'] || params['GDC_PROTOCOL'] || DEFAULT_PROTOCOL
  hostname = params['CLIENT_GDC_HOSTNAME'] || params['GDC_HOSTNAME'] || DEFAULT_HOSTNAME
  server = "#{protocol}://#{hostname}"
  client = GoodDataMiddleware.connect(
    server,
    params['GDC_VERIFY_SSL'].to_b,
    params['GDC_USERNAME'],
    params['GDC_PASSWORD'],
    params['GDC_SST'],
    execution_id
  )

  opts = params['development_client']
  if opts
    if opts['server']
      server = opts['server']
    else
      protocol = opts['protocol'] || DEFAULT_PROTOCOL
      hostname = opts['hostname'] || DEFAULT_HOSTNAME
      server = "#{protocol}://#{hostname}"
    end

    development_client = GoodDataMiddleware.connect(
      server,
      opts['verify_ssl'].to_b,
      opts['username'] || opts['login'] || opts['email'],
      opts['password'],
      opts['sst'],
      execution_id
    )
  else
    development_client = client
  end

  new_params = {
    'GDC_GD_CLIENT' => client,
    'development_client' => development_client
  }

  # collect parent project if deployed as process
  if params['GDC_PROJECT_ID']
    new_params['gdc_project'] = GoodData.project = client.projects(params['GDC_PROJECT_ID'])
  end

  returning_value = @app.call(params.merge(new_params))

  # Try to disconnect client
  begin
    client.disconnect
  rescue StandardError => e
    GoodData.logger.warn("Tried to disconnect client. Was unsuccessful. Proceeding anyway. Error: #{e}")
  end

  # Try to disconnect development_client
  begin
    development_client.disconnect if development_client != client
  rescue StandardError => e
    GoodData.logger.warn("Tried to disconnect development_client. Was unsuccessful. Proceeding anyway. Error: #{e}")
  end

  returning_value
end