Change Body to Custom Type.

This commit is contained in:
Dillon Kearns 2020-01-03 13:37:50 -08:00
parent 6f59399e2c
commit 21da4794e7
5 changed files with 52 additions and 12 deletions

View File

@ -51,8 +51,8 @@ in [this article introducing StaticHttp requests and some concepts around it](ht
import Dict exposing (Dict)
import Dict.Extra
import Json.Decode.Exploration as Decode exposing (Decoder)
import Json.Encode as Encode
import Pages.Secrets
import Pages.StaticHttp.Body as Body exposing (Body)
import Pages.StaticHttp.Request as HashRequest
import Pages.StaticHttpRequest exposing (Request(..))
import Secrets
@ -404,7 +404,7 @@ get url decoder =
{ url = okUrl
, method = "GET"
, headers = []
, body = Nothing
, body = Body.empty
}
)
)
@ -417,7 +417,7 @@ type alias RequestDetails =
{ url : String
, method : String
, headers : List ( String, String )
, body : Maybe String
, body : Body
}

View File

@ -0,0 +1,36 @@
module Pages.StaticHttp.Body exposing (Body, empty, encode, string)
import Json.Encode as Encode
empty : Body
empty =
EmptyBody
string : String -> Body
string content =
StringBody content
type Body
= EmptyBody
| StringBody String
encode : Body -> Encode.Value
encode body =
case body of
EmptyBody ->
encodeWithType "empty" []
StringBody content ->
encodeWithType "string"
[ ( "content", Encode.string content )
]
encodeWithType typeName otherFields =
Encode.object <|
( "type", Encode.string typeName )
:: otherFields

View File

@ -1,13 +1,14 @@
module Pages.StaticHttp.Request exposing (Request, hash)
import Json.Encode as Encode
import Pages.StaticHttp.Body as Body exposing (Body)
type alias Request =
{ url : String
, method : String
, headers : List ( String, String )
, body : Maybe String
, body : Body
}
@ -17,6 +18,7 @@ hash requestDetails =
[ ( "method", Encode.string requestDetails.method )
, ( "url", Encode.string requestDetails.url )
, ( "headers", Encode.list hashHeader requestDetails.headers )
, ( "body", Body.encode requestDetails.body )
]
|> Encode.encode 0

View File

@ -13,6 +13,7 @@ import Pages.Internal.Platform.Cli as Main exposing (..)
import Pages.Manifest as Manifest
import Pages.PagePath as PagePath
import Pages.StaticHttp as StaticHttp
import Pages.StaticHttp.Body as Body exposing (Body)
import Pages.StaticHttp.Request as Request
import ProgramTest exposing (ProgramTest)
import Regex
@ -20,7 +21,6 @@ import Secrets
import SimulatedEffect.Cmd
import SimulatedEffect.Http as Http
import SimulatedEffect.Ports
import TerminalText as Terminal
import Test exposing (Test, describe, only, test)
import Test.Http
@ -257,7 +257,7 @@ all =
{ method = "POST"
, url = "https://api.github.com/repos/dillonkearns/elm-pages"
, headers = []
, body = Nothing
, body = Body.empty
}
)
(Decode.field "stargazer_count" Decode.int)
@ -272,7 +272,7 @@ all =
, [ ( { method = "POST"
, url = "https://api.github.com/repos/dillonkearns/elm-pages"
, headers = []
, body = Nothing
, body = Body.empty
}
, """{"stargazer_count":86}"""
)
@ -469,7 +469,7 @@ Bad status: 404""")
{ url = "https://api.github.com/repos/dillonkearns/elm-pages?apiKey=" ++ apiKey
, method = "GET"
, headers = [ ( "Authorization", "Bearer " ++ bearer ) ]
, body = Nothing
, body = Body.empty
}
)
|> Secrets.with "API_KEY"
@ -500,7 +500,7 @@ Bad status: 404""")
, headers =
[ ( "Authorization", "Bearer <BEARER>" )
]
, body = Nothing
, body = Body.empty
}
, """{}"""
)
@ -720,5 +720,5 @@ get url =
{ method = "GET"
, url = url
, headers = []
, body = Nothing
, body = Body.empty
}

View File

@ -4,6 +4,7 @@ import Dict exposing (Dict)
import Expect
import Json.Decode.Exploration as Decode
import Pages.StaticHttp as StaticHttp
import Pages.StaticHttp.Body as Body
import Pages.StaticHttp.Request as Request
import Pages.StaticHttpRequest as StaticHttpRequest
import Secrets
@ -30,7 +31,7 @@ get url =
{ method = "GET"
, url = url
, headers = []
, body = Nothing
, body = Body.empty
}
@ -130,9 +131,10 @@ all =
]
getReq : String -> StaticHttp.RequestDetails
getReq url =
{ url = url
, method = "GET"
, headers = []
, body = Nothing
, body = Body.empty
}