-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHttpGetter.elm
55 lines (45 loc) · 1.5 KB
/
HttpGetter.elm
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
module HttpGetter where
-- Module that sends HTTP GET requests to the server.
-- The server returns a JSON Blob that is of Blob type.
import Json.Decode exposing ((:=))
import Http exposing (Error)
import Task exposing (Task)
import Html exposing (..)
import Html.Attributes exposing (..)
import Html.Events exposing (..)
import Signal
import Effects
-- MODEL: Json Object that GET requerst returns
type alias Blob = { listings : List (Listing) }
type alias Listing = { key : String
, title : String
, body : String
, price : Float
, categories : List String
, approved : Bool
, sold : Bool
, lastUpdated : String }
init : Blob
init = { listings = [] }
-- Decoder
listingDecoder : Json.Decode.Decoder Listing
listingDecoder =
Json.Decode.object8 Listing
("key" := Json.Decode.string)
("title" := Json.Decode.string)
("body" := Json.Decode.string)
("price" := Json.Decode.float)
("categories" := (Json.Decode.list Json.Decode.string))
("approved" := Json.Decode.bool)
("sold" := Json.Decode.bool)
("lastUpdate" := Json.Decode.string)
blobDecoder : Json.Decode.Decoder Blob
blobDecoder =
Json.Decode.object1 Blob
("listings" := (Json.Decode.list listingDecoder))
-- Effects
-- getListings will trigger a GET request at this url.
getListings : String -> Task never (Maybe Blob)
getListings url =
Http.get blobDecoder url
|> Task.toMaybe