Using ExEasyPost to Calculate Shipping Costs

I’m trying to use ExEasyPost to calculate shipping costs. From what I understand from the EasyPost docs, ExEasyPost.Address.create(address) should return an address object with an id, and then you’re supposed to call ExEasyPost.Address.find(address.id) with that id then piped to ExEasyPost.request() to return… something?

address = ExEasyPost.Address.create(
  %{
  street1: "417 MONTGOMERY ST",
  city: "SAN FRANCISCO",
  state: "CA",
  zip: "94104",
  country: "US",
  company: "EasyPost",
  phone: "415-123-4567"
  }
)

config = %{api_key: "Test_API_Key", json_codec: Jason}
ExEasyPost.Address.find(address) 
|> ExEasyPost.request(config)

in my case, ExEasyPost.Address.create(%{...params}) returns

%ExEasyPost.Operation{
  headers: [],
  http_method: :post,
  params: %{
    address: %{
      city: "SAN FRANCISCO",
      company: "EasyPost",
      country: "US",
      phone: "415-123-4567",
      state: "CA",
      street1: "417 MONTGOMERY ST",
      zip: "94104"
    }
  },
  path: "addresses"
}

which does not include an id field. So when passed to ExEasyPost.Address.find(address) I just get a String.Chars not implemented for %ExEasyPost.Operation{} error and a resource not found error from ExEasyPost.request(config). There is no Elixir/Erlang example on the EasyPost docs, so I’m doing my best to translate things here. Am I reading this wrong? Does anyone have any examples of this in use?

I’m wondering if I’m even using this right, I think I should be creating a shipment and reading the rates key values?

Looks to me that it is necessary to

result = 
  ExEasyPost.Address.create(map)
  |> ExEasyPost.request(config)

to get your ID.


Notice in the documentation:

create(params)

create(map()) :: ExEasyPost.Operation.t()
find(id)

find(binary()) :: ExEasyPost.Operation.t()

both result in ExEasyPost.Operation.t()

And here

request(operation, overrides \\ [])

request(ExEasyPost.Operation.t(), Keyword.t()) :: {:ok, term()} | {:error, term()}

Even in a dynamically typed language you have to be type aware.

3 Likes