File: Pastis.hs

package info (click to toggle)
haskell-pastis 0.1.2-5
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 72 kB
  • sloc: haskell: 18; makefile: 2
file content (20 lines) | stat: -rw-r--r-- 886 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
{-# LANGUAGE ScopedTypeVariables #-}

module Network.Pastis (pastisURL) where

import Control.Exception.Base (catch, IOException)
import Network.HTTP
import Network.URI

-- | Use pastisURL to shorten a URL. If an error occurs, the function returns 'url'.
pastisURL :: String -> IO String
pastisURL url = fmap (either (const url) rspBody) (simpleHTTP request) `catch` (\(_ :: IOException) -> return 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 = "" }