-
Notifications
You must be signed in to change notification settings - Fork 10
/
kinesis.ex
71 lines (58 loc) · 1.96 KB
/
kinesis.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
defmodule KinesisClient.Kinesis do
@moduledoc """
Behaviour that Kinesis client needs to support
"""
def get_shard_iterator(stream_name, shard_id, shard_iterator_type, opts \\ []) do
client(opts).get_shard_iterator(stream_name, shard_id, shard_iterator_type, opts)
end
def describe_stream(stream_name, opts \\ []) do
client(opts).describe_stream(stream_name, opts)
end
def get_records(shard_iterator, opts \\ []) do
client(opts).get_records(shard_iterator, opts)
end
defp client(opts) do
Keyword.get(opts, :adapter, KinesisClient.Kinesis.AwsAdapter)
end
end
defmodule KinesisClient.Kinesis.Adapter do
@moduledoc false
@type iterator_type :: :at_sequence_number | :after_sequence_number | :trim_horizon | :latest
@callback get_shard_iterator(
stream_name :: binary,
shard_id :: binary,
shard_iterator_type :: iterator_type,
opts :: keyword
) :: {:ok, map}
@callback describe_stream(stream_name :: binary, opts :: keyword) :: {:ok, map}
@callback get_records(shard_iterator :: String.t(), opts :: keyword) ::
{:ok, map}
end
defmodule KinesisClient.Kinesis.AwsAdapter do
@moduledoc false
alias ExAws.Kinesis
alias KinesisClient.Kinesis.Adapter
@behaviour Adapter
@impl Adapter
def describe_stream(stream_name, opts) do
case Kinesis.describe_stream(stream_name, opts) |> ExAws.request() do
{:ok, _} = reply -> reply
error -> error
end
end
@impl Adapter
def get_shard_iterator(stream_name, shard_id, shard_iterator_type, opts) do
request = Kinesis.get_shard_iterator(stream_name, shard_id, shard_iterator_type, opts)
case request |> ExAws.request() do
{:ok, _} = reply -> reply
error -> error
end
end
@impl Adapter
def get_records(shard_iterator, opts) do
case Kinesis.get_records(shard_iterator, opts) |> ExAws.request() do
{:ok, _} = r -> r
{:error, _} = e -> e
end
end
end