using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using RudderSdk.Core.Models.Stores;
namespace RudderSdk.Core;
/// In-game stores and offer purchases.
public sealed class StoresService
{
private readonly RudderClient _client;
internal StoresService(RudderClient client) => _client = client;
/// Lists the available stores with their offers.
public async Task> ListAsync(CancellationToken cancellationToken = default)
{
var response = await _client.SendAsync(
"GET",
"/sdk/v1/stores",
cancellationToken).ConfigureAwait(false);
return response?.Stores ?? new List();
}
/// Resolves one store by slug.
public Task GetAsync(string slug, CancellationToken cancellationToken = default)
=> _client.SendAsync("GET", "/sdk/v1/stores/" + Url.Encode(slug), cancellationToken);
///
/// Purchases an offer (charges the wallet). When
/// is null, a random one is generated;
/// pass a stable key to make retries safe.
///
public Task PurchaseAsync(
string storeSlug,
string offerSlug,
string? idempotencyKey = null,
CancellationToken cancellationToken = default)
{
return _client.SendAsync(
"POST",
"/sdk/v1/stores/" + Url.Encode(storeSlug) + "/offers/" + Url.Encode(offerSlug) + "/purchase",
new PurchaseOfferRequest
{
StoreSlug = storeSlug,
OfferSlug = offerSlug,
IdempotencyKey = idempotencyKey ?? Guid.NewGuid().ToString()
},
cancellationToken);
}
}