aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/program/aedifex/repository/remote.lux
blob: 7d320c2265e403c5aa07b8c683b966549740745a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
... This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
... If a copy of the MPL was not distributed with this file, You can obtain one at https://mozilla.org/MPL/2.0/.

(.require
 [library
  [lux (.except)
   [abstract
    [monad (.only do)]]
   [control
    ["[0]" io (.only IO)]
    ["[0]" try (.only Try)]
    ["[0]" exception (.only Exception)]]
   [data
    ["[0]" product]
    [text
     ["%" \\format (.only format)]]
    [collection
     ["[0]" list]]]
   ["[0]" meta
    ["[1]/[0]" version]
    [compiler
     ["[0]" version]]]
   [world
    [net (.only URL)
     [uri (.only URI)]
     ["[0]" http
      ["[0]" response (.only Response)]
      ["[1]" client]
      ["[1]/[0]" status]
      ["@[1]" /]]]]]]
 ["[0]" // (.only)
  ["[1][0]" identity (.only Identity)]
  ["/[1]" //
   ["[1][0]" artifact (.only Version Artifact)
    [extension (.only Extension)]]]])

(type .public Address
  URL)

(with_template [<name>]
  [(exception.def .public (<name> [url status])
     (Exception [URL Nat])
     (exception.report
      (list ["URL" (%.text url)]
            ["Status Code" (%.nat status)])))]

  [download_failure]
  [upload_failure]
  )

(def .public (uri version_template artifact extension)
  (-> Version Artifact Extension URI)
  (format (///artifact.uri version_template artifact) extension))

(def .public user_agent
  (format "LuxAedifex/" (version.format meta/version.latest)))

(def base_headers
  (List [Text Text])
  (list ["User-Agent" ..user_agent]))

(def .public (repository http identity address)
  (All (_ s) (-> (http.Client IO) (Maybe Identity) Address (//.Repository IO)))
  (implementation
   (def description
     address)
   (def (download uri)
     (do [! (try.with io.monad)]
       [[status message] (is (IO (Try (Response IO)))
                             (http.get (format address uri)
                                       (http.headers ..base_headers)
                                       {.#None}
                                       http))]
       (when status
         http/status.ok
         (of ! each product.right ((the @http.#body message) {.#None}))
         
         _
         (do !
           [_ ((the @http.#body message) {.#Some 0})]
           (of io.monad in (exception.except ..download_failure [(format address uri) status]))))))

   (def (upload uri content)
     (do (try.with io.monad)
       [[status message] (is (IO (Try (Response IO)))
                             (http.put (format address uri)
                                       (http.headers (when identity
                                                       {.#None}
                                                       ..base_headers
                                                       
                                                       {.#Some [user password]}
                                                       (list.partial ["Authorization" (//identity.basic_auth user password)]
                                                                     ..base_headers)))
                                       {.#Some content}
                                       http))
        _ ((the @http.#body message) {.#Some 0})]
       (when status
         http/status.created
         (in [])
         
         _
         (of io.monad in (exception.except ..upload_failure [(format address uri) status])))))
   ))