Skip to content

Commit

Permalink
refactor config
Browse files Browse the repository at this point in the history
  • Loading branch information
mithereal committed Nov 22, 2022
1 parent 686e1a7 commit 3dad4ad
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 48 deletions.
16 changes: 14 additions & 2 deletions lib/shippex/carrier/ups.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ defmodule Shippex.Carrier.UPS do
import Shippex.Address, only: [state_without_country: 1]

alias Shippex.Carrier.UPS.Client
alias Shippex.{Address, Shipment, Service, Transaction, Util}
alias Shippex.{Address, Config, InvalidConfigError, Shipment, Service, Transaction, Util}

defmacro with_response(response, do: block) do
quote do
Expand Down Expand Up @@ -432,5 +432,17 @@ defmodule Shippex.Carrier.UPS do
end
end

defdelegate config(), to: Shippex.Config, as: :ups_config
defp config() do
with cfg when is_list(cfg) <- Keyword.get(Config.config(), :usps, {:error, :not_found}),
un <- Keyword.get(cfg, :username, {:error, :not_found, :username}),
pw <- Keyword.get(cfg, :password, {:error, :not_found, :password}) do
%{username: un, password: pw}
else
{:error, :not_found, token} ->
raise InvalidConfigError, message: "USPS config key missing: #{token}"

{:error, :not_found} ->
raise InvalidConfigError, message: "USPS config is either invalid or not found."
end
end
end
33 changes: 31 additions & 2 deletions lib/shippex/carrier/usps.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ defmodule Shippex.Carrier.USPS do
import Shippex.Address, only: [state_without_country: 1]

alias Shippex.Carrier.USPS.Client
alias Shippex.{Address, Package, Label, Service, Shipment, Util}
alias Shippex.{Address, Config, InvalidConfigError, Package, Label, Service, Shipment, Util}

@default_container :rectangular
@large_containers ~w(rectangular nonrectangular variable)a
Expand Down Expand Up @@ -489,5 +489,34 @@ defmodule Shippex.Carrier.USPS do
|> String.replace(~r/<\/?\w+>.*<\/\w+>/, "")
end

defdelegate config(), to: Shippex.Config, as: :usps_config
def config() do
with cfg when is_list(cfg) <-
Keyword.get(Config.config(), :ups, {:error, :not_found}),
sk when is_binary(sk) <-
Keyword.get(cfg, :secret_key, {:error, :not_found, :secret_key}),
sh when is_map(sh) <- Keyword.get(cfg, :shipper, {:error, :not_found, :shipper}),
an when is_binary(an) <-
Keyword.get(cfg, :shipper)
|> Map.get(:account_number, {:error, :not_found, :account_number}),
un when is_binary(an) <- Keyword.get(cfg, :username, {:error, :not_found, :username}),
pw when is_binary(pw) <- Keyword.get(cfg, :password, {:error, :not_found, :password}) do
%{
username: un,
password: pw,
secret_key: sk,
shipper: sh
}
else
{:error, :not_found, :shipper} ->
raise InvalidConfigError,
message:
"UPS shipper config key missing. This could be because was provided as a keyword list instead of a map."

{:error, :not_found, token} ->
raise InvalidConfigError, message: "UPS config key missing: #{token}"

{:error, :not_found} ->
raise InvalidConfigError, message: "UPS config is either invalid or not found."
end
end
end
44 changes: 0 additions & 44 deletions lib/shippex/config.ex
Original file line number Diff line number Diff line change
Expand Up @@ -43,48 +43,4 @@ defmodule Shippex.Config do
_ -> raise InvalidConfigError, "Shippex env must be either :dev or :prod"
end
end

def usps_config() do
with cfg when is_list(cfg) <- Keyword.get(config(), :usps, {:error, :not_found}),
un <- Keyword.get(cfg, :username, {:error, :not_found, :username}),
pw <- Keyword.get(cfg, :password, {:error, :not_found, :password}) do
%{username: un, password: pw}
else
{:error, :not_found, token} ->
raise InvalidConfigError, message: "USPS config key missing: #{token}"

{:error, :not_found} ->
raise InvalidConfigError, message: "USPS config is either invalid or not found."
end
end

def ups_config() do
with cfg when is_list(cfg) <- Keyword.get(config(), :ups, {:error, :not_found}),
sk when is_binary(sk) <-
Keyword.get(cfg, :secret_key, {:error, :not_found, :secret_key}),
sh when is_map(sh) <- Keyword.get(cfg, :shipper, {:error, :not_found, :shipper}),
an when is_binary(an) <-
Keyword.get(cfg, :shipper)
|> Map.get(:account_number, {:error, :not_found, :account_number}),
un when is_binary(an) <- Keyword.get(cfg, :username, {:error, :not_found, :username}),
pw when is_binary(pw) <- Keyword.get(cfg, :password, {:error, :not_found, :password}) do
%{
username: un,
password: pw,
secret_key: sk,
shipper: sh
}
else
{:error, :not_found, :shipper} ->
raise InvalidConfigError,
message:
"UPS shipper config key missing. This could be because was provided as a keyword list instead of a map."

{:error, :not_found, token} ->
raise InvalidConfigError, message: "UPS config key missing: #{token}"

{:error, :not_found} ->
raise InvalidConfigError, message: "UPS config is either invalid or not found."
end
end
end

0 comments on commit 3dad4ad

Please sign in to comment.