Skip to content

Commit f7fe7f4

Browse files
committed
refactor(ads-client): housekeeping — sort fields/methods, add UniFFI defaults, split docs
- Sort struct fields alphabetically (HttpCache) - Sort methods: constructors → pub (alpha) → private (alpha) in HttpCache, AdsClient, MARSClient - Add #[uniffi(default = None)] to optional FFI fields for better consumer ergonomics (iab_content, cache config fields, ttl_seconds) - Replace manual Default impl with #[derive(Default)] (clippy) - Split usage.md into per-language files (JavaScript, Kotlin, Swift) with only consumer-facing examples; update README links
1 parent 127c347 commit f7fe7f4

18 files changed

Lines changed: 579 additions & 512 deletions

File tree

components/ads-client/README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,10 @@ cargo test -p ads-client-integration-tests --test mars test_contract_image_stagi
4444

4545
## Usage
4646

47-
Please refer to `./docs/usage.md` for information on using the component.
47+
Please refer to `./docs/usage.md` for the API reference and type documentation.
48+
49+
Language-specific guides with code examples:
50+
51+
- [Swift](./docs/usage-swift.md)
52+
- [Kotlin](./docs/usage-kotlin.md)
53+
- [JavaScript](./docs/usage-javascript.md)
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# Mozilla Ads Client — JavaScript Usage Guide
2+
3+
This guide covers JavaScript-specific examples for using the Mozilla Ads Client (MAC) via UniFFI bindings. For the full API reference and type documentation, see [usage.md](./usage.md).
4+
5+
---
6+
7+
## Creating a Client
8+
9+
```javascript
10+
const client = MozAdsClientBuilder()
11+
.environment(MozAdsEnvironment.Prod)
12+
.cacheConfig(cache)
13+
.telemetry(telemetry)
14+
.build();
15+
```
16+
17+
---
18+
19+
## Implementing Telemetry
20+
21+
```javascript
22+
class AdsClientTelemetry {
23+
recordBuildCacheError(label, value) {
24+
// Bind to your telemetry system
25+
}
26+
27+
recordClientError(label, value) {
28+
// Bind to your telemetry system
29+
}
30+
31+
recordClientOperationTotal(label) {
32+
// Bind to your telemetry system
33+
}
34+
35+
recordDeserializationError(label, value) {
36+
// Bind to your telemetry system
37+
}
38+
39+
recordHttpCacheOutcome(label, value) {
40+
// Bind to your telemetry system
41+
}
42+
}
43+
```
44+
45+
---
46+
47+
## Configuring the Cache
48+
49+
```javascript
50+
const cache = MozAdsCacheConfig({
51+
dbPath: "/tmp/ads_cache.sqlite",
52+
defaultCacheTtlSeconds: 600, // 10 min
53+
maxSizeMib: 20 // 20 MiB
54+
});
55+
56+
const telemetry = new AdsClientTelemetry();
57+
58+
const client = MozAdsClientBuilder()
59+
.environment(MozAdsEnvironment.Prod)
60+
.cacheConfig(cache)
61+
.telemetry(telemetry)
62+
.build();
63+
```
64+
65+
---
66+
67+
## Per-Request Cache Policy Override
68+
69+
```javascript
70+
// Always fetch from network but only cache for 60 seconds
71+
const options = MozAdsRequestOptions({
72+
cachePolicy: MozAdsRequestCachePolicy({ mode: MozAdsCacheMode.NetworkFirst, ttlSeconds: 60 })
73+
});
74+
75+
// Use it when requesting ads
76+
const placements = client.requestImageAds(configs, options);
77+
```
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
# Mozilla Ads Client — Kotlin Usage Guide
2+
3+
This guide covers Kotlin-specific examples for using the Mozilla Ads Client (MAC) via UniFFI bindings. For the full API reference and type documentation, see [usage.md](./usage.md).
4+
5+
---
6+
7+
## Creating a Client
8+
9+
```kotlin
10+
val client = MozAdsClientBuilder()
11+
.environment(MozAdsEnvironment.PROD)
12+
.cacheConfig(cache)
13+
.telemetry(telemetry)
14+
.build()
15+
```
16+
17+
---
18+
19+
## Implementing Telemetry
20+
21+
```kotlin
22+
import mozilla.appservices.adsclient.MozAdsTelemetry
23+
import org.mozilla.appservices.ads_client.GleanMetrics.AdsClient
24+
25+
class AdsClientTelemetry : MozAdsTelemetry {
26+
override fun recordBuildCacheError(label: String, value: String) {
27+
AdsClient.buildCacheError[label].set(value)
28+
}
29+
30+
override fun recordClientError(label: String, value: String) {
31+
AdsClient.clientError[label].set(value)
32+
}
33+
34+
override fun recordClientOperationTotal(label: String) {
35+
AdsClient.clientOperationTotal[label].add()
36+
}
37+
38+
override fun recordDeserializationError(label: String, value: String) {
39+
AdsClient.deserializationError[label].set(value)
40+
}
41+
42+
override fun recordHttpCacheOutcome(label: String, value: String) {
43+
AdsClient.httpCacheOutcome[label].set(value)
44+
}
45+
}
46+
```
47+
48+
---
49+
50+
## Configuring the Cache
51+
52+
```kotlin
53+
val cache = MozAdsCacheConfig(
54+
dbPath = "/tmp/ads_cache.sqlite",
55+
defaultCacheTtlSeconds = 600L, // 10 min
56+
maxSizeMib = 20L // 20 MiB
57+
)
58+
59+
val telemetry = AdsClientTelemetry()
60+
61+
val client = MozAdsClientBuilder()
62+
.environment(MozAdsEnvironment.PROD)
63+
.cacheConfig(cache)
64+
.telemetry(telemetry)
65+
.build()
66+
```
67+
68+
---
69+
70+
## Per-Request Cache Policy Override
71+
72+
```kotlin
73+
// Always fetch from network but only cache for 60 seconds
74+
val options = MozAdsRequestOptions(
75+
cachePolicy = MozAdsRequestCachePolicy(mode = MozAdsCacheMode.NETWORK_FIRST, ttlSeconds = 60L)
76+
)
77+
78+
// Use it when requesting ads
79+
val placements = client.requestImageAds(configs, options = options)
80+
```
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
# Mozilla Ads Client — Swift Usage Guide
2+
3+
This guide covers Swift-specific examples for using the Mozilla Ads Client (MAC) via UniFFI bindings. For the full API reference and type documentation, see [usage.md](./usage.md).
4+
5+
---
6+
7+
## Creating a Client
8+
9+
```swift
10+
let client = MozAdsClientBuilder()
11+
.environment(environment: .prod)
12+
.cacheConfig(cacheConfig: cache)
13+
.telemetry(telemetry: telemetry)
14+
.build()
15+
```
16+
17+
---
18+
19+
## Implementing Telemetry
20+
21+
```swift
22+
import MozillaRustComponents
23+
import Glean
24+
25+
public final class AdsClientTelemetry: MozAdsTelemetry {
26+
public func recordBuildCacheError(label: String, value: String) {
27+
AdsClientMetrics.buildCacheError[label].set(value)
28+
}
29+
30+
public func recordClientError(label: String, value: String) {
31+
AdsClientMetrics.clientError[label].set(value)
32+
}
33+
34+
public func recordClientOperationTotal(label: String) {
35+
AdsClientMetrics.clientOperationTotal[label].add()
36+
}
37+
38+
public func recordDeserializationError(label: String, value: String) {
39+
AdsClientMetrics.deserializationError[label].set(value)
40+
}
41+
42+
public func recordHttpCacheOutcome(label: String, value: String) {
43+
AdsClientMetrics.httpCacheOutcome[label].set(value)
44+
}
45+
}
46+
```
47+
48+
---
49+
50+
## Configuring the Cache
51+
52+
```swift
53+
let cache = MozAdsCacheConfig(
54+
dbPath: "/tmp/ads_cache.sqlite",
55+
defaultCacheTtlSeconds: 600, // 10 min
56+
maxSizeMib: 20 // 20 MiB
57+
)
58+
59+
let telemetry = AdsClientTelemetry()
60+
61+
let client = MozAdsClientBuilder()
62+
.environment(environment: .prod)
63+
.cacheConfig(cacheConfig: cache)
64+
.telemetry(telemetry: telemetry)
65+
.build()
66+
```
67+
68+
---
69+
70+
## Per-Request Cache Policy Override
71+
72+
```swift
73+
// Always fetch from network but only cache for 60 seconds
74+
let options = MozAdsRequestOptions(
75+
cachePolicy: MozAdsRequestCachePolicy(mode: .networkFirst, ttlSeconds: 60)
76+
)
77+
78+
// Use it when requesting ads
79+
let placements = client.requestImageAds(configs, options: options)
80+
```

0 commit comments

Comments
 (0)