Wire through empty headers in requests.

This commit is contained in:
Dillon Kearns 2019-11-11 16:20:53 -08:00
parent cd8077ebc7
commit 63abeece20
4 changed files with 40 additions and 124 deletions

View File

@ -1,50 +1,14 @@
module Pages.Internal.Secrets exposing (RequestDetails, Secrets(..), UnmaskedUrl, Url, UrlWithSecrets, decoder, empty, get, hashRequest, masked, requestToString, stringToUrl, unwrap, urlWithoutSecrets, useFakeSecrets, useFakeSecrets2, useFakeSecrets3)
module Pages.Internal.Secrets exposing (RequestDetails, Secrets(..), UnmaskedUrl, Url, UrlWithSecrets, get, unwrap)
import BuildError exposing (BuildError)
import Dict exposing (Dict)
import Http
import Json.Decode as Decode exposing (Decoder)
stringToUrl : (Secrets -> Result BuildError { url : String, method : String }) -> (Secrets -> Result BuildError Url)
stringToUrl f1 =
let
maskedUrl =
-- TODO hash it and mask it here
useFakeSecrets2 f1
|> .url
in
\secrets ->
case f1 secrets of
Ok unmaskedUrl ->
Ok (Url ( UnmaskedUrl unmaskedUrl, maskedUrl ))
Err error ->
Err error
hashUrl : RequestDetails -> String
hashUrl requestDetails =
"["
++ requestDetails.method
++ "]"
++ requestDetails.url
urlWithoutSecrets : { url : String, method : String } -> UrlWithSecrets
urlWithoutSecrets rawUrlWithoutSecrets =
stringToUrl (\secrets -> Ok rawUrlWithoutSecrets)
type Url
= Url ( UnmaskedUrl, String )
masked : Url -> String
masked (Url ( _, maskedUrl )) =
maskedUrl
type UnmaskedUrl
= UnmaskedUrl { url : String, method : String }
@ -88,57 +52,5 @@ protected =
Protected
useFakeSecrets : UrlWithSecrets -> String
useFakeSecrets urlWithSecrets =
urlWithSecrets protected
|> Result.withDefault (Url ( UnmaskedUrl { url = "", method = "" }, "" ))
|> masked
type alias RequestDetails =
{ url : String, method : String }
useFakeSecrets3 : (Secrets -> Result BuildError Url) -> RequestDetails
useFakeSecrets3 urlWithSecrets =
urlWithSecrets protected
|> Result.map
(\(Url ( UnmaskedUrl unmaskedUrl, maskedUrl )) ->
unmaskedUrl
)
|> Result.withDefault defaultRequest
useFakeSecrets2 : (Secrets -> Result BuildError a) -> a
useFakeSecrets2 urlWithSecrets =
case urlWithSecrets protected of
Ok value ->
value
Err _ ->
Debug.todo "ERROR"
requestToString : RequestDetails -> String
requestToString requestDetails =
requestDetails.url
hashRequest : RequestDetails -> String
hashRequest requestDetails =
hashUrl requestDetails
defaultRequest : RequestDetails
defaultRequest =
{ url = "", method = "" }
empty =
Secrets Dict.empty
decoder : Decoder Secrets
decoder =
Decode.dict Decode.string
|> Decode.map Secrets
{ url : String, method : String, headers : List ( String, String ) }

View File

@ -9,7 +9,7 @@ import TerminalText as Terminal
type Request value
= Request ( List (Secrets2.Value { url : String, method : String }), Dict String String -> Result Error ( Dict String String, Request value ) )
= Request ( List (Secrets2.Value { url : String, method : String, headers : List ( String, String ) }), Dict String String -> Result Error ( Dict String String, Request value ) )
| Done value
@ -44,7 +44,7 @@ type Error
type alias RequestDetails =
{ url : String, method : String }
{ url : String, method : String, headers : List ( String, String ) }
urls : Request value -> List (Secrets2.Value RequestDetails)

View File

@ -174,7 +174,7 @@ lookup requestInfo rawResponses =
Ok ( rawResponses, value )
addUrls : List (Secrets2.Value { url : String, method : String }) -> Pages.StaticHttpRequest.Request value -> Pages.StaticHttpRequest.Request value
addUrls : List (Secrets2.Value { url : String, method : String, headers : List ( String, String ) }) -> Pages.StaticHttpRequest.Request value -> Pages.StaticHttpRequest.Request value
addUrls urlsToAdd requestInfo =
case requestInfo of
Request ( initialUrls, function ) ->
@ -188,7 +188,7 @@ addUrls urlsToAdd requestInfo =
-- Request ( urlsToAdd, \_ -> value |> Done |> Ok )
lookupUrls : Pages.StaticHttpRequest.Request value -> List (Secrets2.Value { url : String, method : String })
lookupUrls : Pages.StaticHttpRequest.Request value -> List (Secrets2.Value RequestDetails)
lookupUrls requestInfo =
case requestInfo of
Request ( urls, lookupFn ) ->
@ -240,7 +240,7 @@ getWithSecrets url decoder =
jsonRequestWithSecrets
(url
|> Secrets2.map
(\okUrl -> { url = okUrl, method = "GET" })
(\okUrl -> { url = okUrl, method = "GET", headers = [] })
)
decoder
@ -250,19 +250,37 @@ getWithSecrets url decoder =
get : String -> Decoder a -> Request a
get url decoder =
jsonRequest
{ url = url, method = "GET" }
{ url = url, method = "GET", headers = [] }
decoder
type alias RequestDetails =
{ url : String, method : String, headers : List ( String, String ) }
hashRequest : RequestDetails -> String
hashRequest requestDetails =
"["
++ requestDetails.method
++ "]"
++ requestDetails.url
++ String.join "," (requestDetails.headers |> List.map (\( key, value ) -> key ++ " : " ++ value))
requestToString : RequestDetails -> String
requestToString requestDetails =
requestDetails.url
{-| TODO
-}
jsonRequest : { url : String, method : String } -> Decoder a -> Request a
jsonRequest : { url : String, method : String, headers : List ( String, String ) } -> Decoder a -> Request a
jsonRequest url decoder =
Request
( [ Secrets2.succeed url ]
, \rawResponseDict ->
rawResponseDict
|> Dict.get (url |> Pages.Internal.Secrets.hashRequest)
|> Dict.get (url |> hashRequest)
|> (\maybeResponse ->
case maybeResponse of
Just rawResponse ->
@ -274,7 +292,7 @@ jsonRequest url decoder =
Nothing ->
url
|> Pages.Internal.Secrets.requestToString
|> requestToString
|> Pages.StaticHttpRequest.MissingHttpResponse
|> Err
)
@ -294,19 +312,19 @@ jsonRequest url decoder =
-}
reducedGet : String -> Json.Decode.Exploration.Decoder a -> Request a
reducedGet url decoder =
reducedJsonRequest { url = url, method = "GET" } decoder
reducedJsonRequest { url = url, method = "GET", headers = [] } decoder
{-| TODO
-}
reducedPost : String -> Json.Decode.Exploration.Decoder a -> Request a
reducedPost url decoder =
reducedJsonRequest { url = url, method = "POST" } decoder
reducedJsonRequest { url = url, method = "POST", headers = [] } decoder
{-| TODO
-}
reducedJsonRequest : { url : String, method : String } -> Json.Decode.Exploration.Decoder a -> Request a
reducedJsonRequest : RequestDetails -> Json.Decode.Exploration.Decoder a -> Request a
reducedJsonRequest requestInfo decoder =
request (Secrets2.succeed requestInfo) decoder
@ -322,14 +340,7 @@ type Expect a
{-| TODO
-}
request :
Secrets2.Value
{ method : String
-- , headers : List Header
, url : String
-- , body : Body
}
Secrets2.Value RequestDetails
-> Json.Decode.Exploration.Decoder a
-> Request a
request urlWithSecrets decoder =
@ -337,7 +348,7 @@ request urlWithSecrets decoder =
( [ urlWithSecrets ]
, \rawResponseDict ->
rawResponseDict
|> Dict.get (Secrets2.maskedLookup urlWithSecrets |> Pages.Internal.Secrets.hashRequest)
|> Dict.get (Secrets2.maskedLookup urlWithSecrets |> hashRequest)
|> (\maybeResponse ->
case maybeResponse of
Just rawResponse ->
@ -349,7 +360,7 @@ request urlWithSecrets decoder =
Nothing ->
Secrets2.maskedLookup urlWithSecrets
|> Pages.Internal.Secrets.requestToString
|> requestToString
|> Pages.StaticHttpRequest.MissingHttpResponse
|> Err
)
@ -384,7 +395,7 @@ request urlWithSecrets decoder =
(\finalRequest ->
( strippedResponses
|> Dict.insert
(Secrets2.maskedLookup urlWithSecrets |> Pages.Internal.Secrets.hashRequest)
(Secrets2.maskedLookup urlWithSecrets |> hashRequest)
reduced
, finalRequest
)
@ -396,14 +407,7 @@ request urlWithSecrets decoder =
{-| TODO
-}
jsonRequestWithSecrets :
Secrets2.Value
{ method : String
-- , headers : List Header
, url : String
-- , body : Body
}
Secrets2.Value RequestDetails
-> Decoder a
-> Request a
jsonRequestWithSecrets urlWithSecrets decoder =
@ -411,7 +415,7 @@ jsonRequestWithSecrets urlWithSecrets decoder =
( [ urlWithSecrets ]
, \rawResponseDict ->
rawResponseDict
|> Dict.get (Secrets2.maskedLookup urlWithSecrets |> Pages.Internal.Secrets.hashRequest)
|> Dict.get (Secrets2.maskedLookup urlWithSecrets |> hashRequest)
|> (\maybeResponse ->
case maybeResponse of
Just rawResponse ->
@ -421,7 +425,7 @@ jsonRequestWithSecrets urlWithSecrets decoder =
Nothing ->
-- Err <| "Couldn't find response for url `" ++ Pages.Internal.Secrets.useFakeSecrets urlWithSecrets ++ "`"
Secrets2.maskedLookup urlWithSecrets
|> Pages.Internal.Secrets.requestToString
|> requestToString
|> Pages.StaticHttpRequest.MissingHttpResponse
|> Err
)

View File

@ -104,4 +104,4 @@ all =
get url =
{ url = url, method = "GET" }
{ url = url, method = "GET", headers = [] }