1- {-# LANGUAGE OverloadedStrings, StandaloneDeriving #-}
1+ {-# LANGUAGE OverloadedStrings, StandaloneDeriving, DeriveDataTypeable #-}
22module Github.Private where
33
44import Github.Data
55import Data.Aeson
66import Data.Attoparsec.ByteString.Lazy
7+ import Data.Data
78import Control.Applicative
89import Data.List
10+ import Data.CaseInsensitive (mk )
911import qualified Data.ByteString.Char8 as BS
1012import qualified Data.ByteString.Lazy.Char8 as LBS
1113import Network.HTTP.Types (Method , Status (.. ))
@@ -14,10 +16,15 @@ import Data.Conduit (ResourceT)
1416import qualified Control.Exception as E
1517import Data.Maybe (fromMaybe )
1618
19+ -- | user/password for HTTP basic access authentication
20+ data GithubAuth = GithubBasicAuth BS. ByteString BS. ByteString
21+ | GithubOAuth String
22+ deriving (Show , Data , Typeable , Eq , Ord )
23+
1724githubGet :: (FromJSON b , Show b ) => [String ] -> IO (Either Error b )
1825githubGet = githubGet' Nothing
1926
20- githubGet' :: (FromJSON b , Show b ) => Maybe BasicAuth -> [String ] -> IO (Either Error b )
27+ githubGet' :: (FromJSON b , Show b ) => Maybe GithubAuth -> [String ] -> IO (Either Error b )
2128githubGet' auth paths =
2229 githubAPI (BS. pack " GET" )
2330 (buildUrl paths)
@@ -27,21 +34,21 @@ githubGet' auth paths =
2734githubGetWithQueryString :: (FromJSON b , Show b ) => [String ] -> String -> IO (Either Error b )
2835githubGetWithQueryString = githubGetWithQueryString' Nothing
2936
30- githubGetWithQueryString' :: (FromJSON b , Show b ) => Maybe BasicAuth -> [String ] -> String -> IO (Either Error b )
37+ githubGetWithQueryString' :: (FromJSON b , Show b ) => Maybe GithubAuth -> [String ] -> String -> IO (Either Error b )
3138githubGetWithQueryString' auth paths queryString =
3239 githubAPI (BS. pack " GET" )
3340 (buildUrl paths ++ " ?" ++ queryString)
3441 auth
3542 (Nothing :: Maybe Value )
3643
37- githubPost :: (ToJSON a , Show a , FromJSON b , Show b ) => BasicAuth -> [String ] -> a -> IO (Either Error b )
44+ githubPost :: (ToJSON a , Show a , FromJSON b , Show b ) => GithubAuth -> [String ] -> a -> IO (Either Error b )
3845githubPost auth paths body =
3946 githubAPI (BS. pack " POST" )
4047 (buildUrl paths)
4148 (Just auth)
4249 (Just body)
4350
44- githubPatch :: (ToJSON a , Show a , FromJSON b , Show b ) => BasicAuth -> [String ] -> a -> IO (Either Error b )
51+ githubPatch :: (ToJSON a , Show a , FromJSON b , Show b ) => GithubAuth -> [String ] -> a -> IO (Either Error b )
4552githubPatch auth paths body =
4653 githubAPI (BS. pack " PATCH" )
4754 (buildUrl paths)
@@ -51,39 +58,44 @@ githubPatch auth paths body =
5158buildUrl :: [String ] -> String
5259buildUrl paths = " https://api.github.com/" ++ intercalate " /" paths
5360
54- githubAPI :: (ToJSON a , Show a , FromJSON b , Show b ) => BS. ByteString -> String -> Maybe BasicAuth -> Maybe a -> IO (Either Error b )
61+ githubAPI :: (ToJSON a , Show a , FromJSON b , Show b ) => BS. ByteString -> String -> Maybe GithubAuth -> Maybe a -> IO (Either Error b )
5562githubAPI method url auth body = do
5663 result <- doHttps method url auth (Just encodedBody)
5764 return $ either (Left . HTTPConnectionError )
5865 (parseJson . responseBody)
5966 result
6067 where encodedBody = RequestBodyLBS $ encode $ toJSON body
6168
62- -- | user/password for HTTP basic access authentication
63- type BasicAuth = (BS. ByteString , BS. ByteString )
64-
65- doHttps :: Method -> String -> Maybe BasicAuth -> Maybe (RequestBody (ResourceT IO )) -> IO (Either E. SomeException (Response LBS. ByteString ))
69+ doHttps :: Method -> String -> Maybe GithubAuth -> Maybe (RequestBody (ResourceT IO )) -> IO (Either E. SomeException (Response LBS. ByteString ))
6670doHttps method url auth body = do
6771 let requestBody = fromMaybe (RequestBodyBS $ BS. pack " " ) body
72+ requestHeaders = maybe [] getOAuth auth
6873 (Just uri) = parseUrl url
6974 request = uri { method = method
7075 , secure = True
7176 , port = 443
7277 , requestBody = requestBody
78+ , requestHeaders = requestHeaders
7379 , checkStatus = successOrMissing
7480 }
75- authRequest = maybe id ( uncurry applyBasicAuth) auth request
81+ authRequest = getAuthRequest auth request
7682
7783 (getResponse authRequest >>= return . Right ) `E.catches` [
7884 -- Re-throw AsyncException, otherwise execution will not terminate on
7985 -- SIGINT (ctrl-c). All AsyncExceptions are re-thrown (not just
8086 -- UserInterrupt) because all of them indicate severe conditions and
8187 -- should not occur during normal operation.
8288 E. Handler (\ e -> E. throw (e :: E. AsyncException )),
83-
8489 E. Handler (\ e -> (return . Left ) (e :: E. SomeException ))
8590 ]
8691 where
92+ getAuthRequest (Just (GithubBasicAuth user pass)) = applyBasicAuth user pass
93+ getAuthRequest _ = id
94+ getBasicAuth (GithubBasicAuth user pass) = applyBasicAuth user pass
95+ getBasicAuth _ = id
96+ getOAuth (GithubOAuth token) = [(mk (BS. pack " Authorization" ),
97+ BS. pack (" token " ++ token))]
98+ getOAuth _ = []
8799 getResponse request = withManager $ \ manager -> httpLbs request manager
88100 successOrMissing s@ (Status sci _) hs
89101 | (200 <= sci && sci < 300 ) || sci == 404 = Nothing
0 commit comments