Page MenuHomePhorge

No OneTemporary

Size
1 KB
Referenced Files
None
Subscribers
None
diff --git a/lib/tesla/middleware/path_params.ex b/lib/tesla/middleware/path_params.ex
index 059b7f3..8b27dbb 100644
--- a/lib/tesla/middleware/path_params.ex
+++ b/lib/tesla/middleware/path_params.ex
@@ -1,42 +1,42 @@
defmodule Tesla.Middleware.PathParams do
@moduledoc """
Use templated URLs with separate params.
Useful when logging or reporting metric per URL.
## Example usage
```
defmodule MyClient do
use Tesla
- plug Tesla.Middleware.BaseURl, "https://api.example.com"
+ plug Tesla.Middleware.BaseUrl, "https://api.example.com"
plug Tesla.Middleware.Logger # or some monitoring middleware
plug Tesla.Middleware.PathParams
def user(id) do
params = [id: id]
get("/users/:id", opts: [path_params: params])
end
end
```
"""
@behaviour Tesla.Middleware
@rx ~r/:([a-zA-Z]{1}[\w_]*)/
@impl Tesla.Middleware
def call(env, next, _) do
url = build_url(env.url, env.opts[:path_params])
Tesla.run(%{env | url: url}, next)
end
defp build_url(url, nil), do: url
defp build_url(url, params) do
Regex.replace(@rx, url, fn match, key ->
to_string(params[String.to_existing_atom(key)] || match)
end)
end
end

File Metadata

Mime Type
text/x-diff
Expires
Sun, Nov 24, 5:44 AM (22 h, 23 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
39197
Default Alt Text
(1 KB)

Event Timeline