caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Using Uri with Google Distance Matrix API
@ 2013-10-28 21:58 O Frolovs
  2013-10-28 22:26 ` David Sheets
  0 siblings, 1 reply; 2+ messages in thread
From: O Frolovs @ 2013-10-28 21:58 UTC (permalink / raw)
  To: caml-list

Hello

I'm learning OCaml and currently trying to use it for my project at uni.

Specifically, i’m trying to use Uri [1] to create a Google Distance
Matrix request and i find it a bit awkward since the API uses pipe
character "|" for separating parameter values but Uri uses commas ","
and i could not find a way to override it:

utop # open Core.Std;;
utop # #require "uri";;

utop # let x = Uri.of_string "http://www.github.com/";;
val x : Uri.t = <abstr>

utop # let y = Uri.add_query_param x ("origins", ["Bristol";
"Cambridge"; "Plymouth"; "London"]);;
val y : Uri.t = <abstr>

utop # Uri.to_string y;;
- : string = "http://www.github.com/?origins=Bristol,Cambridge,Plymouth,London"

This is not what the API expects.

I also tried concatenating the origins values before passing it to Uri
but as you can see below, Uri performed character escape on pipe
characters. So besides not looking elegant, it did not work:

utop # String.concat ~sep:"|" ["Bristol"; "Cambridge"; "Plymouth"; "London"];;
- : string = "Bristol|Cambridge|Plymouth|London"

utop # let y = Uri.add_query_param x ("origins", [c]);;
val y : Uri.t = <abstr>

utop # Uri.to_string y;;
- : string = "http://www.github.com/?origins=Bristol%7CCambridge%7CPlymouth%7CLondon"

I am wondering

(a) if there is a way to make Uri do what i want, that is to use pipe
character to separate the origins values.

(b) why Uri does not have an optional ~sep parameter, like Jane
Street's Core.String.concat does?

Help?!

-- Ollie

[1] http://www.cl.cam.ac.uk/projects/ocamllabs/pkg/uri/1.3.8/index.html
[2] https://developers.google.com/maps/documentation/distancematrix/

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [Caml-list] Using Uri with Google Distance Matrix API
  2013-10-28 21:58 [Caml-list] Using Uri with Google Distance Matrix API O Frolovs
@ 2013-10-28 22:26 ` David Sheets
  0 siblings, 0 replies; 2+ messages in thread
From: David Sheets @ 2013-10-28 22:26 UTC (permalink / raw)
  To: ollie.frolovs.2012; +Cc: O Caml

On Mon, Oct 28, 2013 at 9:58 PM, O Frolovs
<ollie.frolovs.2012@my.bristol.ac.uk> wrote:
> Hello

Hi Ollie,

> I'm learning OCaml and currently trying to use it for my project at uni.
>
> Specifically, i’m trying to use Uri [1] to create a Google Distance
> Matrix request and i find it a bit awkward since the API uses pipe
> character "|" for separating parameter values but Uri uses commas ","
> and i could not find a way to override it:

Unfortunately, the Google Distance Matrix API does not conform to the
Internet Standard for URIs. Specifically, the pipe character "|" is
not allowed in a well-formed URI. I'm not sure why Google has decided
to use this character out of the very many separators available to
them.

> utop # open Core.Std;;
> utop # #require "uri";;
>
> utop # let x = Uri.of_string "http://www.github.com/";;
> val x : Uri.t = <abstr>
>
> utop # let y = Uri.add_query_param x ("origins", ["Bristol";
> "Cambridge"; "Plymouth"; "London"]);;
> val y : Uri.t = <abstr>
>
> utop # Uri.to_string y;;
> - : string = "http://www.github.com/?origins=Bristol,Cambridge,Plymouth,London"
>
> This is not what the API expects.
>
> I also tried concatenating the origins values before passing it to Uri
> but as you can see below, Uri performed character escape on pipe
> characters. So besides not looking elegant, it did not work:
>
> utop # String.concat ~sep:"|" ["Bristol"; "Cambridge"; "Plymouth"; "London"];;
> - : string = "Bristol|Cambridge|Plymouth|London"
>
> utop # let y = Uri.add_query_param x ("origins", [c]);;
> val y : Uri.t = <abstr>
>
> utop # Uri.to_string y;;
> - : string = "http://www.github.com/?origins=Bristol%7CCambridge%7CPlymouth%7CLondon"
>
> I am wondering
>
> (a) if there is a way to make Uri do what i want, that is to use pipe
> character to separate the origins values.

I do not believe there is a way to accomplish this in Uri 1.3.8. Have
you tried submitting the percent-encoded query string to the API to
see if Google properly interprets the escaping of invalid characters?

Uri is designed to take as input any string and produce as output only
those strings that are conformant to RFC 3986.

> (b) why Uri does not have an optional ~sep parameter, like Jane
> Street's Core.String.concat does?

No one has implemented it yet. :-) There has been discussion about
significantly changing this API feature for 1.4 as well.

If you are not manipulating the URIs heavily and are not in need of
input sanitation, using Uri may not be necessary unless another
library requires input of Uri.t values.

If none of this helps you, please create an issue at
<https://github.com/mirage/ocaml-uri/issues?state=open> and we may end
up adding something to help you in the next revision (could be later
this week depending on how persuasive you are and how extensive the
feature).

Hope this helps,

David

> Help?!
>
> -- Ollie
>
> [1] http://www.cl.cam.ac.uk/projects/ocamllabs/pkg/uri/1.3.8/index.html
> [2] https://developers.google.com/maps/documentation/distancematrix/
>
> --
> Caml-list mailing list.  Subscription management and archives:
> https://sympa.inria.fr/sympa/arc/caml-list
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> Bug reports: http://caml.inria.fr/bin/caml-bugs

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2013-10-28 22:26 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-10-28 21:58 [Caml-list] Using Uri with Google Distance Matrix API O Frolovs
2013-10-28 22:26 ` David Sheets

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).