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

[Proposal] Allow adapter specific data to be stored inside Tesla.Env #48

Closed
amatalai opened this issue Jan 25, 2017 · 10 comments
Closed

Comments

@amatalai
Copy link
Collaborator

Example:

#little change to adapter
defmodule Tesla.Adapter.Hackney do
    def call(env, opts) do
      with  {:ok, status, headers, body, location} <- request(env, opts || []) do
        %{env | status:   status,
                headers:  headers,
                body:     body,
                private: %{final_location: location}}
      end
    end

    defp handle({:ok, status, headers, ref}) do
      with  location <> "" <- :hackney.location(ref),
               {:ok, body} <- :hackney.body(ref) do
        {:ok, status, headers, body, location}
      end
    end
  end
#usage
iex(1)> Tesla.get("https://github.com/teamon/tesla", opts: [follow_redirect: true])
%Tesla.Env{
  url: "https://github.com/teamon/tesla",
  private: %{final_location:  "https://github.com/teamon/tesla"}
}

iex(2)> Tesla.get("https://goo.gl/8hfJ7F", opts: [follow_redirect: true])
%Tesla.Env{
  url: "https://goo.gl/8hfJ7F",
  private: %{final_location:  "https://github.com/teamon/tesla"}
}
@teamon
Copy link
Member

teamon commented Jan 25, 2017 via email

@amatalai
Copy link
Collaborator Author

amatalai commented Jan 25, 2017

Sure, opts could be used, but I think it's would be cleaner to not mix different entities under same key.

For me :opts is write-only for user and read-only for Tesla when :private will be opposite.
What do I mean by this?

When you do Tesla.get(url, opts: [something: true]) you probably don't care about what's saved under :opts key in response, because you know what you have passed in function call. On the other hand Tesla shouldn't use data from :private when making request, because this field would allow to store inconsistent data(specific to one adapter/middleware), which from end user perspective may be useful.

#some pseudocode to make this 100% clear

important_in_request = [follow_redirect: true]
ignored_by_tesla = [random_key: :value]

%Tesla.Env{
   opts: _user_probably_dont_care,
   private: additional_data_that_user_may_be_interested_in
} = Tesla.get(url, opts: important_in_request, private: ignored_by_tesla)

@teamon
Copy link
Member

teamon commented Jan 25, 2017

I agree mixing opts with private might not be a good idea.

One question remains - what should be the desired adapter's behaviour and rules for using private properties? (Assuming client code does not modify adapters)

In case of "follow redirects" I think this should be a tesla middleware like this

@amatalai
Copy link
Collaborator Author

My first idea for private properties can be described as "allow to store anything useful, don't merge to master anything that tries to read from it". I'll think about it more later, but unfortunately I'm little busy at the moment.

about FollowRedirects middleware
I like this, it will allow me to remove custom implementation from my library
one note: httpc default for autoredirect is true, you should probably disable it inside this middleware

btw I made middleware for data compression
https://github.com/amatalai/link_preview/blob/1.0.0/lib/compression_middleware.ex
It's work in progress now, but I think that I'll have time to finish it in next few days (I'll make separate issue tomorrow to discuss details)

@teamon
Copy link
Member

teamon commented Apr 5, 2017

@amatalai sooooo, what are we doing with this? ;)

@amatalai
Copy link
Collaborator Author

amatalai commented Apr 5, 2017

I wish I could create PR with some proposal to push work on this forward, but lately I don't have time to do that.

@amatalai amatalai self-assigned this Sep 11, 2017
@amatalai
Copy link
Collaborator Author

I need to revisit this idea. With ability to add middleware before adapter it should be easy to save raw headers etc with simple middleware and without any breaking changes.

@teamon
Copy link
Member

teamon commented Sep 14, 2017

As mentioned in #104 there was always a possibility to add something right around adapter :)

@teamon
Copy link
Member

teamon commented Oct 7, 2017

Please see possible solution in #37

@amatalai
Copy link
Collaborator Author

amatalai commented Oct 7, 2017

Yeah, this wouldn't be useful at the moment

@amatalai amatalai closed this as completed Oct 7, 2017
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