Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

1.2 draft #240

Closed
3 tasks done
teamon opened this issue Aug 29, 2018 · 3 comments
Closed
3 tasks done

1.2 draft #240

teamon opened this issue Aug 29, 2018 · 3 comments
Milestone

Comments

@teamon
Copy link
Member

teamon commented Aug 29, 2018

Tesla 1.2 Draft

This is an early draft of proposed changes for next version of tesla.
All feedback is more than welcome - the more I know how you all use this library the better it can become 💃

There are three main points I'd like to tackle (A, B and C). This does not mean any other issues/improvements won't make it into the next release, these are the biggest & hardest ones.

A. Promote macro-less clients

Instead of

defmodule Github do
  use Tesla

  plug Tesla.Middleware.BaseUrl, "https://api.github.com"
  plug Tesla.Middleware.JSON

  adapter Tesla.Adapter.Hackney

  def repos, do: get("/repos")
end

use this one

defmodule Github do
  @middleware [
    {Tesla.Middleware.BaseUrl, "https://api.github.com"},
    Tesla.Middleware.JSON
  ]

  @adapter Tesla.Adapter.Hackney

  def repos, do: Tesla.get(client(), "/repos")

  defp client, do: Tesla.client(@middleware, @adapter)
end

or with dynamic middleware/adapter

defmodule MyApi do
  @default_middleware [
    {Tesla.Middleware.BaseUrl, "https://api.github.com"},
    Tesla.Middleware.JSON
  ]

  def items(client \\ new()), do: Tesla.get(client, "/items")

  def new(user_token, opts \\ []) do
    middleware = [{Tesla.Middleware.Headers, [{"authorization", "Bearer #{user_token}"}]}]
    Tesla.client(middleware ++ @default_middleware, opts[:adapter] || @adapter)
  end
end

The reasons for such change are (unordered):

The changes required are very minimal:

  • Introduce Tesla.client/1 and Tesla.client/2 (Should it be called Tesla.new?)
  • Update adapter README with configuration examples
  • Update README and examples (while keeping the DSL docs!)

---

WDYT?

@amatalai
Copy link
Collaborator

Introduce Tesla.client/1 and Tesla.client/2 (Should it be called Tesla.new?)

I vote for client. Tesla.new would be a good name if it was supposed to create %Tesla{} struct

Update README and examples (while keeping the DSL docs!)

Do we have a plan to deprecate/remove macros in the long run? Maybe something like:

  • 1.2 soft deprecation (explained in @doc)
  • 1.3 hard deprecation (@doc deprecated: "use ... instead)
  • 2.0 removed

[timeout] if the library does not implement it it will have to be implemented on the adapter level (most probably in a similar fashion as current timeout middleware).

Wouldn't it cause same issues as current timeout middleware?

@teamon
Copy link
Member Author

teamon commented Aug 29, 2018

Do we have a plan to deprecate/remove macros in the long run?

I think we can keep both, I don't have a strong urge to remove macros.

Wouldn't it cause same issues as current timeout middleware?

No, since in test you'd replace that adapter with Tesla.Mock (or other)

@teamon
Copy link
Member Author

teamon commented Sep 7, 2018

Regarding a well-defined adapter interface:

httpc return type:

{ok, Result} | {ok, saved_to_file} | {error, term()}

hackney return type:

{ok, integer(), list(), client_ref()} | {ok, integer(), list()} | {error, term()}

ibrowse return type:

{ok, Status, ResponseHeaders, ResponseBody} | {ibrowse_req_id, req_id()} | {error, term()}

Since none of the current adapters define proper error contract, I don't see how could tesla enforce one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants