diff options
Diffstat (limited to 'opendc-web/opendc-web-client/src')
3 files changed, 35 insertions, 15 deletions
diff --git a/opendc-web/opendc-web-client/src/main/kotlin/org/opendc/web/client/OpenDCClient.kt b/opendc-web/opendc-web-client/src/main/kotlin/org/opendc/web/client/OpenDCClient.kt index 33f2b41e..a34c7864 100644 --- a/opendc-web/opendc-web-client/src/main/kotlin/org/opendc/web/client/OpenDCClient.kt +++ b/opendc-web/opendc-web-client/src/main/kotlin/org/opendc/web/client/OpenDCClient.kt @@ -39,7 +39,7 @@ public class OpenDCClient(client: TransportClient) { * @param baseUrl The base url of the API. * @param auth Helper class for managing authentication. */ - public constructor(baseUrl: URI, auth: AuthController) : this(HttpTransportClient(baseUrl, auth)) + public constructor(baseUrl: URI, auth: AuthController? = null) : this(HttpTransportClient(baseUrl, auth)) /** * A resource for the available projects. diff --git a/opendc-web/opendc-web-client/src/main/kotlin/org/opendc/web/client/runner/OpenDCRunnerClient.kt b/opendc-web/opendc-web-client/src/main/kotlin/org/opendc/web/client/runner/OpenDCRunnerClient.kt index a3cff6c3..e2112b8c 100644 --- a/opendc-web/opendc-web-client/src/main/kotlin/org/opendc/web/client/runner/OpenDCRunnerClient.kt +++ b/opendc-web/opendc-web-client/src/main/kotlin/org/opendc/web/client/runner/OpenDCRunnerClient.kt @@ -40,7 +40,7 @@ public class OpenDCRunnerClient(client: TransportClient) { * @param baseUrl The base url of the API. * @param auth Helper class for managing authentication. */ - public constructor(baseUrl: URI, auth: AuthController) : this(HttpTransportClient(baseUrl, auth)) + public constructor(baseUrl: URI, auth: AuthController? = null) : this(HttpTransportClient(baseUrl, auth)) /** * A resource for the available simulation jobs. diff --git a/opendc-web/opendc-web-client/src/main/kotlin/org/opendc/web/client/transport/HttpTransportClient.kt b/opendc-web/opendc-web-client/src/main/kotlin/org/opendc/web/client/transport/HttpTransportClient.kt index 03b3945f..e407380b 100644 --- a/opendc-web/opendc-web-client/src/main/kotlin/org/opendc/web/client/transport/HttpTransportClient.kt +++ b/opendc-web/opendc-web-client/src/main/kotlin/org/opendc/web/client/transport/HttpTransportClient.kt @@ -42,7 +42,7 @@ import java.nio.file.Paths */ public class HttpTransportClient( private val baseUrl: URI, - private val auth: AuthController, + private val auth: AuthController?, private val client: HttpClient = HttpClient.newHttpClient() ) : TransportClient { /** @@ -58,15 +58,20 @@ public class HttpTransportClient( override fun <T> get(path: String, targetType: TypeReference<T>): T? { val request = HttpRequest.newBuilder(buildUri(path)) .GET() - .also { auth.injectToken(it) } + .also { auth?.injectToken(it) } .build() val response = client.send(request, HttpResponse.BodyHandlers.ofInputStream()) return when (val code = response.statusCode()) { in 200..299 -> mapper.readValue(response.body(), targetType) 401 -> { - auth.refreshToken() - get(path, targetType) + val auth = auth + if (auth != null) { + auth.refreshToken() + get(path, targetType) + } else { + throw IllegalStateException("Authorization required") + } } 404 -> null else -> throw IllegalStateException("Invalid response $code") @@ -80,15 +85,20 @@ public class HttpTransportClient( val request = HttpRequest.newBuilder(buildUri(path)) .POST(HttpRequest.BodyPublishers.ofByteArray(mapper.writeValueAsBytes(body))) .header("Content-Type", "application/json") - .also { auth.injectToken(it) } + .also { auth?.injectToken(it) } .build() val response = client.send(request, HttpResponse.BodyHandlers.ofInputStream()) return when (val code = response.statusCode()) { in 200..299 -> mapper.readValue(response.body(), targetType) 401 -> { - auth.refreshToken() - post(path, body, targetType) + val auth = auth + if (auth != null) { + auth.refreshToken() + post(path, body, targetType) + } else { + throw IllegalStateException("Authorization required") + } } 404 -> null else -> throw IllegalStateException("Invalid response $code") @@ -102,15 +112,20 @@ public class HttpTransportClient( val request = HttpRequest.newBuilder(buildUri(path)) .PUT(HttpRequest.BodyPublishers.ofByteArray(mapper.writeValueAsBytes(body))) .header("Content-Type", "application/json") - .also { auth.injectToken(it) } + .also { auth?.injectToken(it) } .build() val response = client.send(request, HttpResponse.BodyHandlers.ofInputStream()) return when (val code = response.statusCode()) { in 200..299 -> mapper.readValue(response.body(), targetType) 401 -> { - auth.refreshToken() - put(path, body, targetType) + val auth = auth + if (auth != null) { + auth.refreshToken() + put(path, body, targetType) + } else { + throw IllegalStateException("Authorization required") + } } 404 -> null else -> throw IllegalStateException("Invalid response $code") @@ -123,15 +138,20 @@ public class HttpTransportClient( override fun <T> delete(path: String, targetType: TypeReference<T>): T? { val request = HttpRequest.newBuilder(buildUri(path)) .DELETE() - .also { auth.injectToken(it) } + .also { auth?.injectToken(it) } .build() val response = client.send(request, HttpResponse.BodyHandlers.ofInputStream()) return when (val code = response.statusCode()) { in 200..299 -> mapper.readValue(response.body(), targetType) 401 -> { - auth.refreshToken() - delete(path, targetType) + val auth = auth + if (auth != null) { + auth.refreshToken() + delete(path, targetType) + } else { + throw IllegalStateException("Authorization required") + } } 404 -> null else -> throw IllegalStateException("Invalid response $code") |
