Class: Embulk::Input::Zendesk::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/embulk/input/zendesk/client.rb

Constant Summary collapse

PARTIAL_RECORDS_SIZE =
50
PARTIAL_RECORDS_BYTE_SIZE =
50000
AVAILABLE_INCREMENTAL_EXPORT =
%w(tickets users organizations ticket_events ticket_metrics).freeze
UNAVAILABLE_INCREMENTAL_EXPORT =
%w(ticket_fields ticket_forms).freeze
AVAILABLE_TARGETS =
AVAILABLE_INCREMENTAL_EXPORT + UNAVAILABLE_INCREMENTAL_EXPORT

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Client

Returns a new instance of Client.



17
18
19
# File 'lib/embulk/input/zendesk/client.rb', line 17

def initialize(config)
  @config = config
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



9
10
11
# File 'lib/embulk/input/zendesk/client.rb', line 9

def config
  @config
end

Instance Method Details

#create_poolObject



34
35
36
37
38
39
40
41
# File 'lib/embulk/input/zendesk/client.rb', line 34

def create_pool
  Concurrent::ThreadPoolExecutor.new(
    min_threads: 10,
    max_threads: 100,
    max_queue: 10_000,
    fallback_policy: :caller_runs
  )
end

#fetch_subresource(record_id, base, target) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/embulk/input/zendesk/client.rb', line 116

def fetch_subresource(record_id, base, target)
  Embulk.logger.info "Fetching subresource #{target} of #{base}:#{record_id}"
  response = request("/api/v2/#{base}/#{record_id}/#{target}.json")
  return [] if response.status == 404

  begin
    data = JSON.parse(response.body)
    data[target]
  rescue => e
    raise Embulk::DataError.new(e)
  end
end

#httpclientObject



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/embulk/input/zendesk/client.rb', line 21

def httpclient
  # multi-threading + retry can create lot of instances, and each will keep connecting
  # re-using instance in multi threads can help to omit cleanup code
  @httpclient ||=
    begin
      clnt = HTTPClient.new
      clnt.connect_timeout = 240 # default:60 is not enough for huge data
      clnt.receive_timeout = 240 # better change default receive_timeout too
      # httpclient.debug_dev = STDOUT
      set_auth(clnt)
    end
end

#validate_app_marketplaceObject



72
73
74
75
76
77
78
79
# File 'lib/embulk/input/zendesk/client.rb', line 72

def validate_app_marketplace
  valid = config[:app_marketplace_integration_name] && config[:app_marketplace_org_id] && config[:app_marketplace_app_id]
  valid = valid || (!config[:app_marketplace_integration_name] && !config[:app_marketplace_org_id] && !config[:app_marketplace_app_id])

  unless valid
     raise Embulk::ConfigError.new("All of app_marketplace_integration_name, app_marketplace_org_id, app_marketplace_app_id are required to fill out for Apps Marketplace API header")
  end
end

#validate_configObject



43
44
45
46
47
# File 'lib/embulk/input/zendesk/client.rb', line 43

def validate_config
  validate_credentials
  validate_target
  validate_app_marketplace
end

#validate_credentialsObject



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/embulk/input/zendesk/client.rb', line 49

def validate_credentials
  valid = case config[:auth_method]
  when "basic"
    config[:username] && config[:password]
  when "token"
    config[:username] && config[:token]
  when "oauth"
    config[:access_token]
  else
    raise Embulk::ConfigError.new("Unknown auth_method (#{config[:auth_method]}). Should pick one from 'basic', 'token' or 'oauth'.")
  end

  unless valid
    raise Embulk::ConfigError.new("Missing required credentials for #{config[:auth_method]}")
  end
end

#validate_targetObject



66
67
68
69
70
# File 'lib/embulk/input/zendesk/client.rb', line 66

def validate_target
  unless AVAILABLE_TARGETS.include?(config[:target])
    raise Embulk::ConfigError.new("target: '#{config[:target]}' is not supported. Supported targets are #{AVAILABLE_TARGETS.join(", ")}.")
  end
end