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

Add default config in config.exs + ability to configure Tesla middleware #71

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

ggiill
Copy link

@ggiill ggiill commented Mar 13, 2023

Inspired by #42


Adding the ability to configure default options in config.exs:

Configure default settings in config/config.exs (optional).

# config/config.exs
config :ex_force, :api_version, "43.0"
config :ex_force, :adapter, {Tesla.Adapter.Hackney, [recv_timeout: 1_000]}

As well as the ability to pass in custom Tesla Middleware:

# config/config.exs
config :ex_force, :middleware, [
   Tesla.Middleware.Telemetry,
   {Tesla.Middleware.Timeout, timeout: :timer.seconds(1)}
 ]

@ggiill ggiill marked this pull request as ready for review March 13, 2023 21:21
@ggiill ggiill changed the title Add default config + ability to config Tesla middleware Add default config + ability to configure Tesla middleware Mar 14, 2023
@ggiill ggiill changed the title Add default config + ability to configure Tesla middleware Add default config in config.exs + ability to configure Tesla middleware Mar 14, 2023
@ggiill ggiill force-pushed the configure-tesla-middleware branch from 5039dbc to 1a835ce Compare March 14, 2023 03:10
Copy link
Owner

@chulkilee chulkilee left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually I think it's better to avoid config at all (I'd rather remove the default api version!) - and let each user decide how to use this library.

I see #71 and #42 were created to customize the Tesla middleware - I believe we should only change ExForce.Client.Tesla to take append_middleware, not introducing any other config.

lib/ex_force.ex Outdated
Comment on lines 17 to 29
Configure default settings in config/config.exs (optional).

```elixir
# config/config.exs

config :ex_force, :api_version, "43.0"
config :ex_force, :adapter, {Tesla.Adapter.Hackney, [recv_timeout: 1_000]}
config :ex_force, :middleware, [
Tesla.Middleware.Telemetry,
{Tesla.Middleware.Timeout, timeout: :timer.seconds(1)}
]
```

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd like to avoid adding package level config like this. This can be done by having own application config and wrapper around ExForce.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Many Elixir libraries support application-level config. This is optional and can be overridden for each client if need be.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated to:

# config/config.exs

config :ex_force, ExForce.Client.Tesla,
  api_version: "43.0",
  adapter: {Tesla.Adapter.Hackney, [recv_timeout: 1_000]},
  append_middleware: [
    Tesla.Middleware.Telemetry,
    {Tesla.Middleware.Timeout, timeout: :timer.seconds(1)}
  ]

],
Keyword.get(opts, :adapter)
)
custom_middleware = get_from_opts_or_config(opts, :middleware, [])
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This middleware is always appended - so it would be better to make that explicit. What would be the better option name? append_middleware?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure - I can change that name and also only scope it to ExForce.Client.Tesla.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated!

Comment on lines 56 to 57
defp get_headers(opts), do: Keyword.get(opts, :headers, [{"user-agent", @default_user_agent}])

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please refrain from moving this function around - as they're defined next to where its first use (not grouped by private funcs).

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure - I'll move this back.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved back.

Comment on lines 58 to 59
|> List.flatten()
|> Tesla.client(adapter)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's avoid using pipe here as we're not transforming a data here.

We can just ++ by enforcing passing list

  [
    {ExForce.Client.Tesla.Middleware,
      {instance_url, Keyword.get(opts, :api_version, @default_api_version)}}
    {Tesla.Middleware.Compression, format: "gzip"},
    {Tesla.Middleware.JSON, engine: Jason},
    {Tesla.Middleware.Headers, headers},
  ] ++ Keyword.get(opts, :append_middlewares, [])

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure - will update.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated.

adapter = get_from_opts_or_config(opts, :adapter)

[
{Tesla.Middleware.DecodeJson, engine: Jason},
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to change this order?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changing this order will fix #70

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

Successfully merging this pull request may close these issues.

2 participants