Class: Envoi::IngestService::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/envoi/ingest_service/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ Client

Returns a new instance of Client.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/envoi/ingest_service/client.rb', line 14

def initialize(args = {})
  initialize_logger(args)

  url = args[:url]
  app_name = args[:app_name]
  url = "#{url}?appname=#{app_name}"

  @default_source_bucket_name = args[:s3_source_bucket_name] || args[:source_bucket_name]
  @default_target_bucket_name = args[:s3_target_bucket_name] || args[:target_bucket_name]
  @default_allow_reimport = args.fetch(:allow_reimport, false)

  @uri = URI.parse(url)
  @headers = {
      'Accept' => 'application/json',
      'Content-Type' => 'application/json'
  }
  @http = Net::HTTP.new(uri.host, uri.port)
  @http.use_ssl = true

  @default_app_name = args[:app_name]

  @log_pretty_print_body = true
  @log_request_body = true
  @log_response_body = true
  @parse_response = true
end

Instance Attribute Details

#headersObject

Returns the value of attribute headers.



11
12
13
# File 'lib/envoi/ingest_service/client.rb', line 11

def headers
  @headers
end

#httpObject

Returns the value of attribute http.



11
12
13
# File 'lib/envoi/ingest_service/client.rb', line 11

def http
  @http
end

#log_pretty_print_bodyObject

Returns the value of attribute log_pretty_print_body.



12
13
14
# File 'lib/envoi/ingest_service/client.rb', line 12

def log_pretty_print_body
  @log_pretty_print_body
end

#log_request_bodyObject

Returns the value of attribute log_request_body.



12
13
14
# File 'lib/envoi/ingest_service/client.rb', line 12

def log_request_body
  @log_request_body
end

#log_response_bodyObject

Returns the value of attribute log_response_body.



12
13
14
# File 'lib/envoi/ingest_service/client.rb', line 12

def log_response_body
  @log_response_body
end

#loggerObject

Returns the value of attribute logger.



11
12
13
# File 'lib/envoi/ingest_service/client.rb', line 11

def logger
  @logger
end

#requestObject

Returns the value of attribute request.



11
12
13
# File 'lib/envoi/ingest_service/client.rb', line 11

def request
  @request
end

#responseObject

Returns the value of attribute response.



11
12
13
# File 'lib/envoi/ingest_service/client.rb', line 11

def response
  @response
end

#uriObject

Returns the value of attribute uri.



11
12
13
# File 'lib/envoi/ingest_service/client.rb', line 11

def uri
  @uri
end

Instance Method Details

#format_body_for_log_output(obj) ⇒ String

Formats a HTTPRequest or HTTPResponse body for log output.

Parameters:

  • obj (HTTPRequest|HTTPResponse)

Returns:

  • (String)


68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/envoi/ingest_service/client.rb', line 68

def format_body_for_log_output(obj)
  if obj.content_type == 'application/json'
    if @log_pretty_print_body
      _body = obj.body
      output = JSON.pretty_generate(JSON.parse(_body)) rescue _body
      return output
    else
      return obj.body
    end
  elsif obj.content_type == 'application/xml'
    return obj.body
  else
    return obj.body.inspect
  end
end

#initialize_logger(args = { }) ⇒ Object



41
42
43
# File 'lib/envoi/ingest_service/client.rb', line 41

def initialize_logger(args = { })
  @logger = args[:logger] || Logger.new(STDOUT)
end

#job_create(path, source_bucket_name = @default_source_bucket_name, target_bucket_name = @default_target_bucket_name, options = { }) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/envoi/ingest_service/client.rb', line 45

def job_create(path, source_bucket_name = @default_source_bucket_name, target_bucket_name = @default_target_bucket_name, options = { })
  allow_reimport = options.fetch(:allow_reimport, @default_allow_reimport)

  args_out = { path: path, sourceBucket: source_bucket_name, targetBucket: target_bucket_name }
  args_out[:allowReimport] = allow_reimport unless allow_reimport.nil?

  request = Net::HTTP::Post.new(uri.request_uri, headers)
  request.body = JSON.generate(args_out)
  send_request(request)
  response.code == '200'
end

#job_create_old(path) ⇒ Object



57
58
59
60
61
62
63
# File 'lib/envoi/ingest_service/client.rb', line 57

def job_create_old(path)
  args_out = [ path ]
  request = Net::HTTP::Post.new(uri.request_uri, headers)
  request.body = JSON.generate(args_out)
  send_request(request)
  response.code == '200'
end

#response_parsedObject



84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/envoi/ingest_service/client.rb', line 84

def response_parsed
  @response_parsed ||= begin
    response_body = response.respond_to?(:body) ? response.body : ''
    logger.debug { "Parsing Response." }

    case response.content_type
    when 'application/json'
      response_body.empty? ? response_body : JSON.parse(response_body) # rescue response
    else
      response_body
    end
  end
end

#send_request(request) ⇒ Object

Parameters:

  • request (HTTPRequest)


100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/envoi/ingest_service/client.rb', line 100

def send_request(request)
  @response_parsed = nil
  @request = request
  logger.debug { %(REQUEST: #{request.method} http#{http.use_ssl? ? 's' : ''}://#{http.address}:#{http.port}#{request.path} HEADERS: #{request.to_hash.inspect} #{log_request_body and request.request_body_permitted? ? "\n-- BODY BEGIN --\n#{format_body_for_log_output(request)}\n-- BODY END --" : ''}) }
  max_retries = 3
  retries = 0
  loop do
    should_retry = false

    @request_time_start = Time.now
    @response = http.request(request)
    @request_time_end = Time.now
    logger.debug { %(RESPONSE: #{response.inspect} HEADERS: #{response.to_hash.inspect} #{log_response_body and response.respond_to?(:body) ? "\n-- BODY BEGIN --\n#{format_body_for_log_output(response)}\n-- BODY END--" : ''}\nTook: #{@request_time_end - @request_time_start} seconds) }
    #logger.debug { "Parse Response? #{@parse_response}" }

    if retries < max_retries && ['504'].include?(@response.code)
      retries += 1
      should_retry = true
    end

    break unless should_retry
  end
  @parse_response ? response_parsed : response.body
end