-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #92 from Parsely/coroutines-send-event
Coroutines: send events flow
- Loading branch information
Showing
15 changed files
with
453 additions
and
352 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
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
51 changes: 51 additions & 0 deletions
51
parsely/src/main/java/com/parsely/parselyandroid/FlushQueue.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,51 @@ | ||
package com.parsely.parselyandroid | ||
|
||
import com.parsely.parselyandroid.JsonSerializer.toParselyEventsPayload | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.launch | ||
import kotlinx.coroutines.sync.Mutex | ||
import kotlinx.coroutines.sync.withLock | ||
|
||
internal class FlushQueue( | ||
private val flushManager: FlushManager, | ||
private val repository: QueueRepository, | ||
private val restClient: RestClient, | ||
private val scope: CoroutineScope | ||
) { | ||
|
||
private val mutex = Mutex() | ||
|
||
operator fun invoke(skipSendingEvents: Boolean) { | ||
scope.launch { | ||
mutex.withLock { | ||
val eventsToSend = repository.getStoredQueue() | ||
|
||
if (eventsToSend.isEmpty()) { | ||
flushManager.stop() | ||
return@launch | ||
} | ||
|
||
if (skipSendingEvents) { | ||
ParselyTracker.PLog("Debug mode on. Not sending to Parse.ly. Otherwise, would sent ${eventsToSend.size} events") | ||
repository.remove(eventsToSend) | ||
return@launch | ||
} | ||
ParselyTracker.PLog("Sending request with %d events", eventsToSend.size) | ||
val jsonPayload = toParselyEventsPayload(eventsToSend) | ||
ParselyTracker.PLog("POST Data %s", jsonPayload) | ||
ParselyTracker.PLog("Requested %s", ParselyTracker.ROOT_URL) | ||
restClient.send(jsonPayload) | ||
.fold( | ||
onSuccess = { | ||
ParselyTracker.PLog("Pixel request success") | ||
repository.remove(eventsToSend) | ||
}, | ||
onFailure = { | ||
ParselyTracker.PLog("Pixel request exception") | ||
ParselyTracker.PLog(it.toString()) | ||
} | ||
) | ||
} | ||
} | ||
} | ||
} |
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
32 changes: 32 additions & 0 deletions
32
parsely/src/main/java/com/parsely/parselyandroid/JsonSerializer.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,32 @@ | ||
package com.parsely.parselyandroid | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper | ||
import java.io.IOException | ||
import java.io.StringWriter | ||
|
||
internal object JsonSerializer { | ||
|
||
fun toParselyEventsPayload(eventsToSend: List<Map<String, Any?>?>): String { | ||
val batchMap: MutableMap<String, Any> = HashMap() | ||
batchMap["events"] = eventsToSend | ||
return toJson(batchMap).orEmpty() | ||
} | ||
/** | ||
* Encode an event Map as JSON. | ||
* | ||
* @param map The Map object to encode as JSON. | ||
* @return The JSON-encoded value of `map`. | ||
*/ | ||
private fun toJson(map: Map<String, Any>): String? { | ||
val mapper = ObjectMapper() | ||
var ret: String? = null | ||
try { | ||
val strWriter = StringWriter() | ||
mapper.writeValue(strWriter, map) | ||
ret = strWriter.toString() | ||
} catch (e: IOException) { | ||
e.printStackTrace() | ||
} | ||
return ret | ||
} | ||
} |
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
Oops, something went wrong.