-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Tiago Nascimento <[email protected]>
- Loading branch information
1 parent
a80779f
commit 3e5df43
Showing
5 changed files
with
85 additions
and
322 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
MobileSdk/src/main/java/com/spruceid/mobile/sdk/OID4VCI.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package com.spruceid.mobile.sdk | ||
|
||
import com.spruceid.mobile.sdk.rs.AsyncHttpClient | ||
import com.spruceid.mobile.sdk.rs.HttpRequest | ||
import com.spruceid.mobile.sdk.rs.HttpResponse | ||
import com.spruceid.mobile.sdk.rs.SyncHttpClient | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.withContext | ||
import java.io.BufferedInputStream | ||
import java.io.DataOutputStream | ||
import java.net.URL | ||
import javax.net.ssl.HttpsURLConnection | ||
|
||
class Oid4vciSyncHttpClient: SyncHttpClient { | ||
override fun httpClient(request: HttpRequest): HttpResponse { | ||
val connection: HttpsURLConnection = URL(request.url).openConnection() as HttpsURLConnection | ||
|
||
connection.requestMethod = request.method | ||
for ((k, v) in request.headers) { | ||
connection.setRequestProperty(k, v) | ||
} | ||
connection.doOutput = true | ||
connection.doInput = true | ||
|
||
val wr = DataOutputStream(connection.outputStream) | ||
wr.write(request.body) | ||
wr.flush() | ||
wr.close() | ||
|
||
val statusCode = connection.responseCode | ||
val stream = BufferedInputStream(connection.inputStream) | ||
val body = stream.readBytes() | ||
stream.close() | ||
|
||
val headers = connection.headerFields.mapValues { it.value.joinToString(",") } | ||
|
||
return HttpResponse( | ||
statusCode = statusCode.toUShort(), | ||
headers = headers, | ||
body = body, | ||
) | ||
} | ||
} | ||
|
||
class Oid4vciAsyncHttpClient: AsyncHttpClient { | ||
override suspend fun httpClient(request: HttpRequest): HttpResponse { | ||
val connection: HttpsURLConnection = | ||
withContext(Dispatchers.IO) { | ||
URL(request.url).openConnection() | ||
} as HttpsURLConnection | ||
|
||
connection.requestMethod = request.method | ||
for ((k, v) in request.headers) { | ||
connection.setRequestProperty(k, v) | ||
} | ||
connection.doOutput = true | ||
connection.doInput = true | ||
|
||
val wr = DataOutputStream(connection.outputStream) | ||
withContext(Dispatchers.IO) { | ||
wr.write(request.body) | ||
wr.flush() | ||
wr.close() | ||
} | ||
|
||
val statusCode = connection.responseCode | ||
val body: ByteArray | ||
withContext(Dispatchers.IO) { | ||
val stream = BufferedInputStream(connection.inputStream) | ||
body = stream.readBytes() | ||
stream.close() | ||
} | ||
val headers = connection.headerFields.mapValues { it.value.joinToString(",") } | ||
|
||
return HttpResponse( | ||
statusCode = statusCode.toUShort(), | ||
headers = headers, | ||
body = body, | ||
) | ||
} | ||
} |
Oops, something went wrong.