Skip to content

Commit

Permalink
Parse error responses (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
nicholasbair authored Oct 5, 2024
1 parent 19c25a5 commit 12dd1db
Show file tree
Hide file tree
Showing 14 changed files with 32 additions and 62 deletions.
11 changes: 1 addition & 10 deletions config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ interfaces:
- name: "since_transaction_id"
type: "string"
doc: "ID of the Transaction to get Account changes since."
required: true
- module_name: "Instruments"
description: "Interface for Oanda instruments."
functions:
Expand Down Expand Up @@ -100,16 +101,6 @@ interfaces:
type: "string"
doc: "The day of the week used for granularities that have weekly alignment."
default: "Friday"

description: "Get position book data for an instrument."
http_method: "GET"
path: "/instruments/:instrument/positionBook"
arguments:
- "instrument"
parameters:
- name: "time"
type: "string"
doc: "The time of the snapshot to fetch using either RFC 3339 or Unix format. If not specified, then the most recent snapshot is fetched."
- module_name: "Orders"
description: "Interface for Oanda orders."
functions:
Expand Down
6 changes: 3 additions & 3 deletions lib/models/definitions/account/account_summary.ex
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ defmodule ExOanda.AccountSummary do
field(:currency, :string)
field(:created_by_user_id, :integer)
field(:created_time, :utc_datetime_usec)
field(:resettabled_pl_time, :utc_datetime_usec)
field(:resettable_pl_time, :utc_datetime_usec)
field(:margin_rate, :float)
field(:open_trade_count, :integer)
field(:open_position_count, :integer)
Expand Down Expand Up @@ -54,7 +54,7 @@ defmodule ExOanda.AccountSummary do
def changeset(struct, params) do
struct
|> cast(params, [
:id, :alias, :currency, :created_by_user_id, :created_time, :resettabled_pl_time,
:id, :alias, :currency, :created_by_user_id, :created_time, :resettable_pl_time,
:margin_rate, :open_trade_count, :open_position_count, :pending_order_count,
:hedging_enabled, :unrealized_pl, :nav, :margin_used, :margin_available,
:position_value, :margin_closeout_unrealized_pl, :margin_closeout_nav,
Expand All @@ -66,7 +66,7 @@ defmodule ExOanda.AccountSummary do
])
|> cast_embed(:guaranteed_stop_loss_order_parameters)
|> validate_required([
:id, :alias, :currency, :created_by_user_id, :created_time, :resettabled_pl_time,
:id, :alias, :currency, :created_by_user_id, :created_time, :resettable_pl_time,
:margin_rate, :open_trade_count, :open_position_count, :pending_order_count,
:hedging_enabled, :unrealized_pl, :nav, :margin_used, :margin_available,
:position_value, :margin_closeout_unrealized_pl, :margin_closeout_nav,
Expand Down
4 changes: 1 addition & 3 deletions lib/models/response/accounts/update_account.ex
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,13 @@ defmodule ExOanda.Response.UpdateAccount do
embeds_one :client_configure_transaction, ClientConfigureTransaction
embeds_one :client_configure_reject_transaction, ClientConfigureRejectTransaction

field(:error_code, :string)
field(:error_message, :string)
field(:last_transaction_id, :string)
end

@doc false
def changeset(struct, params) do
struct
|> cast(params, [:error_code, :error_message, :last_transaction_id])
|> cast(params, [:last_transaction_id])
|> cast_embed(:client_configure_transaction)
|> cast_embed(:client_configure_reject_transaction)
end
Expand Down
21 changes: 0 additions & 21 deletions lib/models/response/error.ex

This file was deleted.

4 changes: 1 addition & 3 deletions lib/models/response/orders/cancel_order.ex
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,12 @@ defmodule ExOanda.Response.CancelOrder do
embeds_one :order_cancel_reject_transaction, OrderCancelRejectTransaction
field(:related_transaction_ids, {:array, :string})
field(:last_transaction_id, :string)
field(:error_code, :string)
field(:error_message, :string)
end

@doc false
def changeset(struct, params) do
struct
|> cast(params, [:related_transaction_ids, :last_transaction_id, :error_code, :error_message])
|> cast(params, [:related_transaction_ids, :last_transaction_id])
|> cast_embed(:order_cancel_transaction)
|> cast_embed(:order_cancel_reject_transaction)
end
Expand Down
4 changes: 1 addition & 3 deletions lib/models/response/orders/create_order.ex
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,14 @@ defmodule ExOanda.Response.CreateOrder do
embeds_one :order_reissue_reject_transaction, Transaction
embeds_one :order_reject_transaction, Transaction

field(:error_code, :string)
field(:error_message, :string)
field(:related_transaction_ids, {:array, :string})
field(:last_transaction_id, :string)
end

@doc false
def changeset(struct, params) do
struct
|> cast(params, [:error_code, :error_message, :related_transaction_ids, :last_transaction_id])
|> cast(params, [:related_transaction_ids, :last_transaction_id])
|> cast_embed(:order_create_transaction)
|> cast_embed(:order_fill_transaction)
|> cast_embed(:order_cancel_transaction)
Expand Down
4 changes: 1 addition & 3 deletions lib/models/response/orders/modify_client_extensions.ex
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ defmodule ExOanda.Response.OrderModifyClientExtensions do
typed_embedded_schema do
field(:related_transaction_ids, {:array, :string})
field(:last_transaction_id, :string)
field(:error_code, :string)
field(:error_message, :string)

embeds_one :order_client_extensions_modify_transaction, OrderClientExtensionsModifyTransaction
embeds_one :order_client_extensions_modify_reject_transaction, OrderClientExtensionsModifyRejectTransaction
Expand All @@ -25,7 +23,7 @@ defmodule ExOanda.Response.OrderModifyClientExtensions do
@doc false
def changeset(struct, params) do
struct
|> cast(params, [:related_transaction_ids, :last_transaction_id, :error_code, :error_message])
|> cast(params, [:related_transaction_ids, :last_transaction_id])
|> cast_embed(:order_client_extensions_modify_transaction)
|> cast_embed(:order_client_extensions_modify_reject_transaction)
end
Expand Down
2 changes: 0 additions & 2 deletions lib/models/response/orders/replace_order.ex
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ defmodule ExOanda.Response.ReplaceOrder do
embeds_one :order_reject_transaction, Transaction
embeds_one :order_cancel_reject_transaction, Transaction

field(:error_code, :string)
field(:error_message, :string)
field(:related_transaction_ids, {:array, :string})
field(:last_transaction_id, :string)
end
Expand Down
4 changes: 1 addition & 3 deletions lib/models/response/positions/close_position.ex
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ defmodule ExOanda.Response.ClosePosition do
typed_embedded_schema do
field(:related_transaction_ids, {:array, :string})
field(:last_transaction_id, :string)
field(:error_code, :string)
field(:error_message, :string)

embeds_one :long_order_create_transaction, MarketOrderTransaction
embeds_one :long_order_fill_transaction, OrderFillTransaction
Expand All @@ -33,7 +31,7 @@ defmodule ExOanda.Response.ClosePosition do
@doc false
def changeset(struct, params) do
struct
|> cast(params, [:related_transaction_ids, :last_transaction_id, :error_code, :error_message])
|> cast(params, [:related_transaction_ids, :last_transaction_id])
|> cast_embed(:long_order_create_transaction)
|> cast_embed(:long_order_fill_transaction)
|> cast_embed(:long_order_cancel_transaction)
Expand Down
4 changes: 3 additions & 1 deletion lib/models/response/response.ex
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@ defmodule ExOanda.Response do
field(:data, MapOrList)
field(:request_id, :string)
field(:status, Atom)
field(:error_code, :string)
field(:error_message, :string)
end

@doc false
def changeset(struct, params) do
struct
|> cast(params, [:data, :request_id, :status])
|> cast(params, [:data, :request_id, :status, :error_code, :error_message])
|> validate_required([:data, :request_id, :status])
end
end
4 changes: 1 addition & 3 deletions lib/models/response/trades/close_trade.ex
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ defmodule ExOanda.Response.CloseTrade do
typed_embedded_schema do
field(:related_transaction_ids, {:array, :string})
field(:last_transaction_id, :string)
field(:error_code, :string)
field(:error_message, :string)

embeds_one :order_create_transaction, MarketOrderTransaction
embeds_one :order_fill_transaction, OrderFillTransaction
Expand All @@ -29,7 +27,7 @@ defmodule ExOanda.Response.CloseTrade do
@doc false
def changeset(struct, params) do
struct
|> cast(params, [:related_transaction_ids, :last_transaction_id, :error_code, :error_message])
|> cast(params, [:related_transaction_ids, :last_transaction_id])
|> cast_embed(:order_create_transaction)
|> cast_embed(:order_fill_transaction)
|> cast_embed(:order_cancel_transaction)
Expand Down
4 changes: 1 addition & 3 deletions lib/models/response/trades/modify_client_extensions.ex
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ defmodule ExOanda.Response.TradeModifyClientExtensions do
typed_embedded_schema do
field(:related_transaction_ids, {:array, :string})
field(:last_transaction_id, :string)
field(:error_code, :string)
field(:error_message, :string)

embeds_one :trade_client_extensions_modify_transaction, TradeClientExtensionsModifyTransaction
embeds_one :trade_client_extensions_modify_reject_transaction, TradeClientExtensionsModifyRejectTransaction
Expand All @@ -25,7 +23,7 @@ defmodule ExOanda.Response.TradeModifyClientExtensions do
@doc false
def changeset(struct, params) do
struct
|> cast(params, [:related_transaction_ids, :last_transaction_id, :error_code, :error_message])
|> cast(params, [:related_transaction_ids, :last_transaction_id])
|> cast_embed(:trade_client_extensions_modify_transaction)
|> cast_embed(:trade_client_extensions_modify_reject_transaction)
end
Expand Down
4 changes: 1 addition & 3 deletions lib/models/response/trades/update_trade.ex
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,14 @@ defmodule ExOanda.Response.UpdateTrade do
embeds_one :guaranteed_stop_loss_order_cancel_reject_transaction, OrderCancelRejectTransaction
embeds_one :guaranteed_stop_loss_order_reject_transaction, GuaranteedStopLossOrderRejectTransaction

field(:error_code, :string)
field(:error_message, :string)
field(:related_transaction_ids, {:array, :string})
field(:last_transaction_id, :string)
end

@doc false
def changeset(struct, params) do
struct
|> cast(params, [:related_transaction_ids, :last_transaction_id, :error_code, :error_message])
|> cast(params, [:related_transaction_ids, :last_transaction_id])
|> cast_embed(:take_profit_order_cancel_transaction)
|> cast_embed(:take_profit_order_transaction)
|> cast_embed(:take_profit_order_fill_transaction)
Expand Down
18 changes: 17 additions & 1 deletion lib/util/transform.ex
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,23 @@ defmodule ExOanda.Transform do
|> Map.get("requestid", [])
|> List.first()

%{"data" => data, "status" => status, "request_id" => request_id}
error_code =
response
|> Map.get(:body, %{})
|> Map.get("errorCode", nil)

error_message =
response
|> Map.get(:body, %{})
|> Map.get("errorMessage", nil)

%{
"data" => data,
"status" => status,
"request_id" => request_id,
"error_code" => error_code,
"error_message" => error_message
}
end

@spec preprocess_data(nil | atom(), map()) :: [Ecto.Schema.t()] | [map()] | Ecto.Schema.t() | map()
Expand Down

0 comments on commit 12dd1db

Please sign in to comment.