-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
193 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,168 @@ | ||
module Example where | ||
|
||
import Effects exposing (Effects, Never) | ||
import StartApp exposing (App) | ||
import Task exposing (Task, toResult) | ||
import Html exposing (Html, h4, div, text, button, input) | ||
import Html.Attributes exposing (id, type') | ||
import Html.Events exposing (onClick, targetValue, on) | ||
import Signal exposing (Signal, Address) | ||
|
||
import Http.Decorators exposing (addCacheBuster, promoteError, interpretStatus) | ||
import Http exposing (..) | ||
|
||
|
||
oneTask : Task RawError Response | ||
oneTask = | ||
addCacheBuster Http.send Http.defaultSettings | ||
{ verb = "GET" | ||
, headers = [] | ||
, url = Http.url "http://www.elm-lang.org/" [] | ||
, body = Http.empty | ||
} | ||
|
||
|
||
specialSend : Settings -> Request -> Task RawError Response | ||
specialSend = addCacheBuster Http.send | ||
|
||
|
||
useSpecialSend : Task RawError Response | ||
useSpecialSend = | ||
specialSend defaultSettings | ||
{ verb = "GET" | ||
, headers = [] | ||
, url = Http.url "http://github.com/" [] | ||
, body = Http.empty | ||
} | ||
|
||
|
||
verySpecialSend : Request -> Task Error Response | ||
verySpecialSend = interpretStatus << addCacheBuster Http.send Http.defaultSettings | ||
|
||
|
||
useVerySpecialSend : Task Error Response | ||
useVerySpecialSend = | ||
verySpecialSend | ||
{ verb = "GET" | ||
, headers = [] | ||
, url = Http.url "http://www.apple.com/" [] | ||
, body = Http.empty | ||
} | ||
|
||
|
||
lessSpecialSend : Settings -> Request -> Task Error Response | ||
lessSpecialSend settings = interpretStatus << addCacheBuster Http.send settings | ||
|
||
|
||
useLessSpecialSend : Task Error Response | ||
useLessSpecialSend = | ||
lessSpecialSend defaultSettings | ||
{ verb = "GET" | ||
, headers = [] | ||
, url = Http.url "http://package.elm-lang.org/" [] | ||
, body = Http.empty | ||
} | ||
|
||
|
||
app : App Model | ||
app = | ||
StartApp.start | ||
{ init = init | ||
, update = update | ||
, view = view | ||
, inputs = [] | ||
} | ||
|
||
|
||
main : Signal Html | ||
main = app.html | ||
|
||
|
||
port tasks : Signal (Task.Task Never ()) | ||
port tasks = app.tasks | ||
|
||
|
||
type alias Model = | ||
{ message : String | ||
} | ||
|
||
|
||
init : (Model, Effects Action) | ||
init = (Model "Initial state", Effects.none) | ||
|
||
|
||
type Action | ||
= OneTask | ||
| SpecialSend | ||
| VerySpecialSend | ||
| LessSpecialSend | ||
| HandleRawResponse (Result RawError Response) | ||
| HandleResponse (Result Error Response) | ||
|
||
|
||
update : Action -> Model -> (Model, Effects Action) | ||
update action model = | ||
case action of | ||
OneTask -> | ||
( { model | message = "Sending with addCacheBuster" } | ||
, oneTask | ||
|> toResult | ||
|> Task.map HandleRawResponse | ||
|> Effects.task | ||
) | ||
|
||
SpecialSend -> | ||
( { model | message = "Sending with specialSend" } | ||
, useSpecialSend | ||
|> toResult | ||
|> Task.map HandleRawResponse | ||
|> Effects.task | ||
) | ||
|
||
VerySpecialSend -> | ||
( { model | message = "Sending with verySpecialSend" } | ||
, useVerySpecialSend | ||
|> toResult | ||
|> Task.map HandleResponse | ||
|> Effects.task | ||
) | ||
|
||
LessSpecialSend -> | ||
( { model | message = "Sending with lessSpecialSend" } | ||
, useLessSpecialSend | ||
|> toResult | ||
|> Task.map HandleResponse | ||
|> Effects.task | ||
) | ||
|
||
HandleRawResponse result -> | ||
( { model | message = toString result } | ||
, Effects.none | ||
) | ||
|
||
HandleResponse result -> | ||
( { model | message = toString result } | ||
, Effects.none | ||
) | ||
|
||
|
||
view : Address Action -> Model -> Html | ||
view address model = | ||
div [] | ||
[ button | ||
[ onClick address OneTask ] | ||
[ text "addCacheBuster" ] | ||
, button | ||
[ onClick address SpecialSend ] | ||
[ text "specialSend" ] | ||
, button | ||
[ onClick address VerySpecialSend ] | ||
[ text "verySpecialSend" ] | ||
, button | ||
[ onClick address LessSpecialSend ] | ||
[ text "lessSpecialSend" ] | ||
|
||
, h4 [] [ text "Message" ] | ||
, div [ id "message" ] [ text model.message ] | ||
] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
{ | ||
"version": "1.0.0", | ||
"summary": "Examples for the Http.Decorators module", | ||
"repository": "https://github.com/rgrempel/elm-http-decoarators.git", | ||
"license": "BSD3", | ||
"source-directories": [ | ||
".", | ||
"../src" | ||
], | ||
"exposed-modules": [], | ||
"dependencies": { | ||
"elm-lang/core": "3.0.0 <= v < 4.0.0", | ||
"evancz/elm-effects": "2.0.1 <= v < 3.0.0", | ||
"evancz/elm-html": "4.0.2 <= v < 5.0.0", | ||
"evancz/elm-http": "3.0.0 <= v < 4.0.0", | ||
"evancz/start-app": "2.0.2 <= v < 3.0.0", | ||
"evancz/task-tutorial": "1.0.3 <= v < 2.0.0" | ||
}, | ||
"elm-version": "0.16.0 <= v < 0.17.0" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters