Giter VIP home page Giter VIP logo

Comments (7)

teamon avatar teamon commented on July 29, 2024

I'm not sure what do you mean. I've checked and everything seems to be fine.

Given such two modules:

defmodule X do
  use Tesla

  plug Tesla.Middleware.BaseUrl, "http://httpbin.org"

  def new do
    Tesla.build_client [
      {Tesla.Middleware.Tuples, nil}
    ]
  end
end

defmodule Y do
  use Tesla

  plug Tesla.Middleware.Tuples

  def new do
    Tesla.build_client [
      {Tesla.Middleware.BaseUrl, "http://httpbin.org"}
    ]
  end
end
iex(1)> X.get("/ip")
%Tesla.Env{url: "http://httpbin.org/ip", ...}
# only BaseUrl used

iex(2)> X.new() |> X.get("/ip")
{:ok,
 %Tesla.Env{url: "http://httpbin.org/ip"}, ...}
# Both Tuples and BaseUrl used

iex(3)> Y.get("/ip")
{:error, %Tesla.Error{message: "adapter error: :no_scheme", reason: :no_scheme}}
# Only Tuples used, hence error since "/ip" is not a valid URL

iex(4)> Y.new() |> Y.get("/ip")
{:ok,
 %Tesla.Env{url: "http://httpbin.org/ip", ...}}
# Both middlewares used

from tesla.

ijcd avatar ijcd commented on July 29, 2024

from tesla.

ijcd avatar ijcd commented on July 29, 2024

Ok, I was able to reproduce it, but I'm not convinced it's an error. Let me know what you think. I have something that looks like this:

defmodule Z do
  use Tesla

  plug Tesla.Middleware.Tuples
  plug Tesla.Middleware.Headers, %{"User-Agent" => "MyConn", "Content-Type" => "application/json"}

  def client(base_url) do
    Tesla.build_client [
      {Tesla.Middleware.BaseUrl, base_url}
    ]
  end
end

client = Z.client("http://httpbin.org")

client |> Tesla.request(method: :get, url: "/ip")
|> IO.inspect

You'll notice that I am calling Tesla.request instead of Z.request. If I call the latter it works. The former does not.

I wanted a method that would allow me to specify requests as data and pass then very late to the execution method, so I sought out the request method (method can be data). I found it in the Tesla module and didn't realize I should use my module to call it.

from tesla.

amatalai avatar amatalai commented on July 29, 2024

Yup, it works this way.

I think we could add something like "common mistakes" to readme. It's not first time when someone uses http functions directly from Tesla module 😢

from tesla.

ijcd avatar ijcd commented on July 29, 2024

from tesla.

amatalai avatar amatalai commented on July 29, 2024

Direct usage serves different purpose than generated module. For example when I want to check output of my own endpoint I run quick Tesla.get("http:://localhost:4000/api/something") in iex. We should not threat this as something bad.

I'm still more convinced to readme/docs improvements. I just need more time to think about it.

from tesla.

teamon avatar teamon commented on July 29, 2024

Closing this for now, I'm not sure we can do much about it.

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.