Giter VIP home page Giter VIP logo

Comments (4)

teamon avatar teamon commented on July 29, 2024

Hi!

Can you show an example of your code? I don't think we can skip the %Tesla.Client{} match since this is the way we can achieve current api - get/2 (path, opts) vs get/2 (client, path)

from tesla.

christhekeele avatar christhekeele commented on July 29, 2024

To clarify, the point of this wouldn't be to skip the %Tesla.Client{} match, it would be to allow you to modify that match to use a different struct name, so that different get/2 functions in different modules that both use Tesla will refuse to use each-other's clients.

Currently, this works fine, because APITwo.get/2 matches on %Tesla.Client{} returned by APIOne.client:

defmodule APIOne do
  use Tesla
  def client do
    Tesla.build_client [
      {Tesla.Middleware.Headers, %{"Authorization" => "token: " <> token }}
    ]
  end
end

defmodule APITwo, do: use Tesla

APITwo.get(APIOne.client, "foo")

My proposal is to allow Tesla to generate client structs 'private' to a particular invocation of use Tesla, such that:

defmodule APIOne do
  use Tesla, client: APIOne.Client
  def client do
    APIOne.Client.build [
      {Tesla.Middleware.Headers, %{"Authorization" => "token: " <> token }}
    ]
  end
end

defmodule APITwo, do: use Tesla, client: APITwo.Client

APITwo.get(APIOne.client, "foo")

raises:

** (FunctionClauseError) no function clause matching in APITwo.get/2

    The following arguments were given to APITwo.get/2:

        # 1
        %APIOne.Client{...}

        # 2
        "foo"

    iex:1: Foo.bar/1

Because, since APIOne's use Tesla was given a client struct name, it generated a client struct with that name (%APIOne.Client{}) , and because APITwo was also given a client struct name, it generated a APITwo.get/2 that is matching on that specific struct (%APITwo.Client{}) instead of any old %Tesla.Client{}.

from tesla.

christhekeele avatar christhekeele commented on July 29, 2024

Here, this is kind of what I was envisioning.

from tesla.

teamon avatar teamon commented on July 29, 2024

Closing in favour of #133

from tesla.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.