Skip to content

Commit

Permalink
auth client ok
Browse files Browse the repository at this point in the history
  • Loading branch information
Manuel Leduc committed May 3, 2016
1 parent 0be4657 commit 53316d5
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 4 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
/.stack-work
/dist
/*.iml
/out
/.idea
13 changes: 9 additions & 4 deletions kreg.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ name: kreg
version: 0.1.0.0
synopsis: Initial project template from stack
description: Please see README.md
homepage: https://github.com/Daroth/kreg#readme
homepage: https://github.com/manuelleduc/kreg-haskell#readme
license: BSD3
license-file: LICENSE
author: Daroth
maintainer: [email protected]
author: Manuel Leduc
maintainer: [email protected]
copyright: beerware
category: draft
build-type: Simple
Expand All @@ -17,6 +17,10 @@ library
hs-source-dirs: src
exposed-modules: Lib
build-depends: base >= 4.7 && < 5
, wreq
, http-client
, aeson
, lens
default-language: Haskell2010

executable kreg-exe
Expand All @@ -27,6 +31,7 @@ executable kreg-exe
, kreg
, optparse-generic
, haskeline

default-language: Haskell2010

test-suite kreg-test
Expand All @@ -40,4 +45,4 @@ test-suite kreg-test

source-repository head
type: git
location: https://github.com/Daroth/kreg
location: https://github.com/manuelleduc/kreg-haskell
2 changes: 2 additions & 0 deletions src/Lib.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,7 @@ module Lib
( someFunc
) where

import Registry.Client (auth)

someFunc :: IO ()
someFunc = putStrLn "someFunc"
37 changes: 37 additions & 0 deletions src/Registry/Client.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE ScopedTypeVariables #-}

module Registry.Client (auth, AuthResponseBody(..)) where

import GHC.Generics
import Data.Aeson
import qualified Network.Wreq as R
import Control.Lens ((^?))
import Control.Exception as E
import Network.HTTP.Client (HttpException(StatusCodeException))

data Auth = Auth {
username :: String,
password :: String,
rememberMe :: Bool
} deriving (Generic)

data AuthResponseBody = AuthResponseBody {
id_token :: String
} deriving (Generic, Show)

instance ToJSON Auth
instance FromJSON AuthResponseBody

baseUrlServer = "http://localhost:8080/"
authenticateResource = baseUrlServer ++ "api/authenticate"

--auth :: String -> String -> IO (Maybe AuthResponseBody)
auth login password =
let handler e@(StatusCodeException _ _ _) = return Nothing
authObj = Auth login password False
operations = do r <- R.post authenticateResource (toJSON authObj)
(body :: R.Response AuthResponseBody) <- R.asJSON r
return (body ^? R.responseBody)
in operations `E.catch` handler

0 comments on commit 53316d5

Please sign in to comment.