-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathAPI.hs
258 lines (222 loc) · 9.26 KB
/
API.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TypeOperators #-}
module Up.API
( -- * Accounts API
listAccounts,
listAccounts_,
retrieveAccount,
-- * Categories API
listCategories,
retrieveCategory,
-- * Tags API
listTags,
listTags_,
addTags,
removeTags,
-- * Transactions API
listTransactions,
listTransactions_,
retrieveTransaction,
listTransactionsByAccount,
listTransactionsByAccount_,
-- * Utility API
ping,
unpaginate,
)
where
import Control.Arrow (second)
import Data.Proxy
import qualified Data.Text as T
import Servant.API
import Servant.Client
import Up.Model.Account
import Up.Model.Category
import Up.Model.Paginated
import Up.Model.Tag
import Up.Model.Transaction
import Up.Model.Utility
-- * Accounts API
type AccountsAPI =
"accounts" :> QueryParam "page[size]" Int
:> QueryParam "page[before]" AccountId
:> QueryParam "page[after]" AccountId
:> Get '[JSON] (Paginated Account)
:<|> "accounts" :> Capture "id" T.Text
:> Get '[JSON] Account
-- | Retrieve a paginated list of all accounts for the currently authenticated user.
listAccounts :: Maybe Int -- ^ page[size]
-> Maybe AccountId -- ^ page[before]
-> Maybe AccountId -- ^ page[after]
-> ClientM (Paginated Account)
-- | Retrieve an unfolded list of all accounts for the currently authenticated user.
listAccounts_ :: Maybe Int -- ^ page[size]
-> ClientM [Account]
listAccounts_ size = unpaginate f g >>= pure . concat
where
f = listAccounts size Nothing Nothing
g after = listAccounts size Nothing after
-- | Retrieve a specific account by providing its unique identifier.
retrieveAccount :: T.Text
-> ClientM Account
listAccounts :<|> retrieveAccount = client (Proxy :: Proxy AccountsAPI)
-- * Categories API
type CategoriesAPI =
-- listCategories
"categories" :> QueryParam "filter[parent]" CategoryId
:> Get '[JSON] Categories
-- retrieveCategory
:<|> "categories" :> Capture "id" T.Text
:> Get '[JSON] Category
-- | Retrieve a list of all categories and their ancestry.
listCategories :: Maybe CategoryId
-> ClientM Categories
-- | Retrieve a specific category by providing its unique identifier.
retrieveCategory :: T.Text
-> ClientM Category
listCategories :<|> retrieveCategory = client (Proxy :: Proxy CategoriesAPI)
-- * Tags API
type TagsAPI =
"tags" :> QueryParam "page[size]" Int
:> QueryParam "page[before]" AccountId
:> QueryParam "page[after]" AccountId
:> Get '[JSON] (Paginated Tag)
:<|> "transactions" :> Capture "transactionId" T.Text
:> "relationships"
:> "tags"
:> ReqBody '[JSON] TagInput
:> Post '[JSON] ()
:<|> "transactions" :> Capture "transactionId" T.Text
:> "relationships"
:> "tags"
:> ReqBody '[JSON] TagInput
:> Delete '[JSON] ()
-- | Retrieve a paginated list of all tags currently in use.
listTags :: Maybe Int -- ^ page[size]
-> Maybe TagId -- ^ page[before]
-> Maybe TagId -- ^ page[after]
-> ClientM (Paginated Tag)
-- | Retrieve an unfolded list of all tags currently in use.
listTags_ :: Maybe Int -- ^ page[size]
-> ClientM [Tag]
listTags_ size = unpaginate f g >>= pure . concat
where f = listTags size Nothing Nothing
g after = listTags size Nothing after
-- | Associates one or more tags with a specific transaction. No more than 6 tags
-- may be present on any single transaction. Duplicate tags are silently ignored.
addTags :: T.Text
-> TagInput
-> ClientM ()
-- | Delete a specific webhook by providing its unique identifier.
-- Once deleted, webhook events will no longer be sent to the configured URL.
removeTags :: T.Text
-> TagInput
-> ClientM ()
listTags
:<|> addTags
:<|> removeTags =
client (Proxy :: Proxy TagsAPI)
-- * Transactions API
type TransactionsAPI =
-- listTransactions
"transactions" :> QueryParam "page[size]" Int
:> QueryParam "page[before]" TransactionId
:> QueryParam "page[after]" TransactionId
:> QueryParam "filter[since]" T.Text
:> QueryParam "filter[until]" T.Text
:> QueryParam "filter[tag]" T.Text
:> Get '[JSON] (Paginated Transaction)
-- retrieveTransaction
:<|> "transactions" :> Capture "transactionId" TransactionId
:> Get '[JSON] (Transaction)
-- listTransactionsByAccount
:<|> "accounts" :> Capture "accountID" AccountId
:> "transactions"
:> QueryParam "page[size]" Int
:> QueryParam "page[before]" TransactionId
:> QueryParam "page[after]" TransactionId
:> QueryParam "filter[since]" T.Text
:> QueryParam "filter[until]" T.Text
:> QueryParam "filter[tag]" T.Text
:> Get '[JSON] (Paginated Transaction)
-- | Retrieve a paginated list of all transactions across all accounts for the currently authenticated user.
listTransactions :: Maybe Int -- ^ page[size]
-> Maybe TransactionId -- ^ page[before]
-> Maybe TransactionId -- ^ page[after]
-> Maybe T.Text -- ^ filter[since]
-> Maybe T.Text -- ^ filter[until]
-> Maybe T.Text -- ^ filter[tag]
-> ClientM (Paginated Transaction)
-- | Retrieve an unfolded list of all transactions across all accounts for the currently authenticated user.
listTransactions_ :: Maybe Int -- ^ page[size]
-> Maybe T.Text -- ^ filter[since]
-> Maybe T.Text -- ^ filter[until]
-> Maybe T.Text -- ^ filter[tag]
-> ClientM [Transaction]
listTransactions_ size fsince funtil ftag = unpaginate f g >>= pure . concat
where
f = listTransactions size Nothing Nothing fsince funtil ftag
g after = listTransactions size Nothing after Nothing Nothing Nothing
-- | Retrieve a specific transaction by providing its unique identifier.
retrieveTransaction :: TransactionId -- ^ transactionId
-> ClientM (Transaction)
-- | Retrieve a list of all transactions for a specific account.
listTransactionsByAccount :: AccountId -- ^ accountId
-> Maybe Int -- ^ page[size]
-> Maybe AccountId -- ^ page[before]
-> Maybe AccountId -- ^ page[after]
-> Maybe T.Text -- ^ filter[since]
-> Maybe T.Text -- ^ filter[until]
-> Maybe T.Text -- ^ filter[tag]
-> ClientM (Paginated Transaction)
-- | Retrieve an unfolded list of all transactions for a specific account.
listTransactionsByAccount_ :: AccountId -- ^ accountId
-> Maybe Int -- ^ page[size]
-> Maybe T.Text -- ^ filter[since]
-> Maybe T.Text -- ^ filter[until]
-> Maybe T.Text -- ^ filter[tag]
-> ClientM [Transaction]
listTransactionsByAccount_ aid size fsince funtil ftag = unpaginate f g >>= pure . concat
where
f = listTransactionsByAccount aid size Nothing Nothing fsince funtil ftag
g after = listTransactionsByAccount aid size Nothing after Nothing Nothing Nothing
listTransactions
:<|> retrieveTransaction
:<|> listTransactionsByAccount =
client (Proxy :: Proxy TransactionsAPI)
-- * Utlity API
type UtilityAPI =
-- Ping
"util" :> "ping" :> Get '[JSON] Ping
-- | Make a basic ping request to the API.
-- | This is useful to verify that authentication is functioning correctly.
ping :: ClientM Ping
ping = client (Proxy :: Proxy UtilityAPI)
data Cursor a = Start | Next a | End deriving (Show)
maybeToCursor :: Maybe a -> Cursor a
maybeToCursor (Just x) = Next x
maybeToCursor Nothing = End
unfoldrM :: Show b => Monad m => (b -> m (Maybe (a, b))) -> b -> m [a]
unfoldrM f b = do
r <- f b
case r of
Just (a, b') -> fmap (a :) (unfoldrM f b')
Nothing -> return []
wrapped ::
Applicative m =>
m (Maybe (a, Maybe b)) ->
(Maybe b -> m (Maybe (a, Maybe b))) ->
Cursor b ->
m (Maybe (a, Cursor b))
wrapped f _g Start = fmap (second maybeToCursor) <$> f
wrapped _f g (Next b) = fmap (second maybeToCursor) <$> g (Just b)
wrapped _f _g End = pure Nothing
unpaginate ::
Monad m =>
m (Paginated a) ->
(Maybe T.Text -> m (Paginated a)) ->
m [[a]]
unpaginate f g = unfoldrM (wrapped f' g') Start
where
f' = f >>= \p -> return $ Just (paginatedData p, paginatedNext p)
g' b = g b >>= \p -> return $ Just (paginatedData p, paginatedNext p)