Skip to content

Commit

Permalink
refactor: Migrate Internal messages package classes to kotlin Part 2
Browse files Browse the repository at this point in the history
  • Loading branch information
Mansi-mParticle committed Nov 25, 2024
1 parent 0a4d64b commit e1cfff8
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 117 deletions.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package com.mparticle.internal.messages

import com.mparticle.MParticle
import com.mparticle.identity.AliasRequest
import com.mparticle.internal.ConfigManager
import com.mparticle.internal.Constants.MessageKey
import org.json.JSONException
import org.json.JSONObject
import java.util.UUID

class MPAliasMessage : JSONObject {
constructor(jsonString: String) : super(jsonString)

@Throws(JSONException::class)
constructor(request: AliasRequest, deviceApplicationStamp: String, apiKey: String) {
val environment = getStringValue(ConfigManager.getEnvironment())
val requestId = UUID.randomUUID().toString()

val dataJson = JSONObject()
.put(MessageKey.SOURCE_MPID, request.sourceMpid)
.put(MessageKey.DESTINATION_MPID, request.destinationMpid)
.put(MessageKey.DEVICE_APPLICATION_STAMP_ALIAS, deviceApplicationStamp)

if (request.startTime != 0L) {
dataJson.put(MessageKey.START_TIME, request.startTime)
}
if (request.endTime != 0L) {
dataJson.put(MessageKey.END_TIME, request.endTime)
}

put(MessageKey.DATA, dataJson)
put(MessageKey.REQUEST_TYPE, MessageKey.ALIAS_REQUEST_TYPE)
put(MessageKey.REQUEST_ID, requestId)
put(MessageKey.ENVIRONMENT_ALIAS, environment)
put(MessageKey.API_KEY, apiKey)
}

@get:Throws(JSONException::class)
val aliasRequest: AliasRequest
get() {
val data = getJSONObject(MessageKey.DATA)
return AliasRequest.builder()
.destinationMpid(data.getLong(MessageKey.DESTINATION_MPID))
.sourceMpid(data.getLong(MessageKey.SOURCE_MPID))
.endTime(data.optLong(MessageKey.END_TIME, 0))
.startTime(data.optLong(MessageKey.START_TIME, 0))
.build()
}

@get:Throws(JSONException::class)
val requestId: String
get() = getString(MessageKey.REQUEST_ID)


protected fun getStringValue(environment: MParticle.Environment): String {
return when (environment) {
MParticle.Environment.Development -> "development"
MParticle.Environment.Production -> "production"
else -> ""
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.mparticle.internal.messages

import android.location.Location
import com.mparticle.MParticle
import com.mparticle.internal.Constants
import com.mparticle.internal.InternalSession
import org.json.JSONException

class MPEventMessage protected constructor(builder: Builder, session: InternalSession, location: Location?, mpId: Long) :
BaseMPMessage(builder, session, location, mpId) {
class Builder(messageType: String) : BaseMPMessageBuilder(messageType) {
fun customEventType(eventType: MParticle.EventType): BaseMPMessageBuilder {
try {
put(Constants.MessageKey.EVENT_TYPE, eventType)
} catch (e: JSONException) {
e.printStackTrace()
}
return this
}

@Throws(JSONException::class)
override fun build(session: InternalSession, location: Location?, mpId: Long): BaseMPMessage {
return MPEventMessage(this, session, location, mpId)
}
}
}

0 comments on commit e1cfff8

Please sign in to comment.