Skip to content

Commit e23adcd

Browse files
Support basic auth url part
1 parent 903dd2f commit e23adcd

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

libraries/common/io/requests.effekt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ def show(m: Method): String = m match {
3737
/// Each of method, hostname, path, port, and protocol must be called at least once!
3838
interface RequestBuilder {
3939
def method(method: Method): Unit
40+
def auth(authStr: String): Unit
4041
def hostname(host: String): Unit
4142
def path(path: String): Unit
4243
def port(port: Int): Unit
@@ -195,6 +196,7 @@ namespace jsNode {
195196
var reqBody: ByteArray = allocate(0)
196197
try body() with RequestBuilder {
197198
def method(m) = resume(options.js::set("method", m.show))
199+
def auth(a) = resume(options.js::set("auth", a))
198200
def hostname(n) = resume(options.js::set("hostname", n))
199201
def path(p) = resume(options.js::set("path", p))
200202
def port(p) = resume(options.js::set("port", p))
@@ -249,19 +251,22 @@ namespace jsWeb {
249251
val options = js::empty()
250252
options.js::set("headers", js::empty())
251253
var protocol = HTTPS()
254+
var auth = None()
252255
var hostname = ""
253256
var path = "/"
254257
var port = None()
255258
try body() with RequestBuilder {
256259
def method(m) = resume(options.js::set("method", m.show))
260+
def auth(a) = resume(auth = Some(a))
257261
def hostname(n) = resume(hostname = n)
258262
def path(p) = resume(path = p)
259263
def port(p) = resume(port = Some(p))
260264
def header(k, v) = resume(options.js::set("headers", k, v))
261265
def protocol(p) = resume(protocol = p)
262266
def body() = resume{ {wr} => options.js::set("body", collectBytes{wr}) }
263267
}
264-
val url = s"${protocol.show}://${hostname}:${port.getOrElse{ protocol.defaultPort }.show}${path}"
268+
val authStr = auth.map{ a => a ++ "@" }.getOrElse{ "" }
269+
val url = s"${protocol.show}://${authStr}${hostname}:${port.getOrElse{ protocol.defaultPort }.show}${path}"
265270
val res = run(url, options)
266271
if(res.isError) { println(res.genericShow); do raise(RequestError(), "Request failed") }
267272

@@ -284,7 +289,7 @@ def uri(uri: String): Unit / { RequestBuilder, Exception[WrongFormat] } = {
284289
case "HTTPS" => do protocol(HTTPS())
285290
case _ => do raise(WrongFormat(), "Unsupported protocol " ++ sc)
286291
})
287-
def userinfo(u) = <>
292+
def userinfo(u) = resume(do auth(u))
288293
def host(h) = resume(do hostname(h))
289294
def port(p) = resume(do port(p))
290295
def path(p) = resume(do write(p))

0 commit comments

Comments
 (0)