Proxy for SendinBlue API

This commit is contained in:
Frederic Merizen 2019-03-07 16:30:25 +01:00
parent fc38880155
commit 98713b6a4d
2 changed files with 74 additions and 0 deletions

73
app/lib/sendinblue/api.rb Normal file
View file

@ -0,0 +1,73 @@
class Sendinblue::Api
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
def identify(email, attributes = {})
req = api_request('identify', email: email, attributes: attributes)
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
def run
hydra.run
@hydra = nil
flush_failures
end
private
def hydra
@hydra ||= Typhoeus::Hydra.new
end
def push_failure(failure)
@failures << failure
end
def flush_failures
failures = @failures
@failures = []
if failures.present?
raise StandardError, failures.join(', ')
end
end
def api_request(path, body)
url = "#{SENDINBLUE_API_URL}/#{path}"
Typhoeus::Request.new(
url,
method: :post,
body: body.to_json,
headers: headers
)
end
def headers
{
'ma-key': client_key,
'Content-Type': 'application/json; charset=UTF-8'
}
end
def client_key
Rails.application.secrets.sendinblue[:client_key]
end
end

View file

@ -6,6 +6,7 @@ API_GEO_URL = ENV.fetch("API_GEO_URL", "https://geo.api.gouv.fr")
API_GEO_SANDBOX_URL = ENV.fetch("API_GEO_SANDBOX_URL", "https://sandbox.geo.api.gouv.fr")
HELPSCOUT_API_URL = ENV.fetch("HELPSCOUT_API_URL", "https://api.helpscout.net/v2")
PIPEDRIVE_API_URL = ENV.fetch("PIPEDRIVE_API_URL", "https://api.pipedrive.com/v1")
SENDINBLUE_API_URL = ENV.fetch("SENDINBLUE_API_URL", "https://in-automate.sendinblue.com/api/v2")
# Internal URLs
FOG_BASE_URL = "https://static.demarches-simplifiees.fr"