From 8aac67a31b322d1ba7643bcb2dd4986a0d1affad Mon Sep 17 00:00:00 2001 From: Vincent Pochet Date: Tue, 18 Jun 2024 10:43:01 +0200 Subject: [PATCH] feat(event): Add support for ingest API --- lib/lago/api/client.rb | 16 ++++++++++++++-- lib/lago/api/resources/event.rb | 9 +++++++++ spec/lago/api/client_spec.rb | 32 +++++++++++++++++++++++++++++--- 3 files changed, 52 insertions(+), 5 deletions(-) diff --git a/lib/lago/api/client.rb b/lib/lago/api/client.rb index 6fdead0..7494fa9 100644 --- a/lib/lago/api/client.rb +++ b/lib/lago/api/client.rb @@ -3,14 +3,16 @@ module Lago module Api BASE_URL = 'https://api.getlago.com/' + BASE_INGEST_URL = 'https://ingest.getlago.com/' API_PATH = 'api/v1/' class Client - attr_reader :api_key, :api_url + attr_reader :api_key, :api_url, :ingest_api_url - def initialize(api_key: nil, api_url: nil) + def initialize(api_key: nil, api_url: nil, ingest_api_url: nil) @api_key = api_key @api_url = api_url + @ingest_api_url = ingest_api_url end def base_api_url @@ -19,6 +21,16 @@ def base_api_url URI.join(base_url, Lago::Api::API_PATH) end + def base_ingest_api_url + base_url = if ingest_api_url.nil? + (api_url.nil? || api_url == Lago::Api::BASE_URL) ? Lago::Api::BASE_INGEST_URL : api_url + else + ingest_api_url + end + + URI.join(base_url, Lago::Api::API_PATH) + end + def customers Lago::Api::Resources::Customer.new(self) end diff --git a/lib/lago/api/resources/event.rb b/lib/lago/api/resources/event.rb index b53dc8e..34e6886 100644 --- a/lib/lago/api/resources/event.rb +++ b/lib/lago/api/resources/event.rb @@ -12,6 +12,15 @@ def root_name 'event' end + def create(params) + uri = URI("#{client.base_ingest_api_url}#{api_resource}") + + payload = whitelist_params(params) + response = connection.post(payload, uri)[root_name] + + JSON.parse(response.to_json, object_class: OpenStruct) + end + def batch_create(params) uri = URI("#{client.base_api_url}#{api_resource}/batch") diff --git a/spec/lago/api/client_spec.rb b/spec/lago/api/client_spec.rb index 24bdfaf..8a6ee65 100644 --- a/spec/lago/api/client_spec.rb +++ b/spec/lago/api/client_spec.rb @@ -3,15 +3,15 @@ require 'spec_helper' RSpec.describe Lago::Api::Client do - let(:api_url) { 'http://test.api.url/' } - subject(:client) do described_class.new( api_key: '123456', - api_url: api_url + api_url: api_url, ) end + let(:api_url) { 'http://test.api.url/' } + describe '#base_api_url' do context 'when api_url is given' do it { expect(client.base_api_url).to eq(URI("#{api_url}api/v1/")) } @@ -23,4 +23,30 @@ it { expect(client.base_api_url).to eq(URI('https://api.getlago.com/api/v1/')) } end end + + describe '#base_ingest_api_url' do + context 'when no ingest_api_url is provided' do + it { expect(client.base_ingest_api_url).to eq(URI("#{api_url}api/v1/")) } + + context 'when api_url is the default' do + let(:api_url) { Lago::Api::BASE_URL } + + it { expect(client.base_ingest_api_url).to eq(URI('https://ingest.getlago.com/api/v1/')) } + end + end + + context 'when ingest is provided' do + let(:ingest_api_url) { 'http://ingest.api.url/' } + + let(:client) do + described_class.new( + api_key: '123456', + api_url: api_url, + ingest_api_url: ingest_api_url, + ) + end + + it { expect(client.base_ingest_api_url).to eq(URI("#{ingest_api_url}api/v1/")) } + end + end end