Skip to content

Extend InventoryUpdate[Request/Response] to support new fields #4462

@ben-beamable

Description

@ben-beamable

Reason for the Feature

In an effort to reduce API calls that our customers need to make, we have optionally allowed them to request a delta response back when making Inventory updates. The delta responses will include enough details to know the before and after states of currencies or items, including properties and whether the item was new/updated/deleted.

Proposed Solution/Requirements

Extend the SDK to support new fields in the request as well as to expect new fields in the response, so that the data can be accessed natively though the SDK.

Alternatives or Workarounds

Customer has to manually write a custom class for serialization in addition to a custom requester request to inventory.

Additional Context

Sample unity code:

using System;
using System.Text;
using Beamable;
using Beamable.Common;
using Beamable.Common.Api;
using UnityEngine;

public class FetchInventoryDeltas : MonoBehaviour
{
    public async void OnClickSendInventoryUpdate()
    {
        try
        {
            var json = await SendInventoryUpdate();
            Debug.Log($"RAW INVENTORY UPDATE RESPONSE:\n{json}");
        }
        catch (Exception ex)
        {
            Debug.LogError(ex);
        }
    }
    
    public async Promise<string> SendInventoryUpdate()
    {
        var ctx = await BeamContext.Default.Instance;
        var userId = ctx.Api.User.id;

        var timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds().ToString();
        // serialization was being a pain and I wanted to quickly test this
        var sb = new StringBuilder();
        sb.Append("{");
        sb.Append("\"currencies\":{");
        sb.Append("\"currency.coins\":20");
        sb.Append("},");
        sb.Append("\"currencyProperties\":{");
        sb.Append("\"currency.coins\":[");
        sb.Append("{\"name\":\"update\",\"value\":\"");
        sb.Append(timestamp);
        sb.Append("\"}");
        sb.Append("]");
        sb.Append("},");
        sb.Append("\"includeDeltas\":true");
        sb.Append("}");

        var bodyJson = sb.ToString();
        
        var json = await ctx.Requester.Request(
            Method.PUT,
            $"/object/inventory/{userId}",
            bodyJson,
            parser: raw => raw
        );

        return json;
    }
}

RAW INVENTORY UPDATE RESPONSE: {"result":"ok","data":{},"deltas":{"currencies":{"currency.coins":{"before":220,"after":240,"properties":{"update":{"before":"1768413169722","after":"1768413170757"}}}},"items":{"created":[],"updated":[],"deleted":[]}}}

https://github.com/beamable/BeamableBackend/pull/735

New case classes:

case class PropertyDelta(
  before: Option[String],
  after: Option[String]
) extends NetworkSerializable

case class CurrencyDelta(
  before: Long,
  after: Long,
  properties: Option[Map[String, PropertyDelta]] = None
) extends NetworkSerializable

case class ItemPropertiesDelta(
  contentId: String,
  itemId: Long,
  properties: Option[Map[String, PropertyDelta]] = None
) extends NetworkSerializable

case class ItemDeltas(
  created: Seq[ItemPropertiesDelta],
  updated: Seq[ItemPropertiesDelta],
  deleted: Seq[ItemPropertiesDelta]
) extends NetworkSerializable

case class InventoryUpdateDelta(
  currencies: Map[String, CurrencyDelta],
  items: ItemDeltas
) extends NetworkSerializable

case class InventoryUpdateResponse(
  result: String,
  data: Map[String,String] = Map.empty,
  deltas: Option[InventoryUpdateDelta] = None
) extends NetworkSerializable

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions