Skip to content

Commit

Permalink
version 3
Browse files Browse the repository at this point in the history
  • Loading branch information
ilyasdirin committed Jan 3, 2021
1 parent 694d187 commit 72f81e5
Show file tree
Hide file tree
Showing 15 changed files with 123 additions and 92 deletions.
3 changes: 3 additions & 0 deletions .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'

android {
compileSdkVersion 29
buildToolsVersion "29.0.2"
compileSdkVersion 30
buildToolsVersion "30.0.2"

defaultConfig {
applicationId "com.idirin.idceptor"
minSdkVersion 16
targetSdkVersion 29
versionCode 2
versionName "0.0.2"
targetSdkVersion 30
versionCode 3
versionName "0.0.3"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
Expand Down
6 changes: 6 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ allprojects {
}
}

ext {
targetSdkVersion = 30
minSdkVersion = 16
buildToolsVersion = '30.0.2'
}

task clean(type: Delete) {
delete rootProject.buildDir
}
17 changes: 12 additions & 5 deletions idceptor/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ group='com.idirin.idceptor'
version='0.0.1'

android {
compileSdkVersion 29
buildToolsVersion "29.0.2"
compileSdkVersion 30
buildToolsVersion "30.0.2"

defaultConfig {
minSdkVersion 16
targetSdkVersion 29
targetSdkVersion 30
versionCode 1
versionName "1.0"

Expand All @@ -32,6 +32,13 @@ android {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'

buildConfigField 'String', 'REST_ENDPOINT', '"https://data.lasercateyes.com/api/"'
buildConfigField 'String', 'CLIENT_ID', '"8D0B85DF-E212-4126-9C3A-AC4EEB98B0B1"'
}
debug {
buildConfigField 'String', 'REST_ENDPOINT', '"https://data-beta.lasercateyes.com/api/"'
buildConfigField 'String', 'CLIENT_ID', '"424F5699-9FBA-4161-AE79-49839455E04A"'
}
}

Expand All @@ -49,8 +56,8 @@ dependencies {
kapt "androidx.room:room-compiler:2.2.5"

//Koin
implementation "org.koin:koin-core:2.0.1"
implementation "org.koin:koin-android:2.0.1"
implementation "org.koin:koin-core:2.1.1"
implementation "org.koin:koin-android:2.1.1"

//Interceptor
implementation "com.squareup.okhttp3:okhttp:3.11.0"
Expand Down
22 changes: 2 additions & 20 deletions idceptor/src/main/java/com/idirin/idceptor/IDCeptor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,7 @@ import okhttp3.internal.http.HttpHeaders
import okio.Buffer
import okio.BufferedSource
import org.koin.android.ext.koin.androidContext
import org.koin.core.context.GlobalContext
import org.koin.core.context.loadKoinModules
import org.koin.core.context.startKoin
import org.koin.core.context.unloadKoinModules
import org.koin.core.context.*
import java.nio.charset.Charset
import java.nio.charset.UnsupportedCharsetException
import java.util.*
Expand All @@ -30,7 +27,7 @@ class IDCeptor(private val application: Application, apiKey: String): Intercepto
IDCeptor.apiKey = apiKey

val modules = listOf(idApiModule, idDbModule, idPrefModule, idConcurrencyModule)
if (GlobalContext.getOrNull() == null) {
if (KoinContextHandler.getOrNull() == null) {
startKoin {
androidContext(application)
modules(modules)
Expand All @@ -44,24 +41,9 @@ class IDCeptor(private val application: Application, apiKey: String): Intercepto
}

companion object {
private val DEFAULT_RETENTION: Period = Period.ONE_WEEK
private val UTF8 = Charset.forName("UTF-8")

lateinit var apiKey: String

enum class Period {
/** Retain data for the last hour. */
ONE_HOUR,

/** Retain data for the last day. */
ONE_DAY,

/** Retain data for the last week. */
ONE_WEEK,

/** Retain data forever. */
FOREVER
}
}

private val maxContentLength = 250000L
Expand Down
12 changes: 0 additions & 12 deletions idceptor/src/main/java/com/idirin/idceptor/di/ApiModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@ import com.idirin.idceptor.IDCeptor
import com.idirin.idceptor.network.AlgClientIdInterceptor
import com.idirin.idceptor.network.IdApiInterface
import com.idirin.idceptor.network.IdTypeAdapterDate
import com.idirin.idceptor.utils.DeviceUtil
import com.idirin.idceptor.utils.USER_AGENT
import okhttp3.Cache
import okhttp3.Interceptor
import okhttp3.logging.HttpLoggingInterceptor
import org.koin.android.ext.koin.androidApplication
import org.koin.core.module.Module
Expand All @@ -34,20 +31,11 @@ inline fun <reified T> getApi(application: Application, apiKey: String): T {
val cacheSize: Long = 10 * 1024 * 1024 //10mb cache size
val cache = Cache(application.cacheDir, cacheSize)

val userAgentInterceptor = Interceptor { chain ->
val request = chain.request()
val requestWithUserAgent = request.newBuilder()
.header(USER_AGENT, DeviceUtil.userAgent)
.build()
chain.proceed(requestWithUserAgent)
}

val okHttpClient = okhttp3.OkHttpClient.Builder()
.connectTimeout(BuildConfig.CONNECTION_TIMEOUT.toLong(), TimeUnit.MILLISECONDS)
.readTimeout(BuildConfig.CONNECTION_TIMEOUT.toLong(), TimeUnit.MILLISECONDS)
.writeTimeout(BuildConfig.CONNECTION_UPLOAD_TIMEOUT.toLong(), TimeUnit.MILLISECONDS)
.cache(cache)
.addInterceptor(userAgentInterceptor)
.addInterceptor(AlgClientIdInterceptor(apiKey))

if (BuildConfig.DEBUG) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.idirin.idceptor.models.network

data class AppInitRequest(
val operatingSystem: String,
val name: String?,
val environment: String?,
val version: String,
val buildNumber: String,
val device: DeviceModel
)

data class DeviceModel(
val name: String,
val userFriendlyName: String,
val uuid: String,
val operatingSystem: String,
val osVersion: String
)
Original file line number Diff line number Diff line change
@@ -1,25 +1,22 @@
package com.idirin.idceptor.models.network

import com.idirin.idceptor.utils.AppUtil
import com.idirin.idceptor.utils.DeviceUtil
import java.util.*


data class PostApiRequest(
val applicationId: String = AppUtil.appId,
val connectionId: String,
val deviceId: String = DeviceUtil.deviceId,
val requestPackage: RequestPackageModel? = null,
val responsePackage: ResponsePackageModel? = null,
val sender: String? = null,
val tags: List<String>? = null,
val text: String? = null
val connectionId: String,
val deviceUUID: String = AppUtil.getDeviceId(),
val requestPackage: RequestPackageModel? = null,
val responsePackage: ResponsePackageModel? = null,
val sender: String? = null,
val tags: List<String>? = null,
val text: String? = null
)

data class RequestPackageModel(
val id: String,
val body: String? = null,
val header: String,
val headers: List<String>,
val methodType: String,
val timeStamp: Date,
val url: String
Expand All @@ -28,7 +25,7 @@ data class RequestPackageModel(
data class ResponsePackageModel(
val id: String,
val body: String?,
val header: String,
val headers: List<String>,
val statusCode: Int,
val timeStamp: Date
)
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.idirin.idceptor.network

import com.idirin.idceptor.BuildConfig
import com.idirin.idceptor.utils.AppUtil.getDeviceId
import okhttp3.Interceptor
import okhttp3.Response
import org.koin.core.KoinComponent
Expand All @@ -10,16 +12,16 @@ import java.io.IOException
* idirin on 01.04.2020...
*/

class AlgClientIdInterceptor(private val apiKey: String) : Interceptor, KoinComponent {

//private const val client_secret = "424F5699-9FBA-4161-AE79-49839455E04A"
class AlgClientIdInterceptor(private val appKey: String) : Interceptor, KoinComponent {

@Throws(IOException::class)
override fun intercept(chain: Interceptor.Chain): Response {
val original = chain.request()

val request = original.newBuilder()
.addHeader("Alg-Client-Id", apiKey)
.addHeader("Alg-Client-Id", BuildConfig.CLIENT_ID)
.addHeader("Alg-App-Key", appKey)
.addHeader("Alg-Device-UUId", getDeviceId())
.method(original.method(), original.body())
.build()

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.idirin.idceptor.network

import com.idirin.idceptor.models.network.AppInitRequest
import com.idirin.idceptor.models.network.PostApiRequest
import kotlinx.coroutines.Deferred
import okhttp3.ResponseBody
Expand All @@ -13,12 +14,12 @@ import retrofit2.http.*

interface IdApiInterface {

@POST("Data/Test")
@POST("Data/SendPackage")
fun postApi(@Body request: PostApiRequest): Call<ResponseBody>




@PUT("App/UpdateSubApp")
fun initApp(@Body request: AppInitRequest): Call<ResponseBody>


}
Expand Down
49 changes: 26 additions & 23 deletions idceptor/src/main/java/com/idirin/idceptor/network/UploadHelper.kt
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package com.idirin.idceptor.network

import com.google.gson.Gson
import android.app.Application
import android.os.Build
import com.idirin.idceptor.db.DbHelper
import com.idirin.idceptor.models.HttpTransaction
import com.idirin.idceptor.models.isRequest
import com.idirin.idceptor.models.network.PostApiRequest
import com.idirin.idceptor.models.network.RequestPackageModel
import com.idirin.idceptor.models.network.ResponsePackageModel
import com.idirin.idceptor.utils.AppUtil
import com.idirin.idceptor.utils.DeviceUtil
import com.idirin.idceptor.models.network.*
import com.idirin.idceptor.utils.*
import com.idirin.idceptor.utils.AppUtil.getDeviceId
import kotlinx.coroutines.*
import org.koin.core.KoinComponent
import org.koin.core.inject
Expand All @@ -18,19 +17,18 @@ import java.util.concurrent.atomic.AtomicBoolean

object UploadHelper: KoinComponent {

private val app: Application by inject()
private val api: IdApiInterface by inject()
private val scope: CoroutineScope by inject()
private val syncThread: ExecutorCoroutineDispatcher by inject()

private var isRequesting: AtomicBoolean = AtomicBoolean(true)

private val gson by lazy { Gson() }

private const val RETRY_DELAY = 60_000L

fun init() {
isRequesting.set(true)
postDeviceInfo()
postAppInfo()
DbHelper.clean()

scope.launch(Dispatchers.Main) {
Expand Down Expand Up @@ -67,13 +65,11 @@ object UploadHelper: KoinComponent {
val uploadTime = System.currentTimeMillis()
val request = PostApiRequest(
connectionId = transaction.transactionId,
applicationId = AppUtil.appId,
deviceId = DeviceUtil.deviceId,
requestPackage = RequestPackageModel(
id = transaction.transactionId,
timeStamp = transaction.requestDate!!,
url = transaction.url!!,
header = gson.toJson(transaction.getRequestHeaders()),
headers = transaction.getRequestHeaders().map { "${it.name} - ${it.value}" },
body = transaction.requestBody,
methodType = transaction.method!!
),
Expand All @@ -85,7 +81,7 @@ object UploadHelper: KoinComponent {
id = transaction.transactionId,
timeStamp = transaction.responseDate!!,
statusCode = transaction.responseCode!!,
header = gson.toJson(transaction.getResponseHeaders()),
headers = transaction.getResponseHeaders().map { "${it.name} - ${it.value}" },
body = transaction.responseBody
)
}
Expand All @@ -104,17 +100,24 @@ object UploadHelper: KoinComponent {

private fun postAppInfo() {
coroutine(async = {
// TODO post AppInfo
// api.postApi(request).await()
upload()
})
}

private fun postDeviceInfo() {
coroutine(async = {
// TODO post Device Info
// api.postApi(request).await()
postAppInfo()
val request = AppInitRequest(
operatingSystem = "Android",
name = "",
environment = "",
version = getVersionName(app),
buildNumber = getVersionCode(app).toString(),
device = DeviceModel(
name = Build.MODEL,
userFriendlyName = Build.PRODUCT,
operatingSystem = "Android",
uuid = getDeviceId(),
osVersion = Build.VERSION.SDK_INT.toString()
)
)

api.initApp(request).await()
upload()
})
}

Expand Down
Loading

0 comments on commit 72f81e5

Please sign in to comment.