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

Further improve Cohttp_eio.Client ergonomics #958

Merged
merged 3 commits into from
Dec 22, 2022

Conversation

bikallem
Copy link
Contributor

@bikallem bikallem commented Dec 22, 2022

While working on documentation for cohttp-eio and #957, it dawned on me that the Cohttp_eio.Client ergonomics could be further improved. Such that users of Cohttp_eio.Client don't even need to use Eio.Net.with_tcp_connect for most use cases. This PR aims to do just that. Now we do,

open Cohttp_eio

let () =
  Eio_main.run @@ fun env ->
  let res = Client.get env ~host:"www.example.org" "/" in
  print_string @@ Client.read_fixed res

rather than a bit verbose below:

open Cohttp_eio

let () =
  let host, port = ("www.example.org", 80) in
  Eio_main.run @@ fun env ->
  Eio.Net.with_tcp_connect ~host ~service:(string_of_int port) env#net
    (fun conn ->
      let host = (host, Some port) in
      let res = Client.get ~conn host "/" in
      print_string @@ Client.read_fixed res)

Note, however, we still retain the ability to pass in the flow/connection parameter like before. It is however now an optional parameter rather than a required one.

?conn:(#Eio.Flow.two_way as 'a) ->
?port:port ->
'b env ->
host:host ->
Copy link
Collaborator

Choose a reason for hiding this comment

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

why did we decide to not use uri here?
A uri will contain all the information at once

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not really sure now but if I remember right being able to use the flow/connection was one I think. It wasn't obvious to me then but now I think it was the correct design. Additionally, I believe we can't use unix domain sockets in url without inventing our own syntax since http uri spec. doesn't specify how we can use unix domain socket scheme in the uri.

Copy link
Collaborator

Choose a reason for hiding this comment

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

That makes sense, the main reason I am asking is because right now it feels weird to separate the host from the path (mostly because any other library will take that all at once) but this is surely an improvement over the previous version

Copy link
Collaborator

Choose a reason for hiding this comment

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

Should we try to be smart and add / as the first character if not already present?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Should we try to be smart and add / as the first character if not already present?

I think that is a good idea but not sure atm. However, I would like to address that in future PR please if we do go down that route.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have created a new issue so that we can discuss it further. #959

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