-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Generalized HTTP Network Layer (#80)
- Loading branch information
1 parent
b3c2e9c
commit a426a7b
Showing
4 changed files
with
121 additions
and
29 deletions.
There are no files selected for viewing
12 changes: 12 additions & 0 deletions
12
lib/src/main/java/com/metaplex/lib/drivers/network/HttpNetworkDriver.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,12 @@ | ||
/* | ||
* NetworkDriver | ||
* metaplex-android | ||
* | ||
* Created by Funkatronics on 11/6/2022 | ||
*/ | ||
|
||
package com.metaplex.lib.drivers.network | ||
|
||
interface HttpNetworkDriver { | ||
suspend fun makeHttpRequest(request: HttpRequest): String | ||
} |
41 changes: 41 additions & 0 deletions
41
lib/src/main/java/com/metaplex/lib/drivers/network/HttpRequest.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,41 @@ | ||
/* | ||
* HttpRequest | ||
* metaplex-android | ||
* | ||
* Created by Funkatronics on 11/6/2022 | ||
*/ | ||
|
||
package com.metaplex.lib.drivers.network | ||
|
||
enum class HttpMethod(name: String){ | ||
GET("GET"), | ||
POST("POST"), | ||
HEAD("HEAD"), | ||
OPTIONS("OPTIONS"), | ||
PUT("PUT"), | ||
DELETE("DELETE"), | ||
TRACE("TRACE") | ||
} | ||
|
||
interface HttpRequest { | ||
val url: String | ||
val method: String | ||
val properties: Map<String, String> | ||
val body: String? | ||
} | ||
|
||
class HttpGetRequest( | ||
override val url: String, | ||
override val properties: Map<String, String> | ||
) : HttpRequest { | ||
override val method = HttpMethod.GET.name | ||
override val body = null | ||
} | ||
|
||
class HttpPostRequest( | ||
override val url: String, | ||
override val properties: Map<String, String>, | ||
override val body: String? = null | ||
) : HttpRequest { | ||
override val method = HttpMethod.POST.name | ||
} |
55 changes: 55 additions & 0 deletions
55
lib/src/main/java/com/metaplex/lib/drivers/network/JdkHttpDriver.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,55 @@ | ||
/* | ||
* JdkHttpDriver | ||
* metaplex-android | ||
* | ||
* Created by Funkatronics on 11/6/2022 | ||
*/ | ||
|
||
package com.metaplex.lib.drivers.network | ||
|
||
import kotlinx.coroutines.suspendCancellableCoroutine | ||
import java.net.HttpURLConnection | ||
import java.net.URL | ||
|
||
/** | ||
* A [HttpNetworkDriver] implementation using the native JDK [HttpURLConnection] | ||
* | ||
* @see HttpNetworkDriver | ||
* @see java.net.HttpURLConnection | ||
* | ||
* @author Funkatronics | ||
*/ | ||
class JdkHttpDriver : HttpNetworkDriver { | ||
override suspend fun makeHttpRequest(request: HttpRequest): String = | ||
suspendCancellableCoroutine { continuation -> | ||
|
||
with(URL(request.url).openConnection() as HttpURLConnection) { | ||
// config | ||
requestMethod = request.method | ||
request.properties.forEach { (key, value) -> | ||
setRequestProperty(key, value) | ||
} | ||
|
||
// cancellation | ||
continuation.invokeOnCancellation { disconnect() } | ||
|
||
// send request body | ||
request.body?.run { | ||
doOutput = true | ||
outputStream.write(toByteArray(Charsets.UTF_8)) | ||
outputStream.flush() | ||
outputStream.close() | ||
} | ||
|
||
// read response | ||
val responseString = inputStream.bufferedReader().use { it.readText() } | ||
|
||
// TODO: should check response code and/or errorStream for errors | ||
// println("URL : $url") | ||
// println("Response Code : $responseCode") | ||
// println("input stream : $responseString") | ||
|
||
continuation.resumeWith(Result.success(responseString)) | ||
} | ||
} | ||
} |
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