aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/program/aedifex/upload.lux
blob: c4572de9d6d1c14ecf2f734a880524b9b0463b52 (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
(.module:
  [lux #*
   ["." host (#+ import:)]
   [abstract
    [monad (#+ Monad do)]]
   [control
    ["." io (#+ IO)]
    ["." try (#+ Try)]
    ["." exception (#+ exception:)]]
   [data
    ["." binary (#+ Binary)]
    ["." text
     ["%" format (#+ format)]
     ["." encoding]]]
   [time
    ["." instant]]
   [world
    [net (#+ URL)
     ["." uri]]]]
  ["." // #_
   ["#." dependency (#+ Repository Dependency)]
   ["#." artifact]])

(type: #export (Action a)
  (IO (Try a)))

(def: #export monad
  (:coerce (Monad Action)
           (try.with io.monad)))

(type: #export User
  Text)

(type: #export Password
  Text)

(def: (url repository dependency)
  (-> Repository Dependency URL)
  (format repository
          uri.separator
          (//artifact.path (get@ #//dependency.artifact dependency))
          "."
          (get@ #//dependency.type dependency)))

(import: java/lang/AutoCloseable
  (close [] #io #try void))

(import: java/io/OutputStream
  (flush [] #io #try void)
  (write [[byte]] #io #try void))

(import: java/lang/String)

(import: java/net/URLConnection
  (setDoOutput [boolean] #io #try void)
  (setRequestProperty [java/lang/String java/lang/String] #io #try void)
  (getOutputStream [] #io #try java/io/OutputStream))

(import: java/net/HttpURLConnection
  (setRequestMethod [java/lang/String] #io #try void)
  (getResponseCode [] #io #try int))

(import: java/net/URL
  (new [java/lang/String])
  (openConnection [] #io #try java/net/URLConnection))

(import: java/util/Base64$Encoder
  (encodeToString [[byte]] java/lang/String))

(import: java/util/Base64
  (#static getEncoder [] java/util/Base64$Encoder))

(exception: #export (failure {code Int})
  (exception.report
   ["Code" (%.int code)]))

(def: (basic-auth user password)
  (-> User Password Text)
  (format "Basic " (java/util/Base64$Encoder::encodeToString (encoding.to-utf8 (format user ":" password))
                                                             (java/util/Base64::getEncoder))))

(def: #export (upload repository user password dependency content)
  (-> Repository User Password Dependency Binary
      (Action Any))
  (do {@ ..monad}
    [connection (|> (..url repository dependency)
                    java/net/URL::new
                    java/net/URL::openConnection)
     #let [connection (:coerce java/net/HttpURLConnection connection)]
     _ (java/net/HttpURLConnection::setRequestMethod "PUT" connection)
     _ (java/net/URLConnection::setDoOutput true connection)
     _ (java/net/URLConnection::setRequestProperty "Authorization" (..basic-auth user password) connection)
     stream (java/net/URLConnection::getOutputStream connection)
     _ (java/io/OutputStream::write content stream)
     _ (java/io/OutputStream::flush stream)
     _ (java/lang/AutoCloseable::close stream)
     code (java/net/HttpURLConnection::getResponseCode connection)]
    (case code
      +200 (wrap [])
      _ (:: io.monad wrap (exception.throw ..failure [code])))))