Skip to content

Commit

Permalink
Add support for host and basePath in spec
Browse files Browse the repository at this point in the history
This means that the request urls are no longer hardcoded to
http://petstore.swagger.io/v2/
  • Loading branch information
Joel Carlbark committed May 27, 2016
1 parent 5dba185 commit 32cf74d
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 55 deletions.
11 changes: 4 additions & 7 deletions Lagun.elm
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import Set exposing (Set)
-- MODEL


type alias Model = -- TODO model spec as Result spec instead?
type alias Model =
{ specUrl : String,
spec : Maybe Spec,
expanded : Set String,
Expand Down Expand Up @@ -80,10 +80,6 @@ update action model =
)



-- Cmd


tryRequest : String -> String -> Http.Request -> Cmd Msg
tryRequest path' verb req =
let
Expand Down Expand Up @@ -112,7 +108,7 @@ type alias ParameterValues =
-- Used for JSON decoding

type alias Spec =
{ info : Info, paths : Paths, swagger : String, host : String }
{ info : Info, paths : Paths, swagger : String, host : String, basePath: String }


type alias Info =
Expand Down Expand Up @@ -220,9 +216,10 @@ optionalField field =

decodeSpec : Json.Decoder Spec
decodeSpec =
Json.object4
Json.object5
Spec
decodeInfo
decodePaths
("swagger" := Json.string)
(optionalField "host")
(optionalField "basePath")
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ I doubt I will add support for other specs (such as [RAML](http://raml.org/)). P
## TODO / Explore

- Add support for remaining parameter types: formData
- Remove hardcoded host and basePath for requests :-1:
- Show schema types for body parameters (ugh)
- Show response model for http responses
- Some tests would be fun and useful
Expand Down
107 changes: 62 additions & 45 deletions View.elm
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import Html.Attributes exposing (placeholder, value, class, type', src, alt, hre
import Html.Events exposing (onClick, onInput, targetValue)
import Markdown
import Regex
import Lagun exposing (Msg, Model, Parameter, ParameterKey, ParameterValues, Operations, Paths, Response, Operation, RequestResults)
import Lagun exposing (Msg, Model, Parameter, ParameterKey, ParameterValues, Operations, Paths, Response, Operation, RequestResults, Spec)
import Dict exposing (Dict)
import Set exposing (Set)
import Http
Expand All @@ -27,7 +27,7 @@ view { specUrl, spec, expanded, paramValues, requestResults } =
, Markdown.toHtml [class "div"] spec.info.description
, p [] [ text ("API Version: " ++ spec.info.version) ]
, hr [] []
, pathList paramValues spec.paths expanded requestResults
, pathList spec paramValues expanded requestResults
]
]

Expand Down Expand Up @@ -64,8 +64,8 @@ specUrlInput specUrl =
[]


operationEntry : ParameterValues -> String -> RequestResults -> ( String, Operation ) -> Html Msg
operationEntry paramValues path' results ( opName, op ) =
operationEntry : String -> ParameterValues -> String -> RequestResults -> ( String, Operation ) -> Html Msg
operationEntry url paramValues path' results ( opName, op ) =
dt
[]
[ div
Expand All @@ -87,7 +87,7 @@ operationEntry paramValues path' results ( opName, op ) =
, parametersTable (parametersTableBody paramValues path' opName op.parameters)
, h6 [] [ text "Responses" ]
, responsesTable op.responses
, requestButton path' opName (requestBuilder opName path' paramValues)
, requestButton path' opName (requestBuilder url opName path' paramValues)
, requestResult (path', opName) results
]
]
Expand All @@ -104,8 +104,8 @@ pathWithVariables path' variables =
Regex.replace Regex.All re (\{ match } -> (Maybe.withDefault "" (Dict.get match variables))) path'


requestBuilder : String -> String -> ParameterValues -> Http.Request
requestBuilder verb path' paramValues =
requestBuilder : String -> String -> String -> ParameterValues -> Http.Request
requestBuilder url verb path' paramValues =
let
relevantParamValues =
Dict.filter (\( p, v, _, _ ) _ -> p == path' && v == verb) paramValues
Expand Down Expand Up @@ -140,7 +140,7 @@ requestBuilder verb path' paramValues =
, headers =
[("Accept", "application/json")] ++ headerParams ++ (Maybe.withDefault [] otherHeaders)
-- application/xml, TODO these reside in paths.<path>.<method>.produces[]
, url = Http.url ("http://petstore.swagger.io/v2" ++ (pathWithVariables path' pathParams)) queryParams
, url = Http.url (url ++ (pathWithVariables path' pathParams)) queryParams
, body = Maybe.withDefault Http.empty bodyParam
}

Expand All @@ -149,7 +149,7 @@ requestButton : String -> String -> Http.Request -> Html Msg
requestButton path' verb req =
button
[ class "button", onClick (Lagun.TryRequest (path', verb) req) ]
[ text "Send request" ]
[ text ("Request " ++ verb) ]

requestResult : (String, String) -> RequestResults -> Html msg
requestResult key results =
Expand Down Expand Up @@ -179,7 +179,10 @@ showHttpResponse mr =
[text ("Response headers:\n")
, div
[ class "code-box" ]
[ code [ class "code-text"] [text (String.join "<br />\n" (List.map (\(k, v) -> (k ++ ": " ++ v)) (Dict.toList headers)))]]
[ code
[ class "code-text"]
[text (String.join "\n" (List.map (\(k, v) -> (k ++ ": " ++ v)) (Dict.toList headers)))]
]
]
]

Expand Down Expand Up @@ -275,9 +278,9 @@ parameterKey path' opName param =
( path', opName, param.in', param.name )


operationList : ParameterValues -> String -> Operations -> RequestResults -> Html Msg
operationList paramValues path' ops results =
dl [] (List.map (operationEntry paramValues path' results) (Dict.toList ops))
operationList : String -> ParameterValues -> String -> Operations -> RequestResults -> Html Msg
operationList url paramValues path' ops results =
dl [] (List.map (operationEntry url paramValues path' results) (Dict.toList ops))


responseEntry : ( String, Response ) -> Html msg
Expand Down Expand Up @@ -328,54 +331,68 @@ responsesTable rs =
]


renderPath : ParameterValues -> Set String -> RequestResults -> ( String, Operations ) -> Html Msg
renderPath paramValues expanded results ( pathName, ops ) =
case (Set.member pathName expanded) of
False ->
div
renderHiddenPath : String -> Set String -> Html Msg
renderHiddenPath pathName expanded =
div
[]
[ h5
[]
[ h5
[]
[ a
[ onClick (Lagun.ExpansionToggled (Set.insert pathName expanded))
, href ("#" ++ pathName)
, name pathName
]
[ fontAwesome "plus-square-o" ]
, text (" " ++ pathName)
[ a
[ onClick (Lagun.ExpansionToggled (Set.insert pathName expanded))
, href ("#" ++ pathName)
, name pathName
]
[ fontAwesome "plus-square-o" ]
, text (" " ++ pathName)
]
]

True ->
div
renderExpandedPath : String -> Set String -> Html Msg -> Html Msg
renderExpandedPath pathName expanded opsList =
div
[]
[ h5
[]
[ h5
[]
[ a
[ onClick (Lagun.ExpansionToggled (Set.remove pathName expanded))
, href ("#" ++ pathName)
, name pathName
]
[ fontAwesome "minus-square-o" ]
, text (" " ++ pathName)
[ a
[ onClick (Lagun.ExpansionToggled (Set.remove pathName expanded))
, href ("#" ++ pathName)
, name pathName
]
, operationList paramValues pathName ops results
[ fontAwesome "minus-square-o" ]
, text (" " ++ pathName)
]
, opsList
]

renderPath : String -> Set String -> Html Msg -> Html Msg
renderPath pathName expanded opsList =
case (Set.member pathName expanded) of
False ->
renderHiddenPath pathName expanded

True ->
renderExpandedPath pathName expanded opsList

pathEntry : ParameterValues -> Set String -> RequestResults -> ( String, Operations ) -> Html Msg
pathEntry paramValues expanded results ( p, ops ) =

pathEntry : String -> Set String -> Html Msg -> Html Msg
pathEntry pathName expanded opsList =
dt
[]
[ (renderPath paramValues expanded results ( p, ops ))
[ (renderPath pathName expanded opsList)
]


pathList : ParameterValues -> Paths -> Set String -> RequestResults -> Html Msg
pathList paramValues paths expanded results =
pathList : Spec -> ParameterValues -> Set String -> RequestResults -> Html Msg
pathList spec paramValues expanded results =
div
[]
[ dl [] (List.map (pathEntry paramValues expanded results) (Dict.toList paths)) ]
[ dl
[]
(List.map
(\(pathName, ops) ->
pathEntry pathName expanded (operationList ("http://" ++ spec.host ++ spec.basePath) paramValues pathName ops results))
(Dict.toList spec.paths))
]


fontAwesome : String -> Html msg
Expand Down
4 changes: 2 additions & 2 deletions css/lagun.css
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@
background: #f4f5f6;
border-radius: 0.4rem;
border: 1px solid #ccc;
padding: 0.2rem 0.5rem;
margin: 0 0.2rem;
}

.code-text {
background: transparent;
white-space: pre-wrap;
padding: 0.2rem 0.5rem;
margin: 0 0.2rem;
}

0 comments on commit 32cf74d

Please sign in to comment.