/// <inheritdoc />
        public Task <Interaction> StoreAsync(Interaction interaction, CancellationToken cancellationToken = default)
        {
            if (interaction == null)
            {
                throw new ArgumentNullException(nameof(interaction));
            }

            try
            {
                var archive          = new HttpArchive(interaction);
                var archiveDirectory = Path.GetDirectoryName(GetFilePath(interaction.Name));
                if (!string.IsNullOrWhiteSpace(archiveDirectory) && !Directory.Exists(archiveDirectory))
                {
                    Directory.CreateDirectory(archiveDirectory);
                }

                File.WriteAllText(GetFilePath(interaction.Name), JsonSerializer.Serialize(archive, _jsonOptions));

                return(Task.FromResult(archive.ToInteraction(interaction.Name)));
            }
            catch (Exception ex) when((ex is IOException) || (ex is JsonException))
            {
                throw new HttpRecorderException($"Error while writing file {GetFilePath(interaction.Name)}: {ex.Message}", ex);
            }
        }
Esempio n. 2
0
        /// <inheritdoc />
        public Task StoreAsync(Interaction interaction, CancellationToken cancellationToken = default)
        {
            try
            {
                var archive          = new HttpArchive(interaction);
                var archiveDirectory = Path.GetDirectoryName(GetFilePath(interaction.Name));
                if (!string.IsNullOrWhiteSpace(archiveDirectory) && !Directory.Exists(archiveDirectory))
                {
                    Directory.CreateDirectory(archiveDirectory);
                }

                File.WriteAllText(GetFilePath(interaction.Name), JsonConvert.SerializeObject(archive, Formatting.Indented, _jsonSettings));

                return(Task.CompletedTask);
            }
            catch (Exception ex)
            {
                throw new HttpRecorderException($"Error while writing file {GetFilePath(interaction.Name)}: {ex.Message}", ex);
            }
        }