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

Extend convenience functions to take arbitrary headers #88

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

Conversation

buckie
Copy link

@buckie buckie commented May 12, 2015

I'd like to extend the convenience functions to take arbitrary headers. This use case is motivated by an issue @cartazio & I ran into at work. I wanted to add a --header flag to a program we've written so that I could add some random header to hit an internal system's URL. As we were using get I needed to replace the convenience function with the more general solution http-streams provides. It would have been really handy to just be able to tack on an extra header name=value.

Please let me know if you want anything further on the pull.

-Will

buckie added 2 commits May 11, 2015 23:24
…s we are stuck in enterprise land and need wierd headers

Signed-off-by: buckie <[email protected]>
@istathar
Copy link
Member

Hi @buckie. Nice to see you.

In general, I'm not keen on this for the same reason I'm not keen on #64 — I don't want to exponentially grow the size of the public API to cater for an ever growing list of custom use cases.

Now, that said, what you're asking for (and what @kosmoskatten is asking for) is not unreasonable. So what I'd like to do is figure out an API that retains the simplicity (if I've earned that title) that http-streams has presently while supporting more complex uses.

(ironically, httpie (https://github.com/jakubroztocil/httpie) has exactly the usability you're looking for: on the command line you can say

$ http GET http://www.example.com/about X-Biscuit-Flavour:chocolate

to set a header field; key=value does query string / form / JSON parameter submission, etc. It's pretty nice. The strongly typed....

Ahhhh. Maybe we leave get alone and give you an universalGeneralizedSuperDouperHttp (since http is taken) function that takes a [(Field,Value)] list, and a configuration object, and who knows what else).

Or maybe we just figure out a new API for this library entirely.

AfC

@kosmoskatten
Copy link

Hi,

Currently I'm using a modified http-streams, and I think one of my basic needs can be boiled down to a slight modification of the connectSocket to enable modification of the (not yet connected) socket. The beginning of my modified function looks like:

connectSocket :: Hostname -> Port -> (Socket -> IO Socket) -> IO Socket
connectSocket h1' p modify = do
    is <- getAddrInfo (Just hints) (Just h1) (Just $ show p)
    let addr = head is
    let a = addrAddress addr
    s <- modify =<< socket (addrFamily addr) Stream defaultProtocol
    connect s a
    return s

The other need I have is to manipulate the io-streams. But if the API is "open" enough to allow me to tailor my own openConnection like functions with the official version of the library I think it's sufficient. Currently I use something like:

openByteCountedConnection :: Hostname -> Port -> IO ByteCountedConnection
openByteCountedConnection host port = do
  sock         <- connectSocket host port return
  streams      <- Streams.socketToStreams sock
  (ins, inc)   <- Streams.countInput (fst streams)
  (outs, outc) <- Streams.countOutput (snd streams)
  conn         <- makeConnection (modifyHostname host port) (close sock)
                                 outs ins
  return (conn, inc, outc)

Then, of course, I have my own withByteCountedConnection etc. So I think my needs can be implemented with very little impact in then http-streams.

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.

3 participants