app/models/maestrano/connector/rails/concerns/entity.rb in maestrano-connector-rails-0.3.13 vs app/models/maestrano/connector/rails/concerns/entity.rb in maestrano-connector-rails-0.4.0
- old
+ new
@@ -115,15 +115,15 @@
def references
[]
end
def can_read_connec?
- true
+ can_write_external?
end
def can_read_external?
- true
+ can_write_connec?
end
def can_write_connec?
true
end
@@ -189,18 +189,18 @@
filter = "updated_at gt '#{last_synchronization.updated_at.iso8601}'"
filter += " and #{opts[:$filter]}" if opts[:$filter]
query_params[:$filter] = filter
end
response = client.get("/#{self.class.normalized_connec_entity_name}?#{query_params.to_query}")
- raise "No data received from Connec! when trying to fetch #{self.class.connec_entity_name.pluralize}" unless response
+ raise "No data received from Connec! when trying to fetch #{self.class.normalized_connec_entity_name}" unless response
response_hash = JSON.parse(response.body)
Maestrano::Connector::Rails::ConnectorLogger.log('debug', organization, "received first page entity=#{self.class.connec_entity_name}, response=#{response.body}")
if response_hash["#{self.class.normalized_connec_entity_name}"]
entities << response_hash["#{self.class.normalized_connec_entity_name}"]
else
- raise "Received unrecognized Connec! data when trying to fetch #{self.class.connec_entity_name.pluralize}"
+ raise "Received unrecognized Connec! data when trying to fetch #{self.class.normalized_connec_entity_name}"
end
# Fetch subsequent pages
while response_hash['pagination'] && response_hash['pagination']['next']
# ugly way to convert https://api-connec/api/v2/group_id/organizations?next_page_params to /organizations?next_page_params
@@ -229,45 +229,56 @@
def push_entities_to_connec_to(connec_client, mapped_external_entities_with_idmaps, connec_entity_name, organization)
return unless self.class.can_write_connec?
Maestrano::Connector::Rails::ConnectorLogger.log('info', organization, "Sending #{Maestrano::Connector::Rails::External.external_name} #{self.class.external_entity_name.pluralize} to Connec! #{connec_entity_name.pluralize}")
- mapped_external_entities_with_idmaps.each do |mapped_external_entity_with_idmap|
- external_entity = mapped_external_entity_with_idmap[:entity]
- idmap = mapped_external_entity_with_idmap[:idmap]
-
- begin
+
+ request_per_call = 100
+ start = 0
+ while start < mapped_external_entities_with_idmaps.size
+ # Prepare batch request
+ batch_entities = mapped_external_entities_with_idmaps.slice(start, request_per_call)
+ batch_request = {sequential: true, ops: []}
+ batch_entities.each do |mapped_external_entity_with_idmap|
+ external_entity = mapped_external_entity_with_idmap[:entity]
+ idmap = mapped_external_entity_with_idmap[:idmap]
if idmap.connec_id.blank?
- connec_entity = create_connec_entity(connec_client, external_entity, self.class.normalize_connec_entity_name(connec_entity_name), organization)
- idmap.update_attributes(connec_id: connec_entity['id'], last_push_to_connec: Time.now, message: nil)
+ batch_request[:ops] << batch_op('post', external_entity, nil, self.class.normalize_connec_entity_name(connec_entity_name), organization)
else
next unless self.class.can_update_connec?
- connec_entity = update_connec_entity(connec_client, external_entity, idmap.connec_id, self.class.normalize_connec_entity_name(connec_entity_name), organization)
- idmap.update_attributes(last_push_to_connec: Time.now, message: nil)
+ batch_request[:ops] << batch_op('put', external_entity, idmap.connec_id, self.class.normalize_connec_entity_name(connec_entity_name), organization)
end
- rescue => e
- # Store Connec! error if any
- Maestrano::Connector::Rails::ConnectorLogger.log('error', organization, "Error while pushing to Connec!: #{e}")
- idmap.update_attributes(message: e.message)
end
+
+ # Batch call
+ Maestrano::Connector::Rails::ConnectorLogger.log('info', organization, "Sending batch request to Connec! for #{self.class.normalize_connec_entity_name(connec_entity_name)}. Batch_request_size: #{batch_request[:ops].size}. Call_number: #{(start/request_per_call) + 1}")
+ response = connec_client.post('/batch', batch_request)
+ response = JSON.parse(response.body)
+
+ # Parse barch response
+ response['results'].each_with_index do |result, index|
+ if result['status'] == 200
+ batch_entities[index][:idmap].update_attributes(last_push_to_connec: Time.now, message: nil)
+ elsif result['status'] == 201
+ batch_entities[index][:idmap].update_attributes(connec_id: result['body'][self.class.normalize_connec_entity_name(connec_entity_name)]['id'], last_push_to_connec: Time.now, message: nil)
+ else
+ Maestrano::Connector::Rails::ConnectorLogger.log('error', organization, "Error while pushing to Connec!: #{result['body']}")
+ batch_entities[index][:idmap].update_attributes(message: result['body'])
+ end
+ end
+ start += request_per_call
end
end
- def create_connec_entity(connec_client, mapped_external_entity, connec_entity_name, organization)
- Maestrano::Connector::Rails::ConnectorLogger.log('info', organization, "Sending create #{connec_entity_name}: #{mapped_external_entity} to Connec!")
- response = connec_client.post("/#{connec_entity_name}", { "#{connec_entity_name}".to_sym => mapped_external_entity })
- response = JSON.parse(response.body)
- raise "Connec!: #{response['errors']['title']}" if response['errors'] && response['errors']['title']
- response["#{connec_entity_name}"]
- end
-
- def update_connec_entity(connec_client, mapped_external_entity, connec_id, connec_entity_name, organization)
-
- Maestrano::Connector::Rails::ConnectorLogger.log('info', organization, "Sending update #{connec_entity_name}: #{mapped_external_entity} to Connec!")
- response = connec_client.put("/#{connec_entity_name}/#{connec_id}", { "#{connec_entity_name}".to_sym => mapped_external_entity })
- response = JSON.parse(response.body)
- raise "Connec!: #{response['errors']['title']}" if response['errors'] && response['errors']['title']
- response["#{connec_entity_name}"]
+ def batch_op(method, mapped_external_entity, id, connec_entity_name, organization)
+ Maestrano::Connector::Rails::ConnectorLogger.log('info', organization, "Sending #{method.upcase} #{connec_entity_name}: #{mapped_external_entity} to Connec! (Preparing batch request)")
+ {
+ method: method,
+ url: "/api/v2/#{organization.uid}/#{connec_entity_name}" + (id.nil? ? '' : "/#{id}"),
+ params: {
+ "#{connec_entity_name}".to_sym => mapped_external_entity
+ }
+ }
end
def map_to_external_with_idmap(entity, organization)
idmap = self.class.find_idmap({connec_id: entity['id'], organization_id: organization.id})
\ No newline at end of file