70 lines
2.4 KiB
Plaintext
70 lines
2.4 KiB
Plaintext
package lyng.io.http
|
|
|
|
import lyng.io.http.types
|
|
|
|
/* Mutable request descriptor for programmatic HTTP calls. */
|
|
extern class HttpRequest {
|
|
/* Request method, e.g. GET, POST, PUT, DELETE. */
|
|
var method: String
|
|
/* Absolute URL including scheme. */
|
|
var url: String
|
|
/* Simple request headers map. */
|
|
var headers: Map<String, String>
|
|
/* Optional UTF-8 request body. */
|
|
var bodyText: String?
|
|
/* Optional binary request body. */
|
|
var bodyBytes: Buffer?
|
|
/* Optional total request timeout in milliseconds. */
|
|
var timeoutMillis: Int?
|
|
}
|
|
|
|
/* HTTP response snapshot with cached body accessors. */
|
|
extern class HttpResponse {
|
|
/* Numeric status code, e.g. 200, 404, 500. */
|
|
val status: Int
|
|
/* Human-readable status text when available. */
|
|
val statusText: String
|
|
/* Response headers. */
|
|
val headers: HttpHeaders
|
|
/* Decode response body as text. Cached after first call. */
|
|
fun text(): String
|
|
/* Return response body as bytes. Cached after first call. */
|
|
fun bytes(): Buffer
|
|
}
|
|
|
|
/*
|
|
HTTP/HTTPS client singleton.
|
|
Transport failures include a diagnostic code, request method/URL, underlying cause,
|
|
and recovery advice: HTTP_TLS_FAILED, HTTP_TIMEOUT, HTTP_DNS_FAILED,
|
|
HTTP_CONNECTION_FAILED, HTTP_REQUEST_FAILED. HTTP_ACCESS_DENIED means no grant.
|
|
HTTP_TLS_FAILED occurs before any response: do not retry the URL unchanged;
|
|
use another source. Query strings/userinfo are redacted from the diagnostic target.
|
|
Non-2xx HTTP statuses are returned normally. Cancellation propagates to the host.
|
|
*/
|
|
extern object Http {
|
|
/* True when HTTP client support is available on this runtime. */
|
|
fun isSupported(): Bool
|
|
|
|
/* Execute a request described by `HttpRequest`. */
|
|
fun request(req: HttpRequest): HttpResponse
|
|
|
|
/*
|
|
Convenience GET request.
|
|
`headers...` accepts `MapEntry` values such as `"Accept" => "text/plain"`
|
|
and 2-item lists such as `["Accept", "text/plain"]`.
|
|
*/
|
|
fun get(url: String, headers...): HttpResponse
|
|
|
|
/*
|
|
Convenience POST request with UTF-8 body text.
|
|
`headers...` follows the same rules as `get`.
|
|
*/
|
|
fun post(url: String, bodyText: String = "", contentType: String? = null, headers...): HttpResponse
|
|
|
|
/*
|
|
Convenience POST request with binary body.
|
|
`headers...` follows the same rules as `get`.
|
|
*/
|
|
fun postBytes(url: String, body: Buffer, contentType: String? = null, headers...): HttpResponse
|
|
}
|