demarches-normaliennes/app/lib/sendinblue/api.rb

134 lines
3.3 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
2020-08-05 18:40:47 +02:00
class Sendinblue::API
2019-03-07 16:30:25 +01:00
def self.new_properly_configured!
api = self.new
if !api.properly_configured?
raise StandardError, 'Sendinblue API is not properly configured'
end
api
end
def initialize
@failures = []
end
def properly_configured?
client_key.present?
end
2019-10-01 17:28:06 +02:00
def update_contact(email, attributes = {})
2021-04-08 17:13:39 +02:00
# TODO: refactor this to use the official SiB SDK (by using contact create + attributes)
2019-10-01 17:28:06 +02:00
req = post_api_request('contacts', email: email, attributes: attributes, updateEnabled: true)
2019-03-07 16:30:25 +01:00
req.on_complete do |response|
if !response.success?
push_failure("Error while updating identity for administrateur '#{email}' in Sendinblue: #{response.response_code} '#{response.body}'")
end
end
hydra.queue(req)
end
2021-04-08 17:13:39 +02:00
# Get messages sent to a user through SendInBlue.
#
# Returns an array of SentMail objects.
def sent_mails(email_address)
client = ::SibApiV3Sdk::TransactionalEmailsApi.new
@events = client.get_email_event_report(email: email_address, days: 30).events
if @events.blank?
Rails.logger.info "SendInBlue::API: no messages found for email address '#{email_address}'"
return []
end
@events.group_by(&:message_id).values.map do |message_events|
latest_event = message_events.first
SentMail.new(
from: latest_event.from,
to: latest_event.email,
subject: latest_event.subject,
delivered_at: parse_date(latest_event.date),
status: latest_event.event,
service_name: 'SendInBlue',
external_url: 'https://app-smtp.sendinblue.com/log'
)
end
rescue ::SibApiV3Sdk::ApiError => e
Rails.logger.error e.message
[]
end
2021-08-14 12:46:56 +02:00
def delete_events(day, opts = {})
client = ::SibApiV3Sdk::TransactionalEmailsApi.new
event_opts = { start_date: day, end_date: day, limit: 100 }.merge(opts)
while (events = client.get_email_event_report(event_opts).events).present?
message_ids = events.map(&:message_id).uniq
message_ids.each do |message_id|
client.smtp_log_message_id_delete(message_id)
end
end
true
rescue ::SibApiV3Sdk::ApiError => e
Rails.logger.error e.message
false
end
def unblock_user(email_address)
client = ::SibApiV3Sdk::TransactionalEmailsApi.new
client.smtp_blocked_contacts_email_delete(email_address)
true
rescue ::SibApiV3Sdk::ApiError => e
Rails.logger.error e.message
false
end
2019-03-07 16:30:25 +01:00
def run
hydra.run
@hydra = nil
flush_failures
end
private
def hydra
2019-10-01 17:28:06 +02:00
@hydra ||= Typhoeus::Hydra.new(max_concurrency: 50)
2019-03-07 16:30:25 +01:00
end
def push_failure(failure)
@failures << failure
end
def flush_failures
failures = @failures
@failures = []
if failures.present?
raise StandardError, failures.join(', ')
end
end
2019-10-01 17:28:06 +02:00
def post_api_request(path, body)
url = "#{SENDINBLUE_API_V3_URL}/#{path}"
2019-03-07 16:30:25 +01:00
Typhoeus::Request.new(
url,
method: :post,
body: body.to_json,
headers: headers
)
end
def headers
{
2019-10-01 17:28:06 +02:00
'api-key': client_key,
2019-03-07 16:30:25 +01:00
'Content-Type': 'application/json; charset=UTF-8'
}
end
def client_key
2019-10-01 17:28:06 +02:00
Rails.application.secrets.sendinblue[:api_v3_key]
2019-03-07 16:30:25 +01:00
end
2021-04-08 17:13:39 +02:00
def parse_date(date)
date.is_a?(String) ? Time.zone.parse(date) : date
end
2019-03-07 16:30:25 +01:00
end