mirror of
https://github.com/rickbarrette/redmine_qbo.git
synced 2026-04-02 08:21:57 -04:00
created a shared helper for performing authenticated request.
This commit is contained in:
@@ -31,13 +31,7 @@ class ServiceBase
|
|||||||
return build_qbo_remote unless @local.present?
|
return build_qbo_remote unless @local.present?
|
||||||
return build_qbo_remote unless @local.id
|
return build_qbo_remote unless @local.id
|
||||||
log "Fetching details for #{@entity} ##{@local.id} from QBO..."
|
log "Fetching details for #{@entity} ##{@local.id} from QBO..."
|
||||||
qbo = QboConnectionService.current!
|
with_qbo_service do |service|
|
||||||
qbo.perform_authenticated_request do |access_token|
|
|
||||||
service_class = "Quickbooks::Service::#{@entity}".constantize
|
|
||||||
service = service_class.new(
|
|
||||||
company_id: qbo.realm_id,
|
|
||||||
access_token: access_token
|
|
||||||
)
|
|
||||||
service.fetch_by_id(@local.id)
|
service.fetch_by_id(@local.id)
|
||||||
end
|
end
|
||||||
rescue => e
|
rescue => e
|
||||||
@@ -51,13 +45,7 @@ class ServiceBase
|
|||||||
# If the push is successful, it returns the remote record; otherwise, it logs the error and returns false.
|
# If the push is successful, it returns the remote record; otherwise, it logs the error and returns false.
|
||||||
def push
|
def push
|
||||||
log "Pushing #{@entity} ##{@local.id} to QBO..."
|
log "Pushing #{@entity} ##{@local.id} to QBO..."
|
||||||
|
remote = with_qbo_service do |service|
|
||||||
remote = @qbo.perform_authenticated_request do |access_token|
|
|
||||||
service_class = "Quickbooks::Service::#{@entity}".constantize
|
|
||||||
service = service_class.new(
|
|
||||||
company_id: @qbo.realm_id,
|
|
||||||
access_token: access_token
|
|
||||||
)
|
|
||||||
if @local.id.present?
|
if @local.id.present?
|
||||||
log "Updating #{@entity}"
|
log "Updating #{@entity}"
|
||||||
service.update(@local.details)
|
service.update(@local.details)
|
||||||
@@ -66,17 +54,29 @@ class ServiceBase
|
|||||||
service.create(@local.details)
|
service.create(@local.details)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@local.id = remote.id unless @local.persisted?
|
@local.id = remote.id unless @local.persisted?
|
||||||
log "Push for remote ##{@local.id} completed."
|
log "Push for remote ##{@local.id} completed."
|
||||||
return @local
|
@local
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
|
# 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
|
||||||
|
|
||||||
# Log messages with the entity type for better traceability
|
# Log messages with the entity type for better traceability
|
||||||
def log(msg)
|
def log(msg)
|
||||||
Rails.logger.info "[#{@entity}Service] #{msg}"
|
Rails.logger.info "[#{@entity}Service] #{msg}"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Dynamically get the Quickbooks Service Class
|
||||||
|
def service_class
|
||||||
|
@service_class ||= "Quickbooks::Service::#{@entity}".constantize
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
Reference in New Issue
Block a user