Scoop API

scoop:scoop.http.client:0.1.0

src/response.scoop

Selected production source · src/response.scoop

1package scoop.http.client
3import scoop.asyncio.*
4import scoop.http.*
5import scoop.url.Url
7/**
8 * Final response headers plus a single-consumer body. Non-2xx responses are ordinary values until
9 * requireSuccess is called; the caller must close the response.
10 */
11public class HttpResponse internal constructor(internal val call: ClientCall,
12 /**
13 * The final response or accepted handshake head.
14 */
15 public val head: HttpResponseHead) : AsyncCloseable {
16 /**
17 * The final response protocol version.
18 */
19 public val version: HttpVersion get() = head.version
20 /**
21 * The final status code.
22 */
23 public val status: UInt get() = head.status
24 /**
25 * Ordered response fields, retaining original content-coding metadata.
26 */
27 public val fields: Array<HttpField> get() = head.fields
28 /**
29 * The final URL after redirects.
30 */
31 public val url: Url get() = call.request.url
32 /**
33 * Completed redirect steps in order.
34 */
35 public val redirectHistory: Array<HttpRedirect> = call.sequence.history.toArray()
36 /**
37 * The number of retries before this result.
38 */
39 public val retryCount: Long = call.sequence.retryCount
40 /**
41 * The number of origin authentication retries before this result.
42 */
43 public val authenticationCount: Long = call.sequence.authenticationCount
44 /**
45 * The number of proxy authentication retries before this result.
46 */
47 public val proxyAuthenticationCount: Long = call.sequence.proxyAuthenticationCount
48 /**
49 * Whether this response came from network, cache, or validation.
50 */
51 public val cacheStatus: HttpCacheStatus = call.cacheStatus
52 /**
53 * The latest optional attempt-metrics snapshot.
54 */
55 public val metrics: HttpAttemptMetrics? get() = clientMetrics(call)
56 /**
57 * Content-Length from the wire headers, which is not necessarily the decoded body length.
58 */
59 public val wireContentLength: Long? = parseHttpContentLength(head.fields)
60 internal val input: ClientResponseInput = ClientResponseInput(call, clientResponseDecoding(call, head, wireContentLength), head)
61 /**
62 * The single forward-only response-body cursor.
63 */
64 public val body: HttpResponseBody = HttpResponseBody(input, head.fields)
65 /**
66 * Complete trailers after successful body completion, or None while incomplete.
67 */
68 public val trailers: Array<HttpField>? get() = if (body.isComplete) call.responseStream!!.trailers else None
69 /**
70 * Returns this response for status 200..299; otherwise throws HttpStatusException without
71 * replacing the caller's close responsibility.
72 */
73 public fun requireSuccess(): HttpResponse {
74 if (status < 200U || status >= 300U) throw HttpStatusException(head, url)
75 return this
76 }
77 /**
78 * Closes the body and awaits call cleanup, releasing or retiring the underlying
79 * connection/stream.
80 */
81 public override suspend fun close() { body.close() }
84/**
85 * requireSuccess rejected a response outside the 2xx status range; the response head and URL
86 * remain available.
87 */
88public class HttpStatusException public constructor(
89 /**
90 * The rejected final response head.
91 */
92 public val head: HttpResponseHead,
93 /**
94 * The URL that produced the status.
95 */
96 public val url: Url)
97 : Exception(Some("HTTP status " + head.status.toString()))