<commit_before>﻿using Newtonsoft.Json;
using Qart.Core.DataStore;
using Qart.Testing.Context;
using Qart.Testing.Framework;
using Qart.Testing.Framework.Http;
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;

namespace Qart.Testing.ActionPipeline.Actions.Http
{
    public class HttpAction : IPipelineAction
    {
        private static readonly HttpClient _httpClient = new();

        private readonly Func<TestCase, TestCaseContext, HttpContent[]> _bodyFunc;
        private readonly HttpMethod _httpMethod;
        private readonly string _url;
        private readonly string _targetKey;
        private readonly IHttpRequestMessageProcessor _httpRequestMessageProcessor;
        private readonly bool _multipleResults;

        public HttpAction(HttpMethod httpMethod, string url, string sourceKey = null, string targetKey = null, string mediaType = null, IHttpRequestMessageProcessor httpRequestMessageProcessor = null)
            : this(httpMethod, url, targetKey ?? sourceKey, (testCase, pipelineContext) => new[] { HttpClientExtensions.CreateContent(pipelineContext.TryGetItem(sourceKey, out string item) ? item : null, mediaType) }, httpRequestMessageProcessor, false)
        { }

        public HttpAction(HttpMethod httpMethod, string url, string path, string sourceKey = null, string targetKey = null, string mediaType = null, IHttpRequestMessageProcessor httpRequestMessageProcessor = null)
            : this(httpMethod, url, targetKey ?? sourceKey, (testCase, pipelineContext) => new[] { HttpClientExtensions.CreateContent(testCase.GetContent(path), mediaType) }, httpRequestMessageProcessor, false)
        { }

        public HttpAction(HttpMethod httpMethod, string url, object body, string targetKey = null, string mediaType = null, IHttpRequestMessageProcessor httpRequestMessageProcessor = null)
            : this(httpMethod, url, targetKey, (testCase, pipelineContext) => new[] { HttpClientExtensions.CreateContent(JsonConvert.SerializeObject(body), mediaType) }, httpRequestMessageProcessor, false)<commit_msg>Ability to Http Post json objects from context<commit_after>﻿using Newtonsoft.Json;
using Qart.Core.DataStore;
using Qart.Testing.Context;
using Qart.Testing.Framework;
using Qart.Testing.Framework.Http;
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;

namespace Qart.Testing.ActionPipeline.Actions.Http
{
    public class HttpAction : IPipelineAction
    {
        private static readonly HttpClient _httpClient = new();

        private readonly Func<TestCase, TestCaseContext, HttpContent[]> _bodyFunc;
        private readonly HttpMethod _httpMethod;
        private readonly string _url;
        private readonly string _targetKey;
        private readonly IHttpRequestMessageProcessor _httpRequestMessageProcessor;
        private readonly bool _multipleResults;

        public HttpAction(HttpMethod httpMethod, string url, string sourceKey = null, string targetKey = null, string mediaType = null, IHttpRequestMessageProcessor httpRequestMessageProcessor = null)
            : this(httpMethod, url, targetKey ?? sourceKey, (testCase, pipelineContext) => new[] { HttpClientExtensions.CreateContent(pipelineContext.TryGetItem(sourceKey, out object item) ? item.ToString() : null, mediaType) }, httpRequestMessageProcessor, false)
        { }

        public HttpAction(HttpMethod httpMethod, string url, string path, string sourceKey = null, string targetKey = null, string mediaType = null, IHttpRequestMessageProcessor httpRequestMessageProcessor = null)
            : this(httpMethod, url, targetKey ?? sourceKey, (testCase, pipelineContext) => new[] { HttpClientExtensions.CreateContent(testCase.GetContent(path), mediaType) }, httpRequestMessageProcessor, false)
        { }

        public HttpAction(HttpMethod httpMethod, string url, object body, string targetKey = null, string mediaType = null, IHttpRequestMessageProcessor httpRequestMessageProcessor = null)
            : this(httpMethod, url, targetKey, (testCase, pipelineContext) => new[] { HttpClientExtensions.CreateContent(JsonConvert.SerializeObject(body), mediaType) }, httpRequestMessageProcessor, false)
{'fraction_non_alphanumeric': 0.08025568181818182, 'fraction_numerical': 0.0, 'mean_word_length': 4.959097320169253, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3747734', 'n_tokens_mistral': 1026, 'n_tokens_neox': 1022, 'n_words': 296}