Proposal: URI.join_path/2

34 views
Skip to first unread message

aaron....@gmail.com

unread,
Dec 13, 2022, 3:58:33 PM12/13/22
to elixir-lang-core
Hi Everyone,

I have repeatedly run into scenarios when building/testing API clients where I want to join a path onto a base url. The base_url could be configurable (and maybe read from an environment variable) and the user may or may not append a trailing slash to it. Here's the problem I'm trying to solve:

Base URI: http://example.com/business_1
Path to append: /foo
Desired result: http://example.com/business_1/foo

Base URI: http://example.com/business_1/ # Includes trailing slash
Path to append: /foo
Desired result: http://example.com/business_1/foo

Path to append: /foo
Desired result: http://example.com/foo

Base URI: http://example.com/ # Includes trailing slash
Path to append: /foo
Desired result: http://example.com/foo

I don't see a helper to quickly do this and URI.merge/2 replaces the path completely. What do you think about `URI.join_path/2`? You should be able to do something like this.

iex> URI.join_path("http://example.com/foo/", "/bar") |> to_string()
"http://example.com/foo/bar"


I guess it would probably leave any query string variables untouched

iex> URI.join_path("http://example.com/foo/?var1=true", "/bar") |> to_string()
"http://example.com/foo/bar?var=1"


And maybe it would allow you to join multiple fragments like Path.join/1:

iex> URI.join_path("http://example.com/foo/", ["bar", "baz"]) |> to_string()
"http://example.com/foo/bar/baz"

What do you think? Is there a better way to do this that I haven't figured out?

Aaron Rener

José Valim

unread,
Dec 13, 2022, 4:03:20 PM12/13/22
to elixir-l...@googlegroups.com
We recently added append_query, so I am in favor of an append_path. PR welcome!

--
You received this message because you are subscribed to the Google Groups "elixir-lang-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-co...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/3573e832-c6d5-4154-95b4-b857c3850319n%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages