Class: FuryDumper::Api

Inherits:
Object
  • Object
show all
Defined in:
lib/fury_dumper/api.rb

Constant Summary collapse

HEALTH_URL =
'fury_dumper/health'

Instance Method Summary collapse

Constructor Details

#initialize(ms_name) ⇒ Api

Returns a new instance of Api.

Parameters:

  • ms_name (String)
    • name of microservice for request



8
9
10
11
12
# File 'lib/fury_dumper/api.rb', line 8

def initialize(ms_name)
  @ms_name      = ms_name
  @http_client  = init_http_client(ms_name)
  @ms_config    = FuryDumper::Config.fetch_service_config(@ms_name)
end

Instance Method Details

#check_ms_healthObject



44
45
46
47
48
49
50
51
# File 'lib/fury_dumper/api.rb', line 44

def check_ms_health
  response = @http_client.get('fury_dumper/health')

  ok_responce?(response)
rescue StandardError => e
  notify_error(e)
  false
end

#default_headerObject



66
67
68
69
70
71
72
# File 'lib/fury_dumper/api.rb', line 66

def default_header
  {
    'Content-Type' => 'application/x-www-form-urlencoded, charset=utf-8',
    'Authorization' => 'Bearer 40637702df32be88886c7083c4fdb075',
    'Accept' => 'application/x-protobuf,application/json'
  }
end

#init_http_client(ms_name) ⇒ Object



58
59
60
61
62
63
64
# File 'lib/fury_dumper/api.rb', line 58

def init_http_client(ms_name)
  base_url = Rails.application.class.parent_name.constantize.config.deep_symbolize_keys[ms_name.to_sym][:endpoint]
  HTTPClient.new(base_url: base_url, default_header: default_header)
rescue StandardError => e
  notify_error(e)
  nil
end

#notify_error(error) ⇒ Object



53
54
55
56
# File 'lib/fury_dumper/api.rb', line 53

def notify_error(error)
  context = { error: error }
  BugTracker.notify error_message: "Не смогли получить данные из #{@ms_name}", context: context
end

#ok_responce?(response) ⇒ Boolean

Returns:

  • (Boolean)


74
75
76
77
78
79
80
# File 'lib/fury_dumper/api.rb', line 74

def ok_responce?(response)
  unless response.status == 200
    raise "[#{@ms_name}] invalid response status => #{response.status}/#{response.body.inspect}"
  end

  true
end

#send_request(ms_model, ms_field_name, field_values) ⇒ Object

Send dumping request to microservice

Examples:

FuryDumper::Dumper.dump(“users”, “id”, [1,2,3])

Parameters:

  • ms_model (String)
    • name of table to start dumping

  • ms_field_name (String)
    • field in table for find records for dump

  • field_values (Array)
    • values of access_way for dumping



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/fury_dumper/api.rb', line 21

def send_request(ms_model, ms_field_name, field_values)
  # Check gem is included to microservice(ms)
  return unless check_ms_health

  message = {
    model_name: ms_model,
    field_name: ms_field_name,
    field_values: field_values,
    password: Encrypter.encrypt(@ms_config['password']),
    host: @ms_config['host'],
    port: @ms_config['port'],
    user: @ms_config['user'],
    database: @ms_config['database']
  }.to_json

  response = @http_client.post('fury_dumper/dump', message)

  ok_responce?(response)
rescue StandardError => e
  notify_error(e)
  nil
end