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

feat: Custom User agent #88

Merged
merged 3 commits into from
Nov 13, 2024
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ platform (Android or iOS).
Here’s a brief example of how to use the `SwissTransferInjection` class from the Core module:

```kotlin
val core = SwissTransferInjection()
val core = SwissTransferInjection(userAgent = "app_user_agent")
core.loadUser(userId)

val manager = core.transferManager
Expand Down
11 changes: 6 additions & 5 deletions STCore/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ centralized access point to orchestrate transfer operations.

| Type | Name | Description |
|----------|---------------------|-------------------------------------------------------|
| Property | userAgent | Customize client api userAgent. |
| Property | appSettingsManager | A manager used to orchestrate AppSettings operations. |
| Property | transferManager | A manager used to orchestrate Transfers operations. |
| Property | accountManager | A manager used to orchestrate Accounts operations. |
Expand All @@ -52,7 +53,7 @@ centralized access point to orchestrate transfer operations.

- **Usage Example**:
```kotlin
val core = SwissTransferInjection()
val core = SwissTransferInjection(userAgent = "user_agent")
val appSettingsManager = core.appSettingsManager
// Use the appSettingsManager to orchestrate AppSettings
```
Expand All @@ -65,7 +66,7 @@ centralized access point to orchestrate transfer operations.

- **Usage Example**:
```kotlin
val core = SwissTransferInjection()
val core = SwissTransferInjection(userAgent = "user_agent")
val transferManager = core.transferManager
// Use the transferManager to orchestrate Transfers
```
Expand All @@ -80,7 +81,7 @@ centralized access point to orchestrate transfer operations.

- **Usage Example**:
```kotlin
val core = SwissTransferInjection()
val core = SwissTransferInjection(userAgent = "user_agent")
val accountManager = core.accountManager
// Use the accountManager to orchestrate Accounts
```
Expand All @@ -92,7 +93,7 @@ centralized access point to orchestrate transfer operations.

- **Usage Example**:
```kotlin
val core = SwissTransferInjection()
val core = SwissTransferInjection(userAgent = "user_agent")
val uploadManager = core.uploadManager
// Use the uploadManager to orchestrate Uploads
```
Expand All @@ -105,7 +106,7 @@ centralized access point to orchestrate transfer operations.

- **Usage Example**:
```kotlin
val core = SwissTransferInjection()
val core = SwissTransferInjection(userAgent = "user_agent")
val accountManager = core.sharedApiUrlCreator
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,19 @@ import com.infomaniak.multiplatform_swisstransfer.network.repositories.UploadRep
*
* This class serves as the main access point.
*
* @property userAgent Customize client api userAgent.
* @property transferManager A manager used to orchestrate transfer operations.
* @property appSettingsManager A manager used to orchestrate AppSettings operations.
* @property accountManager A manager used to orchestrate Accounts operations.
* @property uploadManager A manager used to orchestrate Uploads operations.
* @property sharedApiUrlCreator An utils to help use shared routes.
*/
class SwissTransferInjection {
class SwissTransferInjection(
private val userAgent: String,
) {

private val realmProvider by lazy { RealmProvider() }
private val apiClientProvider by lazy { ApiClientProvider() }
private val apiClientProvider by lazy { ApiClientProvider(userAgent) }

private val uploadRepository by lazy { UploadRepository(apiClientProvider) }
private val transferRepository by lazy { TransferRepository(apiClientProvider) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import io.ktor.client.engine.HttpClientEngineFactory
import io.ktor.client.plugins.HttpRequestRetry
import io.ktor.client.plugins.HttpResponseValidator
import io.ktor.client.plugins.HttpTimeout
import io.ktor.client.plugins.UserAgent
import io.ktor.client.plugins.compression.ContentEncoding
import io.ktor.client.plugins.contentnegotiation.ContentNegotiation
import io.ktor.client.statement.HttpResponse
Expand All @@ -36,9 +37,15 @@ import io.ktor.serialization.kotlinx.json.json
import kotlinx.io.IOException
import kotlinx.serialization.json.Json

class ApiClientProvider internal constructor(engine: HttpClientEngineFactory<*>? = null) {
class ApiClientProvider internal constructor(
engine: HttpClientEngineFactory<*>? = null,
// When you don't use SwissTransferInjection, you don't have an userAgent, so we're currently setting a default value.
// See later how to improve it.
private val userAgent: String = "Ktor client",
) {

constructor() : this(null)
constructor(userAgent: String) : this(engine = null, userAgent)

val json = Json {
ignoreUnknownKeys = true
Expand All @@ -52,6 +59,9 @@ class ApiClientProvider internal constructor(engine: HttpClientEngineFactory<*>?
fun createHttpClient(engine: HttpClientEngineFactory<*>?): HttpClient {
val block: HttpClientConfig<*>.() -> Unit = {
expectSuccess = true
install(UserAgent) {
agent = userAgent
}
install(ContentNegotiation) {
json(this@ApiClientProvider.json)
}
Expand Down
Loading