forked from kejace/purescript-trout-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.purs
55 lines (49 loc) · 1.78 KB
/
Main.purs
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
module Example.Client.Main where
import Prelude
import Data.Argonaut (class DecodeJson, decodeJson)
import Data.Either (Either(..))
import Data.Traversable (for_)
import Data.Tuple (Tuple(..))
import Effect (Effect)
import Effect.Aff (launchAff_)
import Effect.Class (liftEffect)
import Effect.Exception (throw)
import Foreign.Object (Object, toUnfoldable)
import JQuery (body, setHtml)
import Text.Smolder.HTML (h1, li, ul)
import Text.Smolder.Markup (Markup, empty, text)
import Text.Smolder.Renderer.String (render)
import Type.Proxy (Proxy(..))
import Type.Trout (type (:=), type (:/), type (:>))
import Type.Trout.Client (asClients, printError)
import Type.Trout.Client.BaseURI (BaseURI, baseURI)
import Type.Trout.ContentType.JSON (JSON)
import Type.Trout.Method (Get)
newtype BreedList = BreedList (Array (Tuple String (Array String)))
instance decodeJsonBreedList :: DecodeJson BreedList where
decodeJson json = do
response :: { message :: Object (Array String) } <- decodeJson json
pure (BreedList $ toUnfoldable response.message)
breedListMarkup :: forall e. BreedList -> Markup e
breedListMarkup (BreedList breeds) = do
h1 $ text "Dog Breeds"
ul $
for_ breeds \(Tuple breed subBreeds) ->
li do
text breed
case subBreeds of
[] -> empty
list -> ul $ for_ list (li <<< text)
type API = BaseURI :> ("breeds" := "breeds" :/ "list" :/ "all" :/ Get BreedList JSON)
main :: Effect Unit
main = do
case asClients (Proxy :: Proxy API) <$> baseURI "https://dog.ceo/api/" of
Left error ->
throw error
Right { breeds } ->
launchAff_ do
bs <- breeds."GET"
liftEffect $ case bs of
Left error -> throw $ printError error
Right breedList ->
setHtml (render $ breedListMarkup breedList) =<< body