Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(network): code cleanup & publish config fixes #274

Merged
merged 7 commits into from
Sep 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
/**
* Source: chucker - https://github.com/ChuckerTeam/chucker.git
* License: https://github.com/ChuckerTeam/chucker/blob/develop/LICENSE.txt
*/
package com.pluto.plugins.network.internal
package com.pluto.plugin.libinterface

import android.app.Application
import java.io.File
import java.io.IOException
import java.util.concurrent.atomic.AtomicLong

internal object FileFactory {
class FilesInterface(application: Application) {
private val uniqueIdGenerator = AtomicLong()
private val directory: File = application.applicationContext.filesDir

fun create(directory: File) = create(directory, fileName = "pluto-${uniqueIdGenerator.getAndIncrement()}")

private fun create(directory: File, fileName: String): File? = try {
File(directory, fileName).apply {
fun createFile(filename: String = "pluto-${uniqueIdGenerator.getAndIncrement()}"): File? = try {
File(directory, filename).apply {
if (exists() && !delete()) {
throw IOException("Failed to delete file $this")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ class PlutoInterface private constructor(
val libInfo: LibraryInfoInterface
get() = LibraryInfoInterface(get.pluginActivityClass, get.selectorActivityClass)

val files: FilesInterface
get() = FilesInterface(get.application)

fun create(
application: Application,
pluginActivityClass: Class<out FragmentActivity>,
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.pluto.plugins.network.intercept

class NetworkData {

data class Request(
val url: String,
val method: String,
val body: Body?,
val headers: Map<String, String?>,
val sentTimestamp: Long
)

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
) {
val isSuccessful: Boolean
get() = statusCode in 200..299
}

data class Body(
val body: CharSequence,
val contentType: String
) {
val sizeInBytes: Long = body.length.toLong()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.pluto.plugins.network.intercept

import java.io.IOException

@SuppressWarnings("EmptyFunctionBlock", "UnusedPrivateMember")
class NetworkInterceptor private constructor(private val request: NetworkData.Request, option: Option) {

companion object {

@JvmOverloads
fun intercept(request: NetworkData.Request, option: Option = Option()): NetworkInterceptor {
return NetworkInterceptor(request, option)
}
}

fun onError(e: IOException) {
}

fun onResponse(response: NetworkData.Response) {
}

data class Option(
val name: String = "Custom",
val metadata: HashMap<String, String>? = null
)
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ class PlutoNetworkPlugin() : Plugin(ID) {
}

override fun onPluginInstalled() {
PlutoNetwork.initialize(context)
MockSettingsRepo.init(context)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import com.pluto.plugins.network.internal.Status
import com.pluto.plugins.network.internal.interceptor.logic.mapCode2Message
import io.ktor.http.ContentType

object NetworkData {
class NetworkData {

data class Request(
val url: String,
Expand Down Expand Up @@ -46,5 +46,7 @@ object NetworkData {
internal val mediaTypeFull: String = "$mediaType/$mediaSubtype"
}

internal val BINARY_MEDIA_TYPES = listOf("audio", "video", "image", "font")
companion object {
internal val BINARY_MEDIA_TYPES = listOf("audio", "video", "image", "font")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,18 @@ 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) {
class NetworkInterceptor private constructor(private val request: NetworkData.Request, 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 {

/**
* Returns updated request url
*
* if Mock setting is configured, returns mock url
*
* else returns actual request url
*/
val actualOrMockRequestUrl: String = MockSettingsRepo.get(request.url, request.method)?.let {
apiCallData.mock = MockConfig(it)
NetworkCallsRepo.set(apiCallData)
it
Expand All @@ -21,6 +29,8 @@ class NetworkInterceptor private constructor(private val request: NetworkData.Re
}

companion object {

@JvmOverloads
fun intercept(request: NetworkData.Request, option: Option = Option()): NetworkInterceptor {
return NetworkInterceptor(request, option)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def verCode, verName, verBuild, verNameShort, verPublish
ext {
PUBLISH_GROUP_ID = "com.plutolib.plugins"
PUBLISH_VERSION = verPublish
PUBLISH_ARTIFACT_ID = 'network-ktor-no-op'
PUBLISH_ARTIFACT_ID = 'network-interceptor-ktor-no-op'
}

android {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.pluto.plugins.network
package com.pluto.plugins.network.ktor

import io.ktor.client.HttpClient

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def verCode, verName, verBuild, verNameShort, verPublish
ext {
PUBLISH_GROUP_ID = "com.plutolib.plugins"
PUBLISH_VERSION = verPublish
PUBLISH_ARTIFACT_ID = "network-ktor"
PUBLISH_ARTIFACT_ID = "network-interceptor-ktor"
}

android {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
package com.pluto.plugins.network
package com.pluto.plugins.network.ktor

import com.pluto.plugins.network.intercept.NetworkInterceptor
import com.pluto.plugins.network.internal.KtorRequestConverter.convert
import com.pluto.plugins.network.internal.KtorResponseConverter.convert
import com.pluto.plugins.network.ktor.internal.KtorRequestConverter.convert
import com.pluto.plugins.network.ktor.internal.KtorResponseConverter.convert
import io.ktor.client.HttpClient
import io.ktor.client.plugins.HttpSend
import io.ktor.client.plugins.plugin
import io.ktor.client.request.url
import io.ktor.utils.io.errors.IOException

fun HttpClient.addPlutoKtorPlugin() {
fun HttpClient.addPlutoKtorInterceptor() {
// todo add ktor settings block here
plugin(HttpSend).intercept { requestUnBuilt ->
val request = requestUnBuilt.build()
val networkInterceptor = NetworkInterceptor.intercept(request.convert(), NetworkInterceptor.Option(NAME))
val callResult = try {
requestUnBuilt.url(networkInterceptor.requestUrlWithMockInfo)
requestUnBuilt.url(networkInterceptor.actualOrMockRequestUrl)
execute(requestUnBuilt)
} catch (e: IOException) {
networkInterceptor.onError(e)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.pluto.plugins.network.internal
package com.pluto.plugins.network.ktor.internal

import com.pluto.plugins.network.intercept.NetworkData.Body
import com.pluto.plugins.network.intercept.NetworkData.Request
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.pluto.plugins.network.internal
package com.pluto.plugins.network.ktor.internal

import com.pluto.plugins.network.intercept.NetworkData.Body
import com.pluto.plugins.network.intercept.NetworkData.Response
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.pluto.plugins.network.internal
package com.pluto.plugins.network.ktor.internal

import com.pluto.plugins.network.intercept.NetworkData.Request

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.pluto.plugins.network.internal
package com.pluto.plugins.network.ktor.internal

import com.pluto.plugins.network.intercept.NetworkData.Response

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def verCode, verName, verBuild, verNameShort, verPublish
ext {
PUBLISH_GROUP_ID = "com.plutolib.plugins"
PUBLISH_VERSION = verPublish
PUBLISH_ARTIFACT_ID = 'network-okhttp-no-op'
PUBLISH_ARTIFACT_ID = 'network-interceptor-okhttp-no-op'
}

android {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.pluto.plugins.network
package com.pluto.plugins.network.okhttp

import androidx.annotation.Keep
import okhttp3.Interceptor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def verCode, verName, verBuild, verNameShort, verPublish
ext {
PUBLISH_GROUP_ID = "com.plutolib.plugins"
PUBLISH_VERSION = verPublish
PUBLISH_ARTIFACT_ID = "network-okhttp"
PUBLISH_ARTIFACT_ID = "network-interceptor-okhttp"
}

android {
Expand Down

This file was deleted.

This file was deleted.

Loading