lib/embulk/input/zendesk/client.rb in embulk-input-zendesk-0.1.2 vs lib/embulk/input/zendesk/client.rb in embulk-input-zendesk-0.1.3
- old
+ new
@@ -5,15 +5,13 @@
module Zendesk
class Client
attr_reader :config
PARTIAL_RECORDS_SIZE = 50
- AVAILABLE_TARGETS = %w(
- tickets ticket_events users organizations
- ticket_fields ticket_forms
- ).freeze
- AVAILABLE_INCREMENTAL_EXPORT = AVAILABLE_TARGETS - %w(ticket_fields ticket_forms)
+ AVAILABLE_INCREMENTAL_EXPORT = %w(tickets users organizations ticket_events).freeze
+ UNAVAILABLE_INCREMENTAL_EXPORT = %w(ticket_fields ticket_forms ticket_metrics).freeze
+ AVAILABLE_TARGETS = AVAILABLE_INCREMENTAL_EXPORT + UNAVAILABLE_INCREMENTAL_EXPORT
def initialize(config)
@config = config
end
@@ -45,40 +43,51 @@
end
end
def validate_target
unless AVAILABLE_TARGETS.include?(config[:target])
- raise Embulk::ConfigError.new("target: '#{config[:target]}' is not supported.")
+ raise Embulk::ConfigError.new("target: '#{config[:target]}' is not supported. Supported targets are #{AVAILABLE_TARGETS.join(", ")}.")
end
end
+ # they have both Incremental API and non-incremental API
%w(tickets users organizations).each do |target|
define_method(target) do |partial = true, start_time = 0, &block|
if partial
export("/api/v2/#{target}.json", target, PARTIAL_RECORDS_SIZE, &block) # Ignore start_time
else
incremental_export("/api/v2/incremental/#{target}.json", target, start_time, [], &block)
end
end
end
- def ticket_events(partial = true, start_time = 0, &block)
- # NOTE: ticket_events only have incremental export API
- path = "/api/v2/incremental/ticket_events"
- incremental_export(path, "ticket_events", start_time, [], &block)
+ # they have incremental API only
+ %w(ticket_events).each do |target|
+ define_method(target) do |partial = true, start_time = 0, &block|
+ path = "/api/v2/incremental/#{target}"
+ incremental_export(path, target, start_time, [], &block)
+ end
end
- def ticket_fields(partial = true, start_time = 0, &block)
- # NOTE: ticket_fields only have export API (not incremental)
- path = "/api/v2/ticket_fields.json"
- export(path, "ticket_fields", 1000, &block)
+ # they have non-incremental API only
+ UNAVAILABLE_INCREMENTAL_EXPORT.each do |target|
+ define_method(target) do |partial = true, start_time = 0, &block|
+ path = "/api/v2/#{target}.json"
+ export(path, target, partial ? PARTIAL_RECORDS_SIZE : 1000, &block)
+ end
end
- def ticket_forms(partial = true, start_time = 0, &block)
- # NOTE: ticket_forms only have export API (not incremental)
- path = "/api/v2/ticket_forms.json"
- export(path, "ticket_forms", 1000, &block)
+ def fetch_subresource(record_id, base, target)
+ 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
private
def export(path, key, per_page, &block)
@@ -161,15 +170,17 @@
def request(path, query = {})
u = URI.parse(config[:login_url])
u.path = path
retryer.with_retry do
+ Embulk.logger.debug "Fetching #{u.to_s}"
response = httpclient.get(u.to_s, query, follow_redirect: true)
# https://developer.zendesk.com/rest_api/docs/core/introduction#response-format
status_code = response.status
case status_code
- when 200
+ when 200, 404
+ # 404 would be returned e.g. ticket comments are empty (on fetch_subresource method)
response
when 400, 401
raise Embulk::ConfigError.new("[#{status_code}] #{response.body}")
when 409
raise "[#{status_code}] temporally failure."