Skip to content

Commit 19e1b0c

Browse files
committed
#1625 add an authorization header field
1 parent 29200b5 commit 19e1b0c

File tree

9 files changed

+48
-1
lines changed

9 files changed

+48
-1
lines changed

app/src/main/java/io/github/sds100/keymapper/actions/ActionData.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -836,6 +836,7 @@ sealed class ActionData : Comparable<ActionData> {
836836
val method: HttpMethod,
837837
val url: String,
838838
val body: String,
839+
val authorizationHeader: String,
839840
) : ActionData() {
840841
override val id: ActionId = ActionId.HTTP_REQUEST
841842
}

app/src/main/java/io/github/sds100/keymapper/actions/ActionDataEntityMapper.kt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,11 +510,16 @@ object ActionDataEntityMapper {
510510

511511
val body = entity.extras.getData(ActionEntity.EXTRA_HTTP_BODY).valueOrNull() ?: ""
512512

513+
val authorizationHeader =
514+
entity.extras.getData(ActionEntity.EXTRA_HTTP_AUTHORIZATION_HEADER)
515+
.valueOrNull() ?: ""
516+
513517
ActionData.HttpRequest(
514518
description = description,
515519
method = method,
516520
url = url,
517521
body = body,
522+
authorizationHeader = authorizationHeader,
518523
)
519524
}
520525
}
@@ -739,6 +744,10 @@ object ActionDataEntityMapper {
739744
EntityExtra(ActionEntity.EXTRA_HTTP_METHOD, HTTP_METHOD_MAP[data.method]!!),
740745
EntityExtra(ActionEntity.EXTRA_HTTP_URL, data.url),
741746
EntityExtra(ActionEntity.EXTRA_HTTP_BODY, data.body),
747+
EntityExtra(
748+
ActionEntity.EXTRA_HTTP_AUTHORIZATION_HEADER,
749+
data.authorizationHeader,
750+
),
742751
)
743752

744753
else -> emptyList()

app/src/main/java/io/github/sds100/keymapper/actions/CreateActionDelegate.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -779,6 +779,7 @@ class CreateActionDelegate(
779779
method = HttpMethod.GET,
780780
url = "",
781781
body = "",
782+
authorizationHeader = "",
782783
)
783784
} else {
784785
httpRequestBottomSheetState = oldData as? ActionData.HttpRequest

app/src/main/java/io/github/sds100/keymapper/actions/HttpRequestBottomSheet.kt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,22 @@ private fun HttpRequestBottomSheet(
214214

215215
Spacer(modifier = Modifier.height(16.dp))
216216

217+
OutlinedTextField(
218+
modifier = Modifier
219+
.fillMaxWidth()
220+
.padding(horizontal = 16.dp),
221+
value = state.authorizationHeader,
222+
label = { Text(stringResource(R.string.action_http_request_authorization_label)) },
223+
onValueChange = {
224+
onBodyChanged(it)
225+
},
226+
supportingText = {
227+
Text(stringResource(R.string.action_http_request_authorization_supporting_text))
228+
},
229+
)
230+
231+
Spacer(modifier = Modifier.height(16.dp))
232+
217233
Row(
218234
modifier = Modifier
219235
.fillMaxWidth()
@@ -278,6 +294,7 @@ private fun PreviewEmpty() {
278294
method = HttpMethod.GET,
279295
url = "",
280296
body = "",
297+
authorizationHeader = "",
281298
),
282299
)
283300
}
@@ -301,6 +318,7 @@ private fun PreviewFilled() {
301318
method = HttpMethod.GET,
302319
url = "https://example.com",
303320
body = "Hello, world!",
321+
authorizationHeader = "Bearer token",
304322
),
305323
)
306324
}

app/src/main/java/io/github/sds100/keymapper/actions/PerformActionsUseCase.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -820,6 +820,7 @@ class PerformActionsUseCaseImpl(
820820
method = action.method,
821821
url = action.url,
822822
body = action.body,
823+
authorizationHeader = action.authorizationHeader,
823824
).showErrorMessageOnFail()
824825
}
825826

app/src/main/java/io/github/sds100/keymapper/data/entities/ActionEntity.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ data class ActionEntity(
8888
const val EXTRA_HTTP_URL = "extra_http_url"
8989
const val EXTRA_HTTP_BODY = "extra_http_body"
9090
const val EXTRA_HTTP_DESCRIPTION = "extra_http_description"
91+
const val EXTRA_HTTP_AUTHORIZATION_HEADER = "extra_http_authorization_header"
9192

9293
// DON'T CHANGE THESE. Used for JSON serialization and parsing.
9394
const val NAME_ACTION_TYPE = "type"

app/src/main/java/io/github/sds100/keymapper/system/network/AndroidNetworkAdapter.kt

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import kotlinx.coroutines.flow.Flow
1818
import kotlinx.coroutines.flow.MutableStateFlow
1919
import kotlinx.coroutines.flow.update
2020
import kotlinx.coroutines.withContext
21+
import okhttp3.Headers
2122
import okhttp3.OkHttpClient
2223
import okhttp3.Request
2324
import okhttp3.RequestBody.Companion.toRequestBody
@@ -128,7 +129,12 @@ class AndroidNetworkAdapter(
128129
}
129130
}
130131

131-
override suspend fun sendHttpRequest(method: HttpMethod, url: String, body: String): Result<*> {
132+
override suspend fun sendHttpRequest(
133+
method: HttpMethod,
134+
url: String,
135+
body: String,
136+
authorizationHeader: String,
137+
): Result<*> {
132138
try {
133139
val requestBody = when (method) {
134140
HttpMethod.HEAD -> Request.Builder().head()
@@ -139,8 +145,15 @@ class AndroidNetworkAdapter(
139145
HttpMethod.PATCH -> Request.Builder().patch(body.toRequestBody())
140146
}
141147

148+
val headers = Headers.Builder()
149+
150+
if (authorizationHeader.isNotBlank()) {
151+
headers.add("Authorization", authorizationHeader)
152+
}
153+
142154
val request = requestBody
143155
.url("https://posttestserver.dev/p/kmr33yjcz5h38hkq/post")
156+
.headers(headers.build())
144157
.build()
145158

146159
withContext(Dispatchers.IO) { httpClient.newCall(request).execute() }

app/src/main/java/io/github/sds100/keymapper/system/network/NetworkAdapter.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,6 @@ interface NetworkAdapter {
2727
method: HttpMethod,
2828
url: String,
2929
body: String,
30+
authorizationHeader: String,
3031
): Result<*>
3132
}

app/src/main/res/values/strings.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1084,6 +1084,8 @@
10841084
<string name="action_http_request_url_label">URL</string>
10851085
<string name="action_http_request_url_empty_error">Can not be empty!</string>
10861086
<string name="action_http_request_body_label">Request body (optional)</string>
1087+
<string name="action_http_request_authorization_label">Authorization header (optional)</string>
1088+
<string name="action_http_request_authorization_supporting_text">You must prepend \'Bearer\' if necessary</string>
10871089
<!-- action labels -->
10881090

10891091
<!-- action category labels -->

0 commit comments

Comments
 (0)