Skip to content

Commit 9678c50

Browse files
authored
Add support for sending additional headers with put requests (#66)
* Add support for sending additional headers with put requests * Rename extraHeaders param to headers * Add support for custom headers in delete requests
1 parent 7d0c4f6 commit 9678c50

File tree

1 file changed

+24
-2
lines changed

1 file changed

+24
-2
lines changed

src/main/kotlin/at/bitfire/dav4jvm/DavResource.kt

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -439,14 +439,22 @@ open class DavResource @JvmOverloads constructor(
439439
* @param ifETag value of `If-Match` header to set, or null to omit
440440
* @param ifScheduleTag value of `If-Schedule-Tag-Match` header to set, or null to omit
441441
* @param ifNoneMatch indicates whether `If-None-Match: *` ("don't overwrite anything existing") header shall be sent
442+
* @param headers additional headers to send
442443
* @param callback called with server response unless an exception is thrown
443444
*
444445
* @throws IOException on I/O error
445446
* @throws HttpException on HTTP error
446447
* @throws DavException on HTTPS -> HTTP redirect
447448
*/
448449
@Throws(IOException::class, HttpException::class)
449-
fun put(body: RequestBody, ifETag: String? = null, ifScheduleTag: String? = null, ifNoneMatch: Boolean = false, callback: ResponseCallback) {
450+
fun put(
451+
body: RequestBody,
452+
ifETag: String? = null,
453+
ifScheduleTag: String? = null,
454+
ifNoneMatch: Boolean = false,
455+
headers: Map<String, String> = emptyMap(),
456+
callback: ResponseCallback
457+
) {
450458
followRedirects {
451459
val builder = Request.Builder()
452460
.put(body)
@@ -462,6 +470,10 @@ open class DavResource @JvmOverloads constructor(
462470
// don't overwrite anything existing
463471
builder.header("If-None-Match", "*")
464472

473+
// Add custom headers
474+
for ((key, value) in headers)
475+
builder.header(key, value)
476+
465477
httpClient.newCall(builder.build()).execute()
466478
}.use { response ->
467479
checkStatus(response)
@@ -477,6 +489,7 @@ open class DavResource @JvmOverloads constructor(
477489
*
478490
* @param ifETag value of `If-Match` header to set, or null to omit
479491
* @param ifScheduleTag value of `If-Schedule-Tag-Match` header to set, or null to omit
492+
* @param headers additional headers to send
480493
* @param callback called with server response unless an exception is thrown
481494
*
482495
* @throws IOException on I/O error
@@ -485,7 +498,12 @@ open class DavResource @JvmOverloads constructor(
485498
* @throws DavException on HTTPS -> HTTP redirect
486499
*/
487500
@Throws(IOException::class, HttpException::class)
488-
fun delete(ifETag: String? = null, ifScheduleTag: String? = null, callback: ResponseCallback) {
501+
fun delete(
502+
ifETag: String? = null,
503+
ifScheduleTag: String? = null,
504+
headers: Map<String, String> = emptyMap(),
505+
callback: ResponseCallback
506+
) {
489507
followRedirects {
490508
val builder = Request.Builder()
491509
.delete()
@@ -495,6 +513,10 @@ open class DavResource @JvmOverloads constructor(
495513
if (ifScheduleTag != null)
496514
builder.header("If-Schedule-Tag-Match", QuotedStringUtils.asQuotedString(ifScheduleTag))
497515

516+
// Add custom headers
517+
for ((key, value) in headers)
518+
builder.header(key, value)
519+
498520
httpClient.newCall(builder.build()).execute()
499521
}.use { response ->
500522
checkStatus(response)

0 commit comments

Comments
 (0)