1module Sources.Services.Azure.FileParser exposing (parseErrorResponse, parseTreeResponse)
2
3import Sources.Processing exposing (Marker(..), TreeAnswer)
4import Sources.Services.Azure.FileMarker as FileMarker exposing (MarkerItem(..))
5import Sources.Services.Common exposing (cleanPath)
6import Xml.Decode exposing (..)
7
8
9
10-- TREE
11
12
13parseTreeResponse : String -> Marker -> TreeAnswer Marker
14parseTreeResponse response previousMarker =
15 response
16 |> decodeString (treeDecoder previousMarker)
17 |> Result.withDefault { filePaths = [], marker = TheEnd }
18
19
20treeDecoder : Marker -> Decoder (TreeAnswer Marker)
21treeDecoder previousMarker =
22 usedDirectoryDecoder
23 |> map cleanPath
24 |> andThen
25 (\usedDirectory ->
26 map2
27 (\a b -> ( usedDirectory, a, b ))
28 (map (List.map <| String.append usedDirectory) filePathsDecoder)
29 (map (List.map <| String.append usedDirectory) directoryPathsDecoder)
30 )
31 |> andThen
32 (\( usedDirectory, filePaths, directoryPaths ) ->
33 previousMarker
34 |> FileMarker.removeOne
35 |> FileMarker.concat (List.map Directory directoryPaths)
36 |> markerDecoder usedDirectory
37 |> map (\marker -> { filePaths = filePaths, marker = marker })
38 )
39
40
41usedDirectoryDecoder : Decoder String
42usedDirectoryDecoder =
43 stringAttr "DirectoryPath"
44
45
46filePathsDecoder : Decoder (List String)
47filePathsDecoder =
48 string
49 |> single
50 |> path [ "Name" ]
51 |> list
52 |> path [ "Entries", "File" ]
53
54
55directoryPathsDecoder : Decoder (List String)
56directoryPathsDecoder =
57 string
58 |> single
59 |> path [ "Name" ]
60 |> list
61 |> path [ "Entries", "Directory" ]
62
63
64markerDecoder : String -> Marker -> Decoder Marker
65markerDecoder usedDirectory markerWithDirectories =
66 map
67 (\maybeNextMarker ->
68 case maybeNextMarker of
69 Just "" ->
70 markerWithDirectories
71
72 Just marker ->
73 FileMarker.concat
74 [ Param { directory = usedDirectory, marker = marker } ]
75 markerWithDirectories
76
77 Nothing ->
78 markerWithDirectories
79 )
80 (maybe <| path [ "NextMarker" ] <| single string)
81
82
83
84-- ERROR
85
86
87parseErrorResponse : String -> Maybe String
88parseErrorResponse response =
89 response
90 |> decodeString errorMessagesDecoder
91 |> Result.toMaybe
92
93
94errorMessagesDecoder : Decoder String
95errorMessagesDecoder =
96 string
97 |> single
98 |> path [ "Message" ]