elixir-google-api icon indicating copy to clipboard operation
elixir-google-api copied to clipboard

RequestBuilder should allow 2xx status codes beyond just 200

Open juszzz opened this issue 8 years ago • 2 comments

The current RequestBuilder.decode methods assume that the only successful status code that should result in decoding the response is an HTTP 200 code. I believe that this should be expanded such that other 200-level status codes are also treated as successful responses that can be decoded. For example:

  defmacro is_success(code) do
    quote do: unquote(code) in [200, 201, 202, 203, 204, 205, 206, 207, 208, 226]
  end

  @spec decode(Tesla.Env.t | term()) :: {:ok, struct()} | {:error, Tesla.Env.t} | {:error, term()}
  def decode(%Tesla.Env{status: code, body: body}) when is_success(code), do: Poison.decode(body)
  def decode(response), do: {:error, response}

  @spec decode(Tesla.Env.t | term(), :false | struct() | [struct()]) :: {:ok, struct()} | {:error, Tesla.Env.t} | {:error, term()}
  def decode(%Tesla.Env{status: code} = env, false) when is_success(code), do: {:ok, env}
  def decode(%Tesla.Env{status: code, body: body}, struct) when is_success(code), do: Poison.decode(body, as: struct)
  def decode(response, _struct), do: {:error, response}

juszzz avatar Dec 18 '17 21:12 juszzz

This has been addressed in the template in https://github.com/GoogleCloudPlatform/elixir-google-api/pull/50 so we should just need to regenerate clients. We'll likely do a global regenerate at some point soon since we've made a bunch of recent fixes. But is there any particular API that you're needing this for?

dazuma avatar Feb 08 '18 16:02 dazuma

Thanks for the update @dazuma, I agree that it looks like #50 solves this issue. No urgent need on my end; I already implemented a fix in the auto-generated code for the client library I needed. Just wanted to raise the issue.

juszzz avatar Feb 09 '18 20:02 juszzz