summaryrefslogtreecommitdiff
path: root/Network/Pastis.hs
blob: 83a2fa94e383395de62839be8a9b399a772658ef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
module Network.Pastis (pastisURL) where

import Network.HTTP
import Network.URI

-- | Use pastisURL to shorten a URL.
pastisURL :: String -> IO String
pastisURL url = fmap (either (const url) rspBody) (simpleHTTP request) `catch` (return . const url)
  where request = Request { rqURI = uri
                          , rqMethod = GET
                          , rqHeaders = []
                          , rqBody = "" }
        uri = URI { uriScheme = "http:"
                  , uriAuthority = Just $ URIAuth { uriUserInfo = "", uriRegName = "past.is", uriPort = "" }
                  , uriPath = "/api/"
                  , uriQuery = "?format=simple&url=" ++ escapeURIString isUnreserved url
                  , uriFragment = "" }