Skip to content

Commit

Permalink
Add DavResource.get() that returns okhttp Response
Browse files Browse the repository at this point in the history
  • Loading branch information
rfc2822 committed Feb 19, 2024
1 parent 20f4a12 commit 57ce15a
Showing 1 changed file with 31 additions and 13 deletions.
44 changes: 31 additions & 13 deletions src/main/kotlin/at/bitfire/dav4jvm/DavResource.kt
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,36 @@ open class DavResource @JvmOverloads constructor(
}
}

/**
* Sends a GET request to the resource. Follows up to [MAX_REDIRECTS] redirects.
*
* Note: Add `Accept-Encoding: identity` to [headers] if you want to disable compression
* (compression might change the returned ETag).
*
* @param accept value of `Accept` header (always sent for clarity; use */* if you don't care)
* @param headers additional headers to send with the request
*
* @return okhttp Response – **caller is responsible for closing it!**
*
* @throws IOException on I/O error
* @throws HttpException on HTTP error
* @throws DavException on HTTPS -> HTTP redirect
*/
fun get(accept: String, headers: Headers?) =
followRedirects {
val request = Request.Builder()
.get()
.url(location)

if (headers != null)
request.headers(headers)

// always Accept header
request.header("Accept", accept)

httpClient.newCall(request.build()).execute()
}

/**
* Sends a GET request to the resource. Sends `Accept-Encoding: identity` to disable
* compression, because compression might change the ETag.
Expand Down Expand Up @@ -335,19 +365,7 @@ open class DavResource @JvmOverloads constructor(
* @throws DavException on HTTPS -> HTTP redirect
*/
fun get(accept: String, headers: Headers?, callback: ResponseCallback) {
followRedirects {
val request = Request.Builder()
.get()
.url(location)

if (headers != null)
request.headers(headers)

// always Accept header
request.header("Accept", accept)

httpClient.newCall(request.build()).execute()
}.use { response ->
get(accept, headers).use { response ->
checkStatus(response)
callback.onResponse(response)
}
Expand Down

0 comments on commit 57ce15a

Please sign in to comment.