Пример #1
0
        public static async Task Run([PerperStreamTrigger] PerperStreamContext context,
                                     [Perper("ipfsGateway")] string ipfsGateway,
                                     [PerperStream("dataStream")] IAsyncEnumerable <object> dataStream,
                                     [PerperStream("outputStream")] IAsyncCollector <IHashed <object> > outputStream,
                                     ILogger logger)
        {
            var ipfs = new IpfsClient(ipfsGateway);

            await dataStream.ForEachAsync(async item =>
            {
                try
                {
                    var jToken = IpfsJsonSettings.JTokenFromObject(item);
                    var cid    = await ipfs.Dag.PutAsync(jToken, cancel: CancellationToken.None);
                    var hash   = new Hash {
                        Bytes = cid.ToArray()
                    };

                    logger.LogDebug("Saved {json} as {hash} in ipfs", jToken.ToString(Formatting.None), hash);

                    await outputStream.AddAsync(Hashed.Create(item, hash));
                }
                catch (Exception e)
                {
                    logger.LogError(e.ToString());
                }
            }, CancellationToken.None);
        }
Пример #2
0
        public static async Task Run([PerperStreamTrigger] PerperStreamContext context,
                                     [Perper("ipfsGateway")] string ipfsGateway,
                                     [PerperStream("hashStream")] IAsyncEnumerable <Hash> hashStream,
                                     [PerperStream("outputStream")] IAsyncCollector <IHashed <object> > outputStream,
                                     ILogger logger)
        {
            var ipfs = new IpfsClient(ipfsGateway);

            await hashStream.ForEachAsync(async hash =>
            {
                try
                {
                    // NOTE: Currently blocks other items on the stream and does not process them
                    // -- we should at least timeout

                    var jToken = await ipfs.Dag.GetAsync(Cid.Read(hash.Bytes), CancellationToken.None);
                    var item   = jToken.ToObject <object>(JsonSerializer.Create(IpfsJsonSettings.DefaultSettings));

                    await outputStream.AddAsync(Hashed.Create(item, hash));
                }
                catch (Exception e)
                {
                    logger.LogError(e.ToString());
                }
            }, CancellationToken.None);
        }