Skip to content

Commit

Permalink
producers
Browse files Browse the repository at this point in the history
  • Loading branch information
efcasado committed May 11, 2024
1 parent 082d200 commit 14e6794
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 67 deletions.
9 changes: 0 additions & 9 deletions lib/uof/api/descriptions.ex
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,4 @@ defmodule UOF.API.Descriptions do

HTTP.get(%UOF.API.Mappings.VariantDescriptions{}, endpoint)
end

@doc """
Describe all currently avbailable producers and their ids.
"""
def producers do
endpoint = ["descriptions", "producers.xml"]

HTTP.get(%UOF.API.Mappings.Producers{}, endpoint)
end
end
73 changes: 73 additions & 0 deletions lib/uof/api/descriptions/producers.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# https://docs.betradar.com/display/BD/UOF+-+Producers
defmodule UOF.API.Descriptions.Producer do
@moduledoc """
Unified Odds Feed handles data from multiple sources, which are called
odds or message producers.
"""
use Ecto.Schema
import Ecto.Changeset
import UOF.API.EctoHelpers

@doc """
List all available producers.
"""
@spec all() :: list(VoidReason.t())
def all() do
case UOF.API.get("/descriptions/producers.xml") do
{:ok, %_{status: 200, body: resp}} ->
resp
|> Map.get("producers")
|> Map.get("producer")
|> Enum.map(fn x ->
{:ok, x} = changeset(x)
x
end)

{:error, _} = error ->
error
end
end

@primary_key false

embedded_schema do
field(:id, :integer)
field(:name, :string)
field(:description, :string)
field(:api_url, :string)
field(:active, :boolean)
field(:scope, {:array, Ecto.Enum}, values: [:live, :prematch, :virtual])
field(:stateful_recovery_window_in_minutes, :integer)
end

def changeset(model \\ %__MODULE__{}, params) do
model
|> cast(prepare(params), [
:id,
:name,
:description,
:api_url,
:active,
:scope,
:stateful_recovery_window_in_minutes
])
|> case do
%Ecto.Changeset{valid?: true} = changeset ->
{:ok, apply_changes(changeset)}

%Ecto.Changeset{} = changeset ->
{:error, {params, traverse_errors(changeset)}}
end
end

defp prepare(params) do
params
|> rename_fields
|> prepare_scope
end

defp prepare_scope(params) do
scope = String.split(params["scope"], "|")
Map.put(params, "scope", scope)
end
end
14 changes: 0 additions & 14 deletions lib/uof/api/mappings/producer.ex

This file was deleted.

10 changes: 0 additions & 10 deletions lib/uof/api/mappings/producers.ex

This file was deleted.

9 changes: 0 additions & 9 deletions lib/uof/api/mappings/void_reason.ex

This file was deleted.

10 changes: 0 additions & 10 deletions lib/uof/api/mappings/void_reason_descriptions.ex

This file was deleted.

28 changes: 28 additions & 0 deletions test/uof/api/descriptions/producer_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
defmodule UOF.API.Descriptions.Producer.Test do
use ExUnit.Case

setup do
:ok = Application.put_env(:tesla, UOF.API, adapter: Tesla.Mock)

Tesla.Mock.mock(fn
%{method: :get} ->
resp = File.read!("test/data/producers.xml")
%Tesla.Env{status: 200, headers: [{"content-type", "application/xml"}], body: resp}
end)

:ok
end

test "can parse UOF.API.Descriptions.Producer.all/0 response" do
resp = UOF.API.Descriptions.Producer.all()

assert Enum.count(resp) == 15
assert hd(resp).id == 1
assert hd(resp).name == "LO"
assert hd(resp).description == "Live Odds"
assert hd(resp).api_url == "https://stgapi.betradar.com/v1/liveodds/"
assert hd(resp).active == true
assert hd(resp).scope == [:live]
assert hd(resp).stateful_recovery_window_in_minutes == 600
end
end
15 changes: 0 additions & 15 deletions test/uof/api/descriptions_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -50,19 +50,4 @@ defmodule UOF.API.Descriptions.Test do
assert outcome_mapping.product_outcome_id == "26"
assert outcome_mapping.product_outcome_name == "4:0"
end

test "can parse UOF.API.Descriptions.producers/0 response" do
{:ok, desc} = UOF.API.Descriptions.producers()

producer = hd(desc.producers)

assert Enum.count(desc.producers) == 15
assert producer.id == 1
assert producer.name == "LO"
assert producer.description == "Live Odds"
assert producer.api_url == "https://stgapi.betradar.com/v1/liveodds/"
assert producer.active == true
assert producer.scope == "live"
assert producer.stateful_recovery_window_in_minutes == 600
end
end

0 comments on commit 14e6794

Please sign in to comment.