Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Undo changes and only allowing data-lake v2 #4

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,6 @@ config :paddy,
topic_id: "some-topic"
```

- Or if you prefer, you can call the publish method directly, passing the project_id and topic_id parameters as arguments:

```elixir
Paddy.publish(params, project_id: "custom_project_id", topic_id: "custom_topic_id")
```

### Using

> The lib is only responsible to receive the data and publish it to the previously set project_id/topic_id, the list IS NOT responsible to transform and mount the format, this step must be done by the application that will use `paddy`.
Expand Down
28 changes: 5 additions & 23 deletions lib/paddy.ex
Original file line number Diff line number Diff line change
Expand Up @@ -41,34 +41,16 @@ defmodule Paddy do
iex> {:ok,%GoogleApi.PubSub.V1.Model.PublishResponse{messageIds: ["422315144637561"]}}
"""

def publish(data, args \\ []) do
project_id = Keyword.get(args, :project_id, @project_id)
topic_id = Keyword.get(args, :topic_id, @topic_id)
client_email = Keyword.get(args, :client_email)
version = Keyword.get(args, :version, :v1)

encoded_data = encode_data(data, version)
def publish(data) do
encoded_data = Base.encode64(data)
message = %Model.PubsubMessage{data: encoded_data}
data_request = %Model.PublishRequest{messages: [message]}

Projects.pubsub_projects_topics_publish(get_connection(client_email), project_id, topic_id,
Projects.pubsub_projects_topics_publish(get_connection(), @project_id, @topic_id,
body: data_request
)
end

defp encode_data(data, version) do
case version do
:v1 ->
{:ok, encoded_data} = Poison.encode(data)
Base.encode64(encoded_data)
:v2 ->
Base.encode64(data)
_ ->
raise ArgumentError, "Unsupported version: #{inspect(version)}"
end
end


### Disclaimer!
#
# We do not need to be concerned about refreshing the token whenever we need to use it,
Expand All @@ -77,8 +59,8 @@ defmodule Paddy do
# We are currently using the lib in version 1.0.1 ~ 27/03/2019.
#
# Link for the doc: https://github.com/peburrows/goth/blob/master/lib/goth/token.ex#L3
defp get_connection(client_email) do
{:ok, token} = Goth.Token.for_scope({client_email, "https://www.googleapis.com/auth/pubsub"})
defp get_connection() do
{:ok, token} = Goth.Token.for_scope("https://www.googleapis.com/auth/pubsub")

token
|> Map.get(:token)
Expand Down
50 changes: 5 additions & 45 deletions test/paddy_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -21,52 +21,12 @@ defmodule PaddyTest do
payload: payload
}

with_mocks [
{Projects, [], pubsub_projects_topics_publish: fn _connection, project_id, topic_id, options ->
publish_request = Keyword.get(options, :body)
with_mock Projects,
pubsub_projects_topics_publish: fn _connection, _project_id, _topic_id, _params -> :ok end do
Paddy.publish(Jason.encode!(params))

assert %GoogleApi.PubSub.V1.Model.PublishRequest{
messages: [%GoogleApi.PubSub.V1.Model.PubsubMessage{data: _}]
} = publish_request
:ok
end},
{Goth.Token, [], for_scope: fn _ -> {:ok, %{token: "mocked_token"}} end}
] do
Paddy.publish(params)
assert called(Projects.pubsub_projects_topics_publish(:_, :_, :_, :_))
end
end

test "publish with custom project_id and topic_id" do
version = :v1
company_id = :rand.uniform(100)
event_type = "foobar_event"
payload = %{id: 1, name: "foobar"}
event_timestamp = ~N[2018-08-01 22:15:07]

params = %{
version: version,
company_id: company_id,
event_type: event_type,
event_timestamp: event_timestamp,
payload: payload
}

with_mocks [
{Projects, [], pubsub_projects_topics_publish: fn _connection, project_id, topic_id, options ->
assert project_id == "custom_project_id"
assert topic_id == "custom_topic_id"

publish_request = Keyword.get(options, :body)

assert %GoogleApi.PubSub.V1.Model.PublishRequest{
messages: [%GoogleApi.PubSub.V1.Model.PubsubMessage{data: _}]
} = publish_request
:ok
end},
{Goth.Token, [], for_scope: fn _ -> {:ok, %{token: "mocked_token"}} end}
] do
Paddy.publish(params, project_id: "custom_project_id", topic_id: "custom_topic_id", client_email: "custom_client_email")
end
end
end
end
end