Skip to content

Commit

Permalink
feat(event): Add support for ingest API
Browse files Browse the repository at this point in the history
  • Loading branch information
vincent-pochet committed Jun 18, 2024
1 parent 9787ab8 commit 8aac67a
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 5 deletions.
16 changes: 14 additions & 2 deletions lib/lago/api/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
9 changes: 9 additions & 0 deletions lib/lago/api/resources/event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down
32 changes: 29 additions & 3 deletions spec/lago/api/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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/")) }
Expand All @@ -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

0 comments on commit 8aac67a

Please sign in to comment.