mirror of
https://github.com/rickbarrette/redmine_qbo.git
synced 2026-04-03 16:51:58 -04:00
created a shared helper for authenticated requests.
This commit is contained in:
@@ -32,17 +32,11 @@ class SyncServiceBase
|
|||||||
# Sync all entities, or only those updated since the last sync
|
# Sync all entities, or only those updated since the last sync
|
||||||
def sync(full_sync: false)
|
def sync(full_sync: false)
|
||||||
log "Starting #{full_sync ? 'full' : 'incremental'} #{@entity.name} sync with page size of: #{@page_size}"
|
log "Starting #{full_sync ? 'full' : 'incremental'} #{@entity.name} sync with page size of: #{@page_size}"
|
||||||
|
with_qbo_service do |service|
|
||||||
@qbo.perform_authenticated_request do |access_token|
|
|
||||||
service_class = "Quickbooks::Service::#{@entity.name}".constantize
|
|
||||||
service = service_class.new(company_id: @qbo.realm_id, access_token: access_token)
|
|
||||||
|
|
||||||
query = build_query(full_sync)
|
query = build_query(full_sync)
|
||||||
|
|
||||||
service.query_in_batches(query, per_page: @page_size) do |batch|
|
service.query_in_batches(query, per_page: @page_size) do |batch|
|
||||||
entries = Array(batch)
|
entries = Array(batch)
|
||||||
log "Processing batch of #{entries.size} #{@entity.name}"
|
log "Processing batch of #{entries.size} #{@entity.name}"
|
||||||
|
|
||||||
entries.each do |remote|
|
entries.each do |remote|
|
||||||
persist(remote)
|
persist(remote)
|
||||||
end
|
end
|
||||||
@@ -55,10 +49,7 @@ class SyncServiceBase
|
|||||||
# Sync a single entity by its QBO ID (webhook usage)
|
# Sync a single entity by its QBO ID (webhook usage)
|
||||||
def sync_by_id(id)
|
def sync_by_id(id)
|
||||||
log "Syncing #{@entity.name} with ID #{id}"
|
log "Syncing #{@entity.name} with ID #{id}"
|
||||||
|
with_qbo_service do |service|
|
||||||
@qbo.perform_authenticated_request do |access_token|
|
|
||||||
service_class = "Quickbooks::Service::#{@entity.name}".constantize
|
|
||||||
service = service_class.new(company_id: @qbo.realm_id, access_token: access_token)
|
|
||||||
remote = service.fetch_by_id(id)
|
remote = service.fetch_by_id(id)
|
||||||
persist(remote)
|
persist(remote)
|
||||||
end
|
end
|
||||||
@@ -124,4 +115,17 @@ class SyncServiceBase
|
|||||||
def process_attributes(local, remote)
|
def process_attributes(local, remote)
|
||||||
raise NotImplementedError, "Subclasses must implement process_attributes"
|
raise NotImplementedError, "Subclasses must implement process_attributes"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Dynamically get the Quickbooks Service Class
|
||||||
|
def service_class
|
||||||
|
@service_class ||= "Quickbooks::Service::#{@entity}".constantize
|
||||||
|
end
|
||||||
|
|
||||||
|
# Performs authenticaed requests with QBO service
|
||||||
|
def with_qbo_service
|
||||||
|
@qbo.perform_authenticated_request do |access_token|
|
||||||
|
service = service_class.new( company_id: @qbo.realm_id, access_token: access_token )
|
||||||
|
yield service
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
Reference in New Issue
Block a user