-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathTypes.hs
86 lines (63 loc) · 1.83 KB
/
Types.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TypeOperators #-}
module OwlCloud.Types where
import Data.Aeson
import Import
import Servant.API
type OwlCloudAPI = UsersAPI :<|> AlbumsAPI
-- * Services
-- ** Users microservice
type UsersAPI =
"api" :> "users" :> "owl-in" :> ReqBody '[JSON] LoginReq
:> Post '[JSON] SigninToken
:<|>
"api" :> "users"
:> Authorized ("owl-out" :> Post '[JSON] ())
:<|>
"private-api" :> "users" :> "token-validity" :> Capture "token" SigninToken
:> Get '[JSON] TokenValidity
newtype SigninToken =
SigninToken Text
deriving (ToJSON, FromJSON, FromHttpApiData, ToHttpApiData, Ord, Eq)
data LoginReq = LoginReq
{ whoo :: Text
, passwoord :: Text
} deriving (Generic)
data TokenValidity = TokenValidity
{ isValid :: Bool
} deriving (Generic, Show)
instance FromJSON LoginReq
instance ToJSON LoginReq
instance FromJSON TokenValidity
instance ToJSON TokenValidity
-- ** Albums microservice
type AlbumsAPI
= "api" :> "albooms"
:> Authorized (QueryParam "sortby" SortBy :> Get '[JSON] [Album])
data Album =
Album [Photo]
deriving (Generic)
data Photo = Photo
{ description :: Text
, image :: URL
} deriving (Generic)
data SortBy
= SortByWhoolest
| SortByDate
instance FromJSON Photo
instance ToJSON Photo
instance FromJSON Album
instance ToJSON Album
instance FromHttpApiData SortBy where
parseQueryParam "whoolest" = Right SortByWhoolest
parseQueryParam "date" = Right SortByDate
parseQueryParam x = Left ("Unknown sortby value:" <> x)
instance ToHttpApiData SortBy where
toQueryParam SortByWhoolest = "whoolest"
toQueryParam SortByDate = "date"
-- * Common prefixes and headers
type Authorized t = Header "Authorization" SigninToken :> t
type URL = Text