std:Import
lib/Import.omg
lib/Import.omg106 lines
1{- Where an import is read from.
2
3 `import s` reads the source `s` describes; `s` is a closed expression of
4 type `Source`, evaluated before the file that contains the import is
5 checked. The literal locations are shorthands for values of `Source`:
6
7 ./x.omg is import (Source.File "./x.omg")
8 std:List is import (Source.Std "List")
9 env:CONFIG is import (Source.Env "CONFIG")
10 https://h/x is import (get "https://h/x")
11
12 A request to a server can say more than a URL:
13
14 import
15 ( std:Import.http
16 ( (std:Import.request "GET" "https://example.com/config.omg")
17 with headers = [ std:Import.bearer (std:Import.Secret.Env "TOKEN") ]
18 with tls.ca = Some "/etc/ssl/internal-ca.pem"
19 )
20 )
21
22 A `Secret` is a reference to a value that is read when the request is
23 sent: it is not written in the source, and it is never part of a hash
24 or of the cache.
25-}
26
27-- A value given in a header or as a key: written out (for what is not
28-- secret), or read from an environment variable or a file.
29let Secret = < Text : Text | Env : Text | File : Text >
30
31-- A header is sent as `name: prefix value`.
32let Header = { name : Text, prefix : Text, value : Secret }
33
34-- How a server is authenticated, and how the client authenticates itself.
35--
36-- ca a file of PEM certificates trusted instead of the system's
37-- certificate a file holding the PEM certificate the client presents
38-- (mutual TLS)
39-- key its private key
40-- insecure accept any certificate the server presents: for a test
41-- server
42let Tls =
43 { ca : Optional Text
44 , certificate : Optional Text
45 , key : Optional Secret
46 , insecure : Bool
47 }
48
49let Http =
50 { url : Text
51 , method : Text
52 , headers : List Header
53 , body : Optional Text
54 , tls : Tls
55 }
56
57let Source = < File : Text | Std : Text | Env : Text | Http : Http >
58
59let defaultTls
60 : Tls
61 = { ca = None Text, certificate = None Text, key = None Secret, insecure = False }
62
63-- A request with the given method and nothing else.
64let request
65 : Text -> Text -> Http
66 = \method url ->
67 { url, method, headers = ([] : List Header), body = None Text, tls = defaultTls }
68
69-- `get url` is the source the literal URL stands for.
70let get : Text -> Source = \url -> Source.Http (request "GET" url)
71
72let post
73 : Text -> Text -> Source
74 = \url body -> Source.Http ((request "POST" url) with body = Some body)
75
76let http : Http -> Source = \h -> Source.Http h
77
78let file : Text -> Source = \path -> Source.File path
79
80let std : Text -> Source = \name -> Source.Std name
81
82let env : Text -> Source = \name -> Source.Env name
83
84let header : Text -> Secret -> Header = \name value -> { name, prefix = "", value }
85
86-- `Authorization: Bearer <token>`, the token read from a secret.
87let bearer
88 : Secret -> Header
89 = \token -> { name = "Authorization", prefix = "Bearer ", value = token }
90
91in { Secret
92 , Header
93 , Tls
94 , Http
95 , Source
96 , defaultTls
97 , request
98 , get
99 , post
100 , http
101 , file
102 , std
103 , env
104 , header
105 , bearer
106 }