-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature(network): introducing Ktor interceptor (#272)
* feat(network): add ktor network plugin (#252) * feat: ktor interceptor * address review comments * ktlint * address review comments * Update build.gradle --------- Co-authored-by: Prateek <[email protected]> * additional fixes (#267) * refactor & fixes * refactor * removed need to add base module as dependency * code restructure * refactor * refactor * move CacheDirectoryProvider to okhttp module * PlutoInterceptor refactored * refactor * PlutoKtorInterceptor refactored * access modifier fixed * gradle fixes * access modifier fixed * mock setting in ktor interceptor * refactor * publish config fixes * removed old custom interceptor logic * data field cleanup * init * content type fix * query param incorrect data fix * sample code refactor * fix(pluto): missing no-op methods added (#271) * fix(pluto): missing no-op methods added * pending fix * added core no-op * renaming * merge conflict fix * sample UI fixes * review comments --------- Co-authored-by: Aditya Kurkure <[email protected]>
- Loading branch information
1 parent
fa247bd
commit 13b6d29
Showing
164 changed files
with
1,181 additions
and
753 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,4 +15,5 @@ ext { | |
composeCompileVersion = '1.4.7' | ||
composeVersion = '1.4.0' | ||
composeMaterial3Version = '1.0.1' | ||
|
||
} |
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
File renamed without changes.
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,51 @@ | ||
plugins { | ||
id 'com.android.library' | ||
id 'kotlin-android' | ||
} | ||
|
||
apply from: "$rootDir/scripts/build/utils.gradle" | ||
apply from: "$rootDir/scripts/publish/module.gradle" | ||
|
||
def verCode, verName, verBuild, verNameShort, verPublish | ||
(verCode, verName, verBuild, verNameShort, verPublish) = genVersion() | ||
|
||
ext { | ||
PUBLISH_GROUP_ID = "com.plutolib.plugins" | ||
PUBLISH_VERSION = verPublish | ||
PUBLISH_ARTIFACT_ID = 'network-no-op' | ||
} | ||
|
||
android { | ||
compileSdkVersion rootProject.compileSdkVersion | ||
buildToolsVersion rootProject.buildToolsVersion | ||
|
||
defaultConfig { | ||
minSdkVersion rootProject.minSdkVersion | ||
targetSdkVersion rootProject.targetSdkVersion | ||
} | ||
|
||
buildTypes { | ||
release { | ||
debuggable true | ||
minifyEnabled false | ||
shrinkResources false | ||
} | ||
} | ||
|
||
compileOptions { | ||
sourceCompatibility JavaVersion.VERSION_1_8 | ||
targetCompatibility JavaVersion.VERSION_1_8 | ||
} | ||
|
||
kotlinOptions { | ||
jvmTarget = JavaVersion.VERSION_1_8.toString() | ||
} | ||
namespace 'com.pluto.plugins.network' | ||
lint { | ||
abortOnError false | ||
} | ||
|
||
} | ||
|
||
dependencies { | ||
} |
File renamed without changes.
24 changes: 24 additions & 0 deletions
24
...ugins/plugins/network/core/lib-no-op/src/main/java/com/pluto/plugins/network/DataModel.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,24 @@ | ||
package com.pluto.plugins.network | ||
|
||
data class RequestData( | ||
val url: String, | ||
val method: String, | ||
val body: ProcessedBody?, | ||
val headers: Map<String, String?>, | ||
val sentTimestamp: Long | ||
) | ||
|
||
data class ResponseData( | ||
val statusCode: Int, | ||
val body: ProcessedBody?, | ||
val headers: Map<String, String?>, | ||
val sentTimestamp: Long, | ||
val receiveTimestamp: Long, | ||
val protocol: String = "", | ||
val fromDiskCache: Boolean = false | ||
) | ||
|
||
data class ProcessedBody( | ||
val body: CharSequence, | ||
val contentType: String | ||
) |
13 changes: 13 additions & 0 deletions
13
...plugins/network/core/lib-no-op/src/main/java/com/pluto/plugins/network/NetworkRecorder.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,13 @@ | ||
package com.pluto.plugins.network | ||
|
||
import java.io.IOException | ||
|
||
@SuppressWarnings("EmptyFunctionBlock", "UnusedPrivateMember") | ||
class NetworkRecorder(private val request: RequestData) { | ||
|
||
fun onError(e: IOException) { | ||
} | ||
|
||
fun onResponse(response: ResponseData) { | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
...ns/plugins/network/core/lib-no-op/src/main/java/com/pluto/plugins/network/PlutoNetwork.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,10 @@ | ||
package com.pluto.plugins.network | ||
|
||
import android.content.Context | ||
|
||
@SuppressWarnings("UnusedPrivateMember", "EmptyFunctionBlock") | ||
object PlutoNetwork { | ||
|
||
internal fun initialize(context: Context) { | ||
} | ||
} |
File renamed without changes.
File renamed without changes.
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
File renamed without changes.
File renamed without changes.
13 changes: 13 additions & 0 deletions
13
...-plugins/plugins/network/core/lib/src/main/java/com/pluto/plugins/network/PlutoNetwork.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,13 @@ | ||
package com.pluto.plugins.network | ||
|
||
import android.content.Context | ||
|
||
object PlutoNetwork { | ||
|
||
var applicationContext: Context? = null | ||
private set | ||
|
||
internal fun initialize(context: Context) { | ||
applicationContext = context.applicationContext | ||
} | ||
} |
File renamed without changes.
50 changes: 50 additions & 0 deletions
50
...plugins/network/core/lib/src/main/java/com/pluto/plugins/network/intercept/NetworkData.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,50 @@ | ||
package com.pluto.plugins.network.intercept | ||
|
||
import com.pluto.plugins.network.internal.Status | ||
import com.pluto.plugins.network.internal.interceptor.logic.mapCode2Message | ||
import io.ktor.http.ContentType | ||
|
||
object NetworkData { | ||
|
||
data class Request( | ||
val url: String, | ||
val method: String, | ||
val body: Body?, | ||
val headers: Map<String, String?>, | ||
val sentTimestamp: Long | ||
) { | ||
internal val isGzipped: Boolean | ||
get() = headers["Content-Encoding"].equals("gzip", ignoreCase = true) | ||
} | ||
|
||
data class Response( | ||
val statusCode: Int, | ||
val body: Body?, | ||
val headers: Map<String, String?>, | ||
val sentTimestamp: Long, | ||
val receiveTimestamp: Long, | ||
val protocol: String = "", | ||
val fromDiskCache: Boolean = false | ||
) { | ||
internal val status: Status | ||
get() = Status(statusCode, mapCode2Message(statusCode)) | ||
val isSuccessful: Boolean | ||
get() = statusCode in 200..299 | ||
internal val isGzipped: Boolean | ||
get() = headers["Content-Encoding"].equals("gzip", ignoreCase = true) | ||
} | ||
|
||
data class Body( | ||
val body: CharSequence, | ||
val contentType: String | ||
) { | ||
private val contentTypeInternal: ContentType = ContentType.parse(contentType) | ||
private val mediaType: String = contentTypeInternal.contentType | ||
internal val mediaSubtype: String = contentTypeInternal.contentSubtype | ||
internal val isBinary: Boolean = BINARY_MEDIA_TYPES.contains(mediaType) | ||
val sizeInBytes: Long = body.length.toLong() | ||
internal val mediaTypeFull: String = "$mediaType/$mediaSubtype" | ||
} | ||
|
||
internal val BINARY_MEDIA_TYPES = listOf("audio", "video", "image", "font") | ||
} |
48 changes: 48 additions & 0 deletions
48
.../network/core/lib/src/main/java/com/pluto/plugins/network/intercept/NetworkInterceptor.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,48 @@ | ||
package com.pluto.plugins.network.intercept | ||
|
||
import com.pluto.plugins.network.internal.ApiCallData | ||
import com.pluto.plugins.network.internal.MockConfig | ||
import com.pluto.plugins.network.internal.interceptor.logic.NetworkCallsRepo | ||
import com.pluto.plugins.network.internal.interceptor.logic.asExceptionData | ||
import com.pluto.plugins.network.internal.mock.logic.MockSettingsRepo | ||
import com.pluto.utilities.DebugLog | ||
import java.io.IOException | ||
import java.util.UUID | ||
|
||
class NetworkInterceptor private constructor(private val request: NetworkData.Request, val option: Option) { | ||
private val getRequestId: String = UUID.nameUUIDFromBytes("${System.currentTimeMillis()}::${request.url}".toByteArray()).toString() | ||
private val apiCallData = ApiCallData(id = getRequestId, interceptorOption = option, request = request) | ||
val requestUrlWithMockInfo: String = MockSettingsRepo.get(request.url, request.method)?.let { | ||
apiCallData.mock = MockConfig(it) | ||
NetworkCallsRepo.set(apiCallData) | ||
it | ||
} ?: run { | ||
request.url | ||
} | ||
|
||
companion object { | ||
fun intercept(request: NetworkData.Request, option: Option = Option()): NetworkInterceptor { | ||
return NetworkInterceptor(request, option) | ||
} | ||
} | ||
|
||
init { | ||
NetworkCallsRepo.set(apiCallData) | ||
} | ||
|
||
fun onError(e: IOException) { | ||
DebugLog.e("pluto.network", "error occurred", e) | ||
apiCallData.exception = e.asExceptionData() | ||
NetworkCallsRepo.set(apiCallData) | ||
} | ||
|
||
fun onResponse(response: NetworkData.Response) { | ||
apiCallData.response = response | ||
NetworkCallsRepo.set(apiCallData) | ||
} | ||
|
||
data class Option( | ||
val name: String = "Custom", | ||
val metadata: HashMap<String, String>? = null | ||
) | ||
} |
File renamed without changes.
38 changes: 38 additions & 0 deletions
38
...ns/plugins/network/core/lib/src/main/java/com/pluto/plugins/network/internal/DataModel.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,38 @@ | ||
package com.pluto.plugins.network.internal | ||
|
||
import com.pluto.plugins.network.intercept.NetworkData | ||
import com.pluto.plugins.network.intercept.NetworkInterceptor | ||
import com.pluto.plugins.network.internal.interceptor.logic.ExceptionData | ||
import com.pluto.plugins.network.internal.share.getCurl | ||
import com.pluto.utilities.list.ListItem | ||
|
||
internal data class MockConfig( | ||
val url: String, | ||
) | ||
|
||
internal data class Status( | ||
val code: Int, | ||
val message: String, | ||
) | ||
|
||
internal class ApiCallData( | ||
val id: String, | ||
val interceptorOption: NetworkInterceptor.Option, | ||
val request: NetworkData.Request, | ||
var response: NetworkData.Response? = null, | ||
var exception: ExceptionData? = null, | ||
var mock: MockConfig? = null, | ||
val isCustomTrace: Boolean = false | ||
) : ListItem() { | ||
val curl: String | ||
get() = request.getCurl() | ||
val responseTime | ||
get() = exception?.timeStamp ?: response?.receiveTimestamp | ||
|
||
override fun isEqual(other: Any): Boolean { | ||
if (other is ApiCallData) { | ||
id == other.id && response == other.response && exception == other.exception | ||
} | ||
return false | ||
} | ||
} |
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
File renamed without changes.
File renamed without changes.
File renamed without changes.
Oops, something went wrong.