Skip to content

Commit

Permalink
reformat everything
Browse files Browse the repository at this point in the history
  • Loading branch information
agrafix committed Nov 7, 2021
1 parent a65c1e1 commit 6055362
Show file tree
Hide file tree
Showing 42 changed files with 3,017 additions and 2,547 deletions.
1 change: 1 addition & 0 deletions Spock-api-ghcjs/Setup.hs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
import Distribution.Simple

main = defaultMain
158 changes: 82 additions & 76 deletions Spock-api-ghcjs/src/Web/Spock/Api/Client.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,107 +7,113 @@
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
module Web.Spock.Api.Client
( callEndpoint, callEndpoint' )
where

import Web.Spock.Api
module Web.Spock.Api.Client (callEndpoint, callEndpoint') where

import Data.HVect
import JavaScript.Web.XMLHttpRequest
import qualified Data.Aeson as A
import qualified Data.ByteString.Lazy as BSL
import Data.HVect
import qualified Data.HVect as HV
import qualified Data.JSString as J
import qualified Data.JSString.Text as J
import qualified Data.Text.Encoding as T
import JavaScript.Web.XMLHttpRequest
import Web.Spock.Api

type Header = (J.JSString, J.JSString)

-- | Call an 'Endpoint' defined using the @Spock-api@ package passing extra headers
callEndpoint' ::
forall p i o. (HasRep (MaybeToList i), HasRep p)
=> Endpoint p i o
-> [Header]
-> HVectElim p (HVectElim (MaybeToList i) (IO (Maybe o)))
forall p i o.
(HasRep (MaybeToList i), HasRep p) =>
Endpoint p i o ->
[Header] ->
HVectElim p (HVectElim (MaybeToList i) (IO (Maybe o)))
callEndpoint' ep extraHeaders =
HV.curry $ \hv -> HV.curry (callEndpointCore' ep extraHeaders hv)
HV.curry $ \hv -> HV.curry (callEndpointCore' ep extraHeaders hv)

-- | Call an 'Endpoint' defined using the @Spock-api@ package
callEndpoint ::
forall p i o. (HasRep (MaybeToList i), HasRep p)
=> Endpoint p i o -> HVectElim p (HVectElim (MaybeToList i) (IO (Maybe o)))
forall p i o.
(HasRep (MaybeToList i), HasRep p) =>
Endpoint p i o ->
HVectElim p (HVectElim (MaybeToList i) (IO (Maybe o)))
callEndpoint ep = callEndpoint' ep []

data EndpointCall p i o
= EndpointCall
{ epc_point :: !(Endpoint p i o)
, epc_headers :: ![Header]
, epc_params :: !(HVect p)
, epc_body :: !(HVect (MaybeToList i))
}
data EndpointCall p i o = EndpointCall
{ epc_point :: !(Endpoint p i o),
epc_headers :: ![Header],
epc_params :: !(HVect p),
epc_body :: !(HVect (MaybeToList i))
}

callEndpointCore' ::
forall p i o.
Endpoint p i o
-> [Header]
-> HVect p
-> HVect (MaybeToList i)
-> IO (Maybe o)
forall p i o.
Endpoint p i o ->
[Header] ->
HVect p ->
HVect (MaybeToList i) ->
IO (Maybe o)
callEndpointCore' ep hdrs hv b = callEndpointCore (EndpointCall ep hdrs hv b)

callEndpointCore :: forall p i o. EndpointCall p i o -> IO (Maybe o)
callEndpointCore call =
case call of
EndpointCall (MethodPost Proxy path) hdrs params (body :&: HNil) ->
do let rt = J.textToJSString $ renderRoute path params
bodyText = J.textToJSString $ T.decodeUtf8 $ BSL.toStrict $ A.encode body
req =
Request
{ reqMethod = POST
, reqURI = rt
, reqLogin = Nothing
, reqHeaders = (("Content-Type", "application/json;charset=UTF-8") : hdrs)
, reqWithCredentials = False
, reqData = StringData bodyText
}
runJsonReq req
EndpointCall (MethodPut Proxy path) hdrs params (body :&: HNil) ->
do let rt = J.textToJSString $ renderRoute path params
bodyText = J.textToJSString $ T.decodeUtf8 $ BSL.toStrict $ A.encode body
req =
Request
{ reqMethod = PUT
, reqURI = rt
, reqLogin = Nothing
, reqHeaders = (("Content-Type", "application/json;charset=UTF-8") : hdrs)
, reqWithCredentials = False
, reqData = StringData bodyText
}
runJsonReq req
EndpointCall (MethodGet path) hdrs params HNil ->
do let rt = J.textToJSString $ renderRoute path params
req =
Request
{ reqMethod = GET
, reqURI = rt
, reqLogin = Nothing
, reqHeaders = hdrs
, reqWithCredentials = False
, reqData = NoData
}
runJsonReq req
case call of
EndpointCall (MethodPost Proxy path) hdrs params (body :&: HNil) ->
do
let rt = J.textToJSString $ renderRoute path params
bodyText = J.textToJSString $ T.decodeUtf8 $ BSL.toStrict $ A.encode body
req =
Request
{ reqMethod = POST,
reqURI = rt,
reqLogin = Nothing,
reqHeaders = (("Content-Type", "application/json;charset=UTF-8") : hdrs),
reqWithCredentials = False,
reqData = StringData bodyText
}
runJsonReq req
EndpointCall (MethodPut Proxy path) hdrs params (body :&: HNil) ->
do
let rt = J.textToJSString $ renderRoute path params
bodyText = J.textToJSString $ T.decodeUtf8 $ BSL.toStrict $ A.encode body
req =
Request
{ reqMethod = PUT,
reqURI = rt,
reqLogin = Nothing,
reqHeaders = (("Content-Type", "application/json;charset=UTF-8") : hdrs),
reqWithCredentials = False,
reqData = StringData bodyText
}
runJsonReq req
EndpointCall (MethodGet path) hdrs params HNil ->
do
let rt = J.textToJSString $ renderRoute path params
req =
Request
{ reqMethod = GET,
reqURI = rt,
reqLogin = Nothing,
reqHeaders = hdrs,
reqWithCredentials = False,
reqData = NoData
}
runJsonReq req

runJsonReq :: A.FromJSON o => Request -> IO (Maybe o)
runJsonReq req =
do response <- xhrText req
case (status response, contents response) of
(200, Just txt) ->
do let res = A.eitherDecodeStrict' (T.encodeUtf8 txt)
case res of
Left errMsg ->
do putStrLn errMsg
pure Nothing
Right val ->
pure (Just val)
_ -> pure Nothing
do
response <- xhrText req
case (status response, contents response) of
(200, Just txt) ->
do
let res = A.eitherDecodeStrict' (T.encodeUtf8 txt)
case res of
Left errMsg ->
do
putStrLn errMsg
pure Nothing
Right val ->
pure (Just val)
_ -> pure Nothing
1 change: 1 addition & 0 deletions Spock-api-server/Setup.hs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
import Distribution.Simple

main = defaultMain
83 changes: 42 additions & 41 deletions Spock-api-server/src/Web/Spock/Api/Server.hs
Original file line number Diff line number Diff line change
@@ -1,57 +1,58 @@
{-# LANGUAGE GADTs #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
module Web.Spock.Api.Server
( defEndpoint )
where

import Web.Spock.Api
import Web.Spock.Core
module Web.Spock.Api.Server (defEndpoint) where

import Control.Monad.Trans
import Data.HVect
import qualified Data.HVect as HV
import Web.Spock.Api
import Web.Spock.Core

-- | Wire an 'Endpoint' defined using the @Spock-api@ package
defEndpoint ::
forall p i o m ctx.
(MonadIO m, HasRep p)
=> Endpoint p i o
-> HVectElim p (HVectElim (MaybeToList i) (ActionCtxT ctx m o))
-> SpockCtxT ctx m ()
forall p i o m ctx.
(MonadIO m, HasRep p) =>
Endpoint p i o ->
HVectElim p (HVectElim (MaybeToList i) (ActionCtxT ctx m o)) ->
SpockCtxT ctx m ()
defEndpoint ep handler =
defEndpointCore (ep, step2)
where
step1 :: HVect p -> HVectElim (MaybeToList i) (ActionCtxT ctx m o)
step1 = HV.uncurry handler
defEndpointCore (ep, step2)
where
step1 :: HVect p -> HVectElim (MaybeToList i) (ActionCtxT ctx m o)
step1 = HV.uncurry handler

step2 :: HVect p -> HVect (MaybeToList i) -> ActionCtxT ctx m o
step2 p = HV.uncurry (step1 p)
step2 :: HVect p -> HVect (MaybeToList i) -> ActionCtxT ctx m o
step2 p = HV.uncurry (step1 p)

defEndpointCore ::
forall p i o m ctx.
(MonadIO m, HasRep p)
=> (Endpoint p i o, HVect p -> HVect (MaybeToList i) -> ActionCtxT ctx m o)
-> SpockCtxT ctx m ()
forall p i o m ctx.
(MonadIO m, HasRep p) =>
(Endpoint p i o, HVect p -> HVect (MaybeToList i) -> ActionCtxT ctx m o) ->
SpockCtxT ctx m ()
defEndpointCore t =
case t of
(MethodGet path, handler) ->
let pf :: HVect p -> ActionCtxT ctx m ()
pf args =
do r <- handler args HNil
json r
in get path (HV.curry pf)
(MethodPost _ path, handler) ->
let pf :: HVect p -> ActionCtxT ctx m ()
pf args =
do req <- jsonBody'
r <- handler args (req :&: HNil)
json r
in post path (HV.curry pf)
(MethodPut _ path, handler) ->
let pf :: HVect p -> ActionCtxT ctx m ()
pf args =
do req <- jsonBody'
r <- handler args (req :&: HNil)
json r
in put path (HV.curry pf)
case t of
(MethodGet path, handler) ->
let pf :: HVect p -> ActionCtxT ctx m ()
pf args =
do
r <- handler args HNil
json r
in get path (HV.curry pf)
(MethodPost _ path, handler) ->
let pf :: HVect p -> ActionCtxT ctx m ()
pf args =
do
req <- jsonBody'
r <- handler args (req :&: HNil)
json r
in post path (HV.curry pf)
(MethodPut _ path, handler) ->
let pf :: HVect p -> ActionCtxT ctx m ()
pf args =
do
req <- jsonBody'
r <- handler args (req :&: HNil)
json r
in put path (HV.curry pf)
1 change: 1 addition & 0 deletions Spock-api/Setup.hs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
import Distribution.Simple

main = defaultMain
38 changes: 25 additions & 13 deletions Spock-api/src/Web/Spock/Api.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,21 @@
{-# LANGUAGE GADTs #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE TypeFamilies #-}

module Web.Spock.Api
( Endpoint(..)
, Proxy(..)
, MaybeToList
, (<//>), var, Path(..), renderRoute
, Generic, ToJSON, FromJSON, NFData, Typeable
)
( Endpoint (..),
Proxy (..),
MaybeToList,
(<//>),
var,
Path (..),
renderRoute,
Generic,
ToJSON,
FromJSON,
NFData,
Typeable,
)
where

import Control.DeepSeq
Expand All @@ -24,10 +32,14 @@ import Web.Routing.Combinators

-- | Describes an endpoint with path parameters, an optional json body and a json response
data Endpoint (p :: [*]) (i :: Maybe *) (o :: *) where
MethodGet :: (ToJSON o, FromJSON o) => Path p 'Open -> Endpoint p 'Nothing o
MethodPost ::
(ToJSON i, FromJSON i, ToJSON o, FromJSON o)
=> Proxy (i -> o) -> Path p 'Open -> Endpoint p ('Just i) o
MethodPut ::
(ToJSON i, FromJSON i, ToJSON o, FromJSON o)
=> Proxy (i -> o) -> Path p 'Open -> Endpoint p ('Just i) o
MethodGet :: (ToJSON o, FromJSON o) => Path p 'Open -> Endpoint p 'Nothing o
MethodPost ::
(ToJSON i, FromJSON i, ToJSON o, FromJSON o) =>
Proxy (i -> o) ->
Path p 'Open ->
Endpoint p ('Just i) o
MethodPut ::
(ToJSON i, FromJSON i, ToJSON o, FromJSON o) =>
Proxy (i -> o) ->
Path p 'Open ->
Endpoint p ('Just i) o
1 change: 1 addition & 0 deletions Spock-core/Setup.hs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
import Distribution.Simple

main = defaultMain
Loading

0 comments on commit 6055362

Please sign in to comment.