Fetch paginated GitLab objects
This commit is contained in:
parent
98d6ebcef4
commit
b73272864b
2 changed files with 75 additions and 30 deletions
75
src/App.elm
75
src/App.elm
|
@ -2,6 +2,7 @@ module App exposing (..)
|
||||||
|
|
||||||
import Gitlab
|
import Gitlab
|
||||||
import Html
|
import Html
|
||||||
|
import Paginated
|
||||||
import RemoteData
|
import RemoteData
|
||||||
|
|
||||||
|
|
||||||
|
@ -15,13 +16,17 @@ client =
|
||||||
Gitlab.Client "http://git.phoenixinquis.net" (Just "AuGrRWBdXfSJbm36itUG")
|
Gitlab.Client "http://git.phoenixinquis.net" (Just "AuGrRWBdXfSJbm36itUG")
|
||||||
|
|
||||||
|
|
||||||
|
type alias ObjectsResponse =
|
||||||
|
RemoteData.WebData (Paginated.Response Gitlab.Object)
|
||||||
|
|
||||||
|
|
||||||
type alias Model =
|
type alias Model =
|
||||||
{ objects : RemoteData.WebData (List Gitlab.Object) }
|
{ objects : ObjectsResponse }
|
||||||
|
|
||||||
|
|
||||||
type Msg
|
type Msg
|
||||||
= GetObjects
|
= GetObjects
|
||||||
| GotObjects (RemoteData.WebData (List Gitlab.Object))
|
| GotObjects ObjectsResponse
|
||||||
|
|
||||||
|
|
||||||
main : Program Never Model Msg
|
main : Program Never Model Msg
|
||||||
|
@ -44,10 +49,33 @@ init =
|
||||||
update : Msg -> Model -> ( Model, Cmd Msg )
|
update : Msg -> Model -> ( Model, Cmd Msg )
|
||||||
update msg model =
|
update msg model =
|
||||||
case msg of
|
case msg of
|
||||||
GotObjects objects ->
|
GotObjects data ->
|
||||||
( { model | objects = objects }
|
let
|
||||||
, Cmd.none
|
updatePaginated new =
|
||||||
)
|
let
|
||||||
|
updated =
|
||||||
|
Paginated.update
|
||||||
|
(RemoteData.toMaybe model.objects)
|
||||||
|
new
|
||||||
|
in
|
||||||
|
case updated of
|
||||||
|
Paginated.Partial options items ->
|
||||||
|
( updated
|
||||||
|
, Paginated.request options
|
||||||
|
|> Paginated.httpRequest
|
||||||
|
|> RemoteData.sendRequest
|
||||||
|
|> Cmd.map GotObjects
|
||||||
|
)
|
||||||
|
|
||||||
|
Paginated.Complete items ->
|
||||||
|
( updated, Cmd.none )
|
||||||
|
|
||||||
|
( objects, cmd ) =
|
||||||
|
RemoteData.update
|
||||||
|
updatePaginated
|
||||||
|
data
|
||||||
|
in
|
||||||
|
( { model | objects = objects }, cmd )
|
||||||
|
|
||||||
_ ->
|
_ ->
|
||||||
( model, Cmd.none )
|
( model, Cmd.none )
|
||||||
|
@ -61,21 +89,39 @@ view model =
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
viewFiles : RemoteData.WebData (List Gitlab.Object) -> Html.Html Msg
|
viewFiles : ObjectsResponse -> Html.Html Msg
|
||||||
viewFiles data =
|
viewFiles data =
|
||||||
case data of
|
case data of
|
||||||
RemoteData.NotAsked ->
|
RemoteData.NotAsked ->
|
||||||
Html.div [] [ Html.text "Not loaded." ]
|
Html.div [] [ Html.text "Not loaded." ]
|
||||||
|
|
||||||
RemoteData.Loading ->
|
|
||||||
Html.div [] [ Html.text "Loading..." ]
|
|
||||||
|
|
||||||
RemoteData.Failure _ ->
|
RemoteData.Failure _ ->
|
||||||
Html.div [] [ Html.text "Whoops." ]
|
Html.div [] [ Html.text "Whoops." ]
|
||||||
|
|
||||||
RemoteData.Success files ->
|
RemoteData.Loading ->
|
||||||
Html.ul [] <|
|
Html.div [] [ Html.text "Loading objects..." ]
|
||||||
List.map (\x -> Html.li [] [ viewFile x ]) files
|
|
||||||
|
RemoteData.Success (Paginated.Partial _ objects) ->
|
||||||
|
Html.div []
|
||||||
|
[ Html.text <|
|
||||||
|
"Loading objects... ("
|
||||||
|
++ (toString (List.length objects))
|
||||||
|
++ " )"
|
||||||
|
]
|
||||||
|
|
||||||
|
RemoteData.Success (Paginated.Complete objects) ->
|
||||||
|
let
|
||||||
|
files =
|
||||||
|
List.filter
|
||||||
|
(\o ->
|
||||||
|
o.objectType
|
||||||
|
== Gitlab.Blob
|
||||||
|
&& String.endsWith ".gpg" o.name
|
||||||
|
)
|
||||||
|
objects
|
||||||
|
in
|
||||||
|
Html.ul [] <|
|
||||||
|
List.map (\x -> Html.li [] [ viewFile x ]) files
|
||||||
|
|
||||||
|
|
||||||
viewFile : Gitlab.Object -> Html.Html Msg
|
viewFile : Gitlab.Object -> Html.Html Msg
|
||||||
|
@ -85,6 +131,7 @@ viewFile file =
|
||||||
|
|
||||||
getObjects : Cmd Msg
|
getObjects : Cmd Msg
|
||||||
getObjects =
|
getObjects =
|
||||||
Gitlab.getFiles repo client
|
Gitlab.getObjects repo client
|
||||||
|
|> Paginated.httpRequest
|
||||||
|> RemoteData.sendRequest
|
|> RemoteData.sendRequest
|
||||||
|> Cmd.map GotObjects
|
|> Cmd.map GotObjects
|
||||||
|
|
|
@ -4,6 +4,7 @@ import Http
|
||||||
import Json.Decode exposing (Decoder)
|
import Json.Decode exposing (Decoder)
|
||||||
import Json.Decode.Pipeline exposing (decode, required)
|
import Json.Decode.Pipeline exposing (decode, required)
|
||||||
import Maybe.Extra
|
import Maybe.Extra
|
||||||
|
import Paginated
|
||||||
|
|
||||||
|
|
||||||
type Msg
|
type Msg
|
||||||
|
@ -40,19 +41,19 @@ type alias Path =
|
||||||
List String
|
List String
|
||||||
|
|
||||||
|
|
||||||
request : String -> Path -> Decoder a -> Client -> Http.Request a
|
request : String -> Path -> Decoder a -> Client -> Paginated.Request a
|
||||||
request method path decoder client =
|
request method path decoder client =
|
||||||
let
|
let
|
||||||
url =
|
url =
|
||||||
apiUrl client path
|
apiUrl client path
|
||||||
in
|
in
|
||||||
Http.request
|
Paginated.request
|
||||||
{ method = method
|
{ method = method
|
||||||
, headers = apiHeaders client
|
, headers = apiHeaders client
|
||||||
, url = url
|
, url = url
|
||||||
, body = Http.emptyBody
|
, body = Http.emptyBody
|
||||||
, expect = Http.expectJson decoder
|
, decoder = decoder
|
||||||
, timeout = Maybe.Nothing
|
, timeout = Nothing
|
||||||
, withCredentials = False
|
, withCredentials = False
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -71,13 +72,13 @@ apiHeaders client =
|
||||||
[ Maybe.map (Http.header "Private-Token") client.token ]
|
[ Maybe.map (Http.header "Private-Token") client.token ]
|
||||||
|
|
||||||
|
|
||||||
get : Path -> Decoder a -> Client -> Http.Request a
|
get : Path -> Decoder a -> Client -> Paginated.Request a
|
||||||
get =
|
get =
|
||||||
request "GET"
|
request "GET"
|
||||||
|
|
||||||
|
|
||||||
getFiles : Repo -> Client -> Http.Request (List Object)
|
getObjects : Repo -> Client -> Paginated.Request Object
|
||||||
getFiles repo client =
|
getObjects repo client =
|
||||||
let
|
let
|
||||||
typeFromString s =
|
typeFromString s =
|
||||||
case s of
|
case s of
|
||||||
|
@ -91,20 +92,17 @@ getFiles repo client =
|
||||||
Tree
|
Tree
|
||||||
|
|
||||||
decoder =
|
decoder =
|
||||||
Json.Decode.list
|
decode Object
|
||||||
(decode Object
|
|> required "id" Json.Decode.string
|
||||||
|> required "id" Json.Decode.string
|
|> required "name" Json.Decode.string
|
||||||
|> required "name" Json.Decode.string
|
|> required "type" (Json.Decode.map typeFromString Json.Decode.string)
|
||||||
|> required "type" (Json.Decode.map typeFromString Json.Decode.string)
|
|> required "path" Json.Decode.string
|
||||||
|> required "path" Json.Decode.string
|
|
||||||
)
|
|
||||||
|> Json.Decode.map (List.filter (\x -> x.objectType == Blob))
|
|
||||||
in
|
in
|
||||||
get
|
get
|
||||||
[ "projects"
|
[ "projects"
|
||||||
, Http.encodeUri (repo.owner ++ "/" ++ repo.name)
|
, Http.encodeUri (repo.owner ++ "/" ++ repo.name)
|
||||||
, "repository"
|
, "repository"
|
||||||
, "tree?recursive=true&per_page=100&page=2"
|
, "tree?recursive=true"
|
||||||
]
|
]
|
||||||
decoder
|
decoder
|
||||||
client
|
client
|
||||||
|
|
Loading…
Reference in a new issue