Skip to content

Commit 1c6d84e

Browse files
More docs, default headers
1 parent 7942a5f commit 1c6d84e

File tree

1 file changed

+27
-5
lines changed

1 file changed

+27
-5
lines changed

libraries/common/io/requests.effekt

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ def each[T](it: EffektAsyncIterator[T]): Unit / emit[T] = {
1818
// Types
1919
// -----
2020

21+
/// HTTP-style Protocol for requests to send.
2122
type Protocol { HTTP(); HTTPS() }
2223
def show(p: Protocol): String = p match {
2324
case HTTP() => "http"
@@ -27,22 +28,33 @@ def defaultPort(p: Protocol): Int = p match {
2728
case HTTP() => 80
2829
case HTTPS() => 443
2930
}
31+
32+
/// HTTP request method
3033
type Method { GET(); POST() }
3134
def show(m: Method): String = m match {
3235
case GET() => "GET"
3336
case POST() => "POST"
3437
}
3538
/// Interface to build HTTP requests.
3639
///
37-
/// Each of method, hostname, path, port, and protocol must be called at least once!
40+
/// Each of method, hostname, path, and protocol must be called at least once!
41+
/// See `uri` for a simple way to fill those.
3842
interface RequestBuilder {
43+
/// HTTP request method to use
3944
def method(method: Method): Unit
45+
/// (optional) user authentication to use, e.g. user:letmein
4046
def auth(authStr: String): Unit
47+
/// hostname of the request, e.g. effekt-lang.org
4148
def hostname(host: String): Unit
49+
/// path of the request, e.g. /index.html
4250
def path(path: String): Unit
51+
/// port to use, defaults to the standard port of the protocol
4352
def port(port: Int): Unit
53+
/// Protocol to use, e.g. HTTP(), HTTPS()
4454
def protocol(proto: Protocol): Unit
55+
/// Add the given request header
4556
def header(key: String, value: String): Unit
57+
/// Write to the body of the request. May only be called once.
4658
def body{ writer: => Unit / emit[Byte] }: Unit
4759
}
4860
/// Interface returned by HTTP requests.
@@ -279,6 +291,7 @@ namespace jsWeb {
279291
}
280292
}
281293

294+
/// Sets the values in the `RequestBuilder` from a given URI string.
282295
def uri(uri: String): Unit / { RequestBuilder, Exception[WrongFormat] } = {
283296
stringBuffer{
284297
try parseURI(uri) with URIBuilder {
@@ -300,12 +313,24 @@ def uri(uri: String): Unit / { RequestBuilder, Exception[WrongFormat] } = {
300313
}
301314
}
302315

316+
def defaultHeaders(): Unit / RequestBuilder = {
317+
if(internal::backend() != "js-web"){
318+
do header("user-agent", "Effekt script / Script written in the Effekt language")
319+
}
320+
// TODO should we add more?
321+
}
322+
303323
namespace internal {
304324
extern pure def backend(): String =
305325
jsNode { "js-node" }
306326
jsWeb { "js-web" }
307327
}
308328

329+
/// Make a HTTP(S) request on the backend-specific implementation,
330+
/// then pass the response to the second block parameter to process.
331+
///
332+
/// The Request body is buffered, the response is streaming (as far
333+
/// as the backend implementation allows).
309334
def request[R]{ body: => Unit / RequestBuilder }{ res: {ResponseReader} => R }: R / Exception[RequestError] = internal::backend() match {
310335
case "js-node" => jsNode::request{body}{res}
311336
case "js-web" => jsWeb::request{body}{res}
@@ -319,10 +344,7 @@ namespace example {
319344
with def res = request{
320345
do method(GET())
321346
uri("https://effekt-lang.org/")
322-
//do hostname("effekt-lang.org")
323-
//do header("user-agent", "Effekt/script") // dont use this on js-web
324-
//do path("/")
325-
// do port(443) // optional
347+
defaultHeaders()
326348
}
327349
if(res.status() == 200){
328350
println("OK")

0 commit comments

Comments
 (0)