2022-02-25 21:04:19 +03:00
|
|
|
module Api exposing (routes)
|
|
|
|
|
|
|
|
import ApiRoute exposing (ApiRoute)
|
|
|
|
import DataSource exposing (DataSource)
|
2022-07-09 04:00:30 +03:00
|
|
|
import Form
|
2022-07-09 03:56:52 +03:00
|
|
|
import Form.Field as Field
|
2022-07-09 03:51:26 +03:00
|
|
|
import Form.Validation as Validation
|
2022-02-25 21:04:19 +03:00
|
|
|
import Html exposing (Html)
|
|
|
|
import Json.Decode as Decode
|
2022-03-08 22:02:19 +03:00
|
|
|
import Json.Encode as Encode
|
2022-03-01 04:45:16 +03:00
|
|
|
import Pages
|
|
|
|
import Random
|
2022-03-09 19:49:46 +03:00
|
|
|
import Result.Extra
|
2022-02-25 21:04:19 +03:00
|
|
|
import Route exposing (Route)
|
|
|
|
import Server.Request as Request
|
2022-03-01 04:45:16 +03:00
|
|
|
import Server.Response as Response exposing (Response)
|
2022-03-01 01:47:22 +03:00
|
|
|
import Test.Glob
|
2022-03-01 04:45:16 +03:00
|
|
|
import Test.Runner.Html
|
|
|
|
import Time
|
2022-03-09 19:49:46 +03:00
|
|
|
import Xml.Decode
|
2022-02-25 21:04:19 +03:00
|
|
|
|
|
|
|
|
|
|
|
routes :
|
|
|
|
DataSource (List Route)
|
|
|
|
-> (Html Never -> String)
|
|
|
|
-> List (ApiRoute.ApiRoute ApiRoute.Response)
|
|
|
|
routes getStaticRoutes htmlToString =
|
2022-03-01 04:45:16 +03:00
|
|
|
let
|
2022-04-02 06:48:50 +03:00
|
|
|
html : Html Never -> Response data Never
|
2022-03-01 04:45:16 +03:00
|
|
|
html htmlValue =
|
2022-03-07 19:31:41 +03:00
|
|
|
htmlToString htmlValue
|
|
|
|
|> Response.body
|
|
|
|
|> Response.withHeader "Content-Type" "text/html; charset=UTF-8"
|
2022-03-01 04:45:16 +03:00
|
|
|
in
|
2022-02-25 21:04:19 +03:00
|
|
|
[ greet
|
2022-03-01 04:45:16 +03:00
|
|
|
, ApiRoute.succeed
|
|
|
|
(Request.succeed
|
|
|
|
(Test.Glob.all
|
|
|
|
|> DataSource.map viewHtmlResults
|
|
|
|
|> DataSource.map html
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|> ApiRoute.literal "tests"
|
|
|
|
|> ApiRoute.serverRender
|
2022-03-08 22:02:19 +03:00
|
|
|
, requestPrinter
|
2022-03-09 19:49:46 +03:00
|
|
|
, xmlDecoder
|
2022-03-09 20:19:46 +03:00
|
|
|
, multipleContentTypes
|
2022-02-25 21:04:19 +03:00
|
|
|
]
|
|
|
|
|
|
|
|
|
2022-03-09 19:49:46 +03:00
|
|
|
xmlDecoder : ApiRoute ApiRoute.Response
|
|
|
|
xmlDecoder =
|
|
|
|
let
|
|
|
|
dataDecoder : Xml.Decode.Decoder String
|
|
|
|
dataDecoder =
|
|
|
|
Xml.Decode.path [ "path", "to", "string", "value" ] (Xml.Decode.single Xml.Decode.string)
|
|
|
|
in
|
|
|
|
ApiRoute.succeed
|
|
|
|
(Request.map2
|
|
|
|
(\_ xmlString ->
|
|
|
|
xmlString
|
|
|
|
|> Xml.Decode.run dataDecoder
|
|
|
|
|> Result.Extra.merge
|
|
|
|
|> Response.plainText
|
|
|
|
|> DataSource.succeed
|
|
|
|
)
|
|
|
|
(Request.expectContentType "application/xml")
|
|
|
|
Request.expectBody
|
|
|
|
)
|
|
|
|
|> ApiRoute.literal "api"
|
|
|
|
|> ApiRoute.slash
|
|
|
|
|> ApiRoute.literal "xml"
|
|
|
|
|> ApiRoute.serverRender
|
|
|
|
|
|
|
|
|
2022-03-09 20:19:46 +03:00
|
|
|
multipleContentTypes : ApiRoute ApiRoute.Response
|
|
|
|
multipleContentTypes =
|
|
|
|
let
|
|
|
|
dataDecoder : Xml.Decode.Decoder String
|
|
|
|
dataDecoder =
|
|
|
|
Xml.Decode.path [ "path", "to", "string", "value" ] (Xml.Decode.single Xml.Decode.string)
|
|
|
|
in
|
|
|
|
ApiRoute.succeed
|
|
|
|
(Request.oneOf
|
|
|
|
[ Request.map2
|
|
|
|
(\_ xmlString ->
|
|
|
|
xmlString
|
|
|
|
|> Xml.Decode.run dataDecoder
|
|
|
|
|> Result.Extra.merge
|
|
|
|
|> Response.plainText
|
|
|
|
|> DataSource.succeed
|
|
|
|
)
|
|
|
|
(Request.expectContentType "application/xml")
|
|
|
|
Request.expectBody
|
|
|
|
, Request.map
|
|
|
|
(\decodedValue ->
|
|
|
|
decodedValue
|
|
|
|
|> Response.plainText
|
|
|
|
|> DataSource.succeed
|
|
|
|
)
|
|
|
|
(Request.expectJsonBody (Decode.at [ "path", "to", "string", "value" ] Decode.string))
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|> ApiRoute.literal "api"
|
|
|
|
|> ApiRoute.slash
|
|
|
|
|> ApiRoute.literal "multiple-content-types"
|
|
|
|
|> ApiRoute.serverRender
|
|
|
|
|
|
|
|
|
2022-03-08 22:02:19 +03:00
|
|
|
requestPrinter : ApiRoute ApiRoute.Response
|
|
|
|
requestPrinter =
|
|
|
|
ApiRoute.succeed
|
|
|
|
(Request.map4
|
|
|
|
(\rawBody method cookies queryParams ->
|
|
|
|
Encode.object
|
|
|
|
[ ( "rawBody"
|
|
|
|
, Maybe.map Encode.string rawBody
|
|
|
|
|> Maybe.withDefault Encode.null
|
|
|
|
)
|
|
|
|
, ( "method"
|
|
|
|
, method |> Request.methodToString |> Encode.string
|
|
|
|
)
|
|
|
|
, ( "cookies"
|
|
|
|
, cookies |> Encode.dict identity Encode.string
|
|
|
|
)
|
|
|
|
, ( "queryParams"
|
|
|
|
, queryParams |> Encode.dict identity (Encode.list Encode.string)
|
|
|
|
)
|
|
|
|
]
|
|
|
|
|> Response.json
|
|
|
|
|> DataSource.succeed
|
|
|
|
)
|
|
|
|
Request.rawBody
|
|
|
|
Request.method
|
2022-03-09 02:33:08 +03:00
|
|
|
Request.allCookies
|
2022-03-08 22:02:19 +03:00
|
|
|
Request.queryParams
|
|
|
|
)
|
|
|
|
|> ApiRoute.literal "api"
|
|
|
|
|> ApiRoute.slash
|
|
|
|
|> ApiRoute.literal "request-test"
|
|
|
|
|> ApiRoute.serverRender
|
|
|
|
|
|
|
|
|
2022-03-01 04:45:16 +03:00
|
|
|
config : Test.Runner.Html.Config
|
|
|
|
config =
|
|
|
|
Random.initialSeed (Pages.builtAt |> Time.posixToMillis)
|
|
|
|
|> Test.Runner.Html.defaultConfig
|
|
|
|
|> Test.Runner.Html.hidePassedTests
|
|
|
|
|
|
|
|
|
|
|
|
viewHtmlResults tests =
|
|
|
|
Html.div []
|
|
|
|
[ Html.h1 [] [ Html.text "My Test Suite" ]
|
|
|
|
, Html.div [] [ Test.Runner.Html.viewResults config tests ]
|
|
|
|
]
|
|
|
|
|
|
|
|
|
2022-02-25 21:04:19 +03:00
|
|
|
greet : ApiRoute ApiRoute.Response
|
|
|
|
greet =
|
|
|
|
ApiRoute.succeed
|
|
|
|
(Request.oneOf
|
2022-08-03 11:01:22 +03:00
|
|
|
[ Request.formData
|
2022-07-24 10:11:12 +03:00
|
|
|
[ Form.init
|
2022-06-30 19:38:49 +03:00
|
|
|
(\bar ->
|
2022-07-23 21:26:40 +03:00
|
|
|
{ combine =
|
|
|
|
Validation.succeed identity
|
|
|
|
|> Validation.andMap bar
|
|
|
|
, view =
|
|
|
|
\_ -> ()
|
|
|
|
}
|
2022-06-30 19:38:49 +03:00
|
|
|
)
|
2022-07-24 10:11:12 +03:00
|
|
|
|> Form.field "first" (Field.text |> Field.required "Required")
|
2022-06-30 19:38:49 +03:00
|
|
|
]
|
|
|
|
|> Request.andThen
|
|
|
|
(\result ->
|
|
|
|
result
|
|
|
|
|> Result.mapError (\_ -> "")
|
|
|
|
|> Request.fromResult
|
|
|
|
)
|
2022-02-25 21:04:19 +03:00
|
|
|
, Request.expectJsonBody (Decode.field "first" Decode.string)
|
|
|
|
, Request.expectQueryParam "first"
|
|
|
|
, Request.expectMultiPartFormPost
|
|
|
|
(\{ field, optionalField } ->
|
|
|
|
field "first"
|
|
|
|
)
|
|
|
|
]
|
|
|
|
|> Request.map
|
|
|
|
(\firstName ->
|
|
|
|
Response.plainText ("Hello " ++ firstName)
|
|
|
|
|> DataSource.succeed
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|> ApiRoute.literal "api"
|
|
|
|
|> ApiRoute.slash
|
|
|
|
|> ApiRoute.literal "greet"
|
|
|
|
|> ApiRoute.serverRender
|