Skip to content

Commit

Permalink
use saxy simpleform
Browse files Browse the repository at this point in the history
  • Loading branch information
efcasado committed May 20, 2024
1 parent 93422ad commit 9c927db
Show file tree
Hide file tree
Showing 27 changed files with 502 additions and 176 deletions.
5 changes: 5 additions & 0 deletions config/test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import Config

config :uof_api,
base_url: "https://localhost",
auth_token: "a-secret-goes-here"
3 changes: 2 additions & 1 deletion lib/uof/api.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ defmodule UOF.API do
{"x-access-token", Application.get_env(:uof_api, :auth_token)}
])

plug(Tesla.Middleware.XML, convention: BadgerFish)
# plug(Tesla.Middleware.XML, convention: BadgerFish)
plug(Tesla.Middleware.XML, engine: Saxy)

# https://docs.betradar.com/display/BD/UOF+-+Language+Support
@supported_languages [
Expand Down
3 changes: 1 addition & 2 deletions lib/uof/api/descriptions/bet_stop.ex
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ defmodule UOF.API.Descriptions.BetStop do
case UOF.API.get("/descriptions/betstop_reasons.xml") do
{:ok, %_{status: 200, body: resp}} ->
resp
|> UOF.API.Utils.xml_to_map()
|> Map.get("betstop_reasons_descriptions")
|> Map.get("betstop_reason")
|> Enum.map(fn x ->
Expand Down Expand Up @@ -134,8 +135,6 @@ defmodule UOF.API.Descriptions.BetStop do
end

def changeset(model \\ %__MODULE__{}, params) do
params = sanitize(params)

model
|> cast(params, [:id, :description])
|> apply
Expand Down
3 changes: 1 addition & 2 deletions lib/uof/api/descriptions/betting_status.ex
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ defmodule UOF.API.Descriptions.BettingStatus do
case UOF.API.get("/descriptions/betting_status.xml") do
{:ok, %_{status: 200, body: resp}} ->
resp
|> UOF.API.Utils.xml_to_map()
|> Map.get("betting_status_descriptions")
|> Map.get("betting_status")
|> Enum.map(fn x ->
Expand Down Expand Up @@ -51,8 +52,6 @@ defmodule UOF.API.Descriptions.BettingStatus do
end

def changeset(model \\ %__MODULE__{}, params) do
params = sanitize(params)

model
|> cast(params, [:id, :description])
|> apply
Expand Down
5 changes: 2 additions & 3 deletions lib/uof/api/descriptions/market.ex
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ defmodule UOF.API.Descriptions.Market do
) do
{:ok, %_{status: 200, body: resp}} ->
resp
|> UOF.API.Utils.xml_to_map()
|> Map.get("market_descriptions")
|> Map.get("market")
|> Enum.map(fn x ->
Expand All @@ -38,6 +39,7 @@ defmodule UOF.API.Descriptions.Market do
) do
{:ok, %_{status: 200, body: resp}} ->
resp
|> UOF.API.Utils.xml_to_map()
|> Map.get("market_descriptions")
|> Map.get("market")

Expand Down Expand Up @@ -80,7 +82,6 @@ defmodule UOF.API.Descriptions.Market do
def changeset(%__MODULE__{} = model, params) do
params =
params
|> sanitize
|> bubble_up("outcomes", "outcome")
|> bubble_up("specifiers", "specifier")
|> split("groups", "|")
Expand All @@ -93,12 +94,10 @@ defmodule UOF.API.Descriptions.Market do
end

def changeset(%UOF.API.Descriptions.Market.Outcome{} = model, params) do
params = sanitize(params)
cast(model, params, [:id, :name])
end

def changeset(%UOF.API.Descriptions.Market.Specifier{} = model, params) do
params = sanitize(params)
cast(model, params, [:type, :name])
end
end
12 changes: 4 additions & 8 deletions lib/uof/api/descriptions/match_status.ex
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ defmodule UOF.API.Descriptions.MatchStatus do
case UOF.API.get("/descriptions/#{lang}/match_status.xml") do
{:ok, %_{status: 200, body: resp}} ->
resp
|> UOF.API.Utils.xml_to_map()
|> Map.get("match_status_descriptions")
|> Map.get("match_status")
|> Enum.map(fn x ->
Expand Down Expand Up @@ -49,19 +50,14 @@ defmodule UOF.API.Descriptions.MatchStatus do
def changeset(model \\ %__MODULE__{}, params)

def changeset(%__MODULE__{} = model, params) do
params = sanitize(params)

model
|> cast(params, [:id, :description, :period_number])
|> cast_embed(:sports, with: &changeset/2)
|> apply
end

def changeset(%UOF.API.Descriptions.MatchStatus.Sports{} = model, params) do
params =
params
|> sanitize
|> prepare_ids
params = prepare_ids(params)

model
|> cast(params, [:all, :ids])
Expand All @@ -72,8 +68,8 @@ defmodule UOF.API.Descriptions.MatchStatus do
params
|> Map.get("sport", [])
|> Enum.map(fn
{"@id", id} -> id
%{"@id" => id} -> id
{"id", id} -> id
%{"id" => id} -> id
end)

Map.put(params, "ids", ids)
Expand Down
6 changes: 2 additions & 4 deletions lib/uof/api/descriptions/producers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ defmodule UOF.API.Descriptions.Producer do
case UOF.API.get("/descriptions/producers.xml") do
{:ok, %_{status: 200, body: resp}} ->
resp
|> UOF.API.Utils.xml_to_map()
|> Map.get("producers")
|> Map.get("producer")
|> Enum.map(fn x ->
Expand All @@ -41,10 +42,7 @@ defmodule UOF.API.Descriptions.Producer do
end

def changeset(model \\ %__MODULE__{}, params) do
params =
params
|> sanitize
|> split("scope", "|")
params = split(params, "scope", "|")

model
|> cast(params, [
Expand Down
7 changes: 1 addition & 6 deletions lib/uof/api/descriptions/variant.ex
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ defmodule UOF.API.Descriptions.Variant do
case UOF.API.get("/descriptions/#{lang}/variants.xml") do
{:ok, %_{status: 200, body: resp}} ->
resp
|> UOF.API.Utils.xml_to_map()
|> Map.get("variant_descriptions")
|> Map.get("variant")
|> Enum.map(fn x ->
Expand Down Expand Up @@ -55,7 +56,6 @@ defmodule UOF.API.Descriptions.Variant do
def changeset(%__MODULE__{} = model, params) do
params =
params
|> sanitize
|> bubble_up("outcomes", "outcome")
|> bubble_up("mappings", "mapping")

Expand All @@ -67,16 +67,13 @@ defmodule UOF.API.Descriptions.Variant do
end

def changeset(%UOF.API.Descriptions.Variant.Outcome{} = model, params) do
params = sanitize(params)

model
|> cast(params, [:id, :name])
end

def changeset(%UOF.API.Descriptions.Variant.Mapping{} = model, params) do
params =
params
|> sanitize
|> split("product_ids", "|")
|> rename("mapping_outcome", "outcome_mappings", [])

Expand All @@ -86,8 +83,6 @@ defmodule UOF.API.Descriptions.Variant do
end

def changeset(%UOF.API.Descriptions.Variant.Mapping.OutcomeMapping{} = model, params) do
params = sanitize(params)

model
|> cast(params, [:outcome_id, :product_outcome_id, :product_outcome_name])
end
Expand Down
3 changes: 1 addition & 2 deletions lib/uof/api/descriptions/void_reason.ex
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ defmodule UOF.API.Descriptions.VoidReason do
case UOF.API.get("/descriptions/void_reasons.xml") do
{:ok, %_{status: 200, body: resp}} ->
resp
|> UOF.API.Utils.xml_to_map()
|> Map.get("void_reasons_descriptions")
|> Map.get("void_reason")
|> Enum.map(fn x ->
Expand Down Expand Up @@ -63,8 +64,6 @@ defmodule UOF.API.Descriptions.VoidReason do
end

def changeset(model \\ %__MODULE__{}, params) do
params = sanitize(params)

model
|> cast(params, [:id, :description])
|> apply
Expand Down
12 changes: 0 additions & 12 deletions lib/uof/api/ecto_helpers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,6 @@ defmodule UOF.API.EctoHelpers do
end)
end

@doc """
Remove leading '@' from field names in the `params` map.
"""
def sanitize(params) do
params
|> Enum.map(fn
{<<"@", key::binary>>, value} -> {key, value}
other -> other
end)
|> Map.new()
end

def bubble_up(params, level1, level2) do
values =
params
Expand Down
10 changes: 0 additions & 10 deletions lib/uof/api/mappings/tournaments.ex

This file was deleted.

18 changes: 0 additions & 18 deletions lib/uof/api/sports.ex
Original file line number Diff line number Diff line change
Expand Up @@ -130,24 +130,6 @@ defmodule UOF.API.Sports do
HTTP.get(%UOF.API.Mappings.Timeline{}, endpoint)
end

def tournaments(lang \\ "en") do
endpoint = ["sports", lang, "tournaments.xml"]

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

@doc """
Get details about the given tournament.
"""
def tournament(tournament, lang \\ "en") do
# https://docs.betradar.com/display/BD/UOF+-+Tournament+we+provide+coverage+for
endpoint = ["sports", lang, "tournaments", tournament, "info.xml"]

# TO-DO: staged tournaments
# https://docs.betradar.com/display/BD/UOF+-+Formula+1
HTTP.get(%UOF.API.Mappings.TournamentInfo{}, endpoint)
end

## Entity Description
## =========================================================================
@doc """
Expand Down
7 changes: 2 additions & 5 deletions lib/uof/api/sports/category.ex
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ defmodule UOF.API.Sports.Category do
case UOF.API.get("/sports/#{lang}/sports/#{sport}/categories.xml") do
{:ok, %_{status: 200, body: resp}} ->
resp
|> UOF.API.Utils.xml_to_map()
|> Map.get("sport_categories")
|> changeset
|> apply

{:error, _} = error ->
error
Expand Down Expand Up @@ -66,19 +68,14 @@ defmodule UOF.API.Sports.Category do
|> cast(params, [])
|> cast_embed(:sport, with: &changeset/2)
|> cast_embed(:categories, with: &changeset/2)
|> apply
end

def changeset(%UOF.API.Sports.Category.Sport{} = model, params) do
params = sanitize(params)

model
|> cast(params, [:id, :name])
end

def changeset(%UOF.API.Sports.Category.Category{} = model, params) do
params = sanitize(params)

model
|> cast(params, [:id, :name, :country_code])
end
Expand Down
9 changes: 3 additions & 6 deletions lib/uof/api/sports/sport.ex
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ defmodule UOF.API.Sports.Sport do
case UOF.API.get("/sports/#{lang}/sports.xml") do
{:ok, %_{status: 200, body: resp}} ->
resp
|> UOF.API.Utils.xml_to_map()
|> Map.get("sports")
|> Map.get("sport")
|> Enum.map(fn x ->
{:ok, x} = changeset(x)
{:ok, x} = apply(changeset(x))
x
end)

Expand All @@ -38,10 +39,6 @@ defmodule UOF.API.Sports.Sport do
end

def changeset(model \\ %__MODULE__{}, params) do
params = sanitize(params)

model
|> cast(params, [:id, :name])
|> apply
cast(model, params, [:id, :name])
end
end
Loading

0 comments on commit 9c927db

Please sign in to comment.