<commit_before>﻿using Newtonsoft.Json;
using ZabbixApi.Helper;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Configuration;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace ZabbixApi
{
    public interface IContext : IDisposable
    {
        T SendRequest<T>(object @params, string method);
    }

    public class Context : IContext
    {
        private readonly string _url;
        private readonly string _user;
        private readonly string _password;

        private string _authenticationToken;

        private WebClient _webClient;

        public Context()
        {
            _url = ConfigurationManager.AppSettings["ZabbixApi.url"];
            _user = ConfigurationManager.AppSettings["ZabbixApi.user"];
            _password = ConfigurationManager.AppSettings["<PASSWORD>"];

            Check.NotEmpty(_url, "ZabbixApi.url");
            Check.NotEmpty(_user, "ZabbixApi.user");
            Check.NotEmpty(_password, "<PASSWORD>");

            _webClient = new WebClient();

            Authenticate();
        }

        private void Authenticate()
        {
            var request = new Request();
            request.method = "user.login";
            request.@params = new Dictionary<string, string>() { { "user", _user }, { "password", _password } };

            var values = new NameValueCollection();
            values.Add("content-type", "application/json-rpc");
            _webClient.Headers.Add(values);

            var responseData = _webClient.UploadData(_url, Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(request)));
            var responseString = Encoding.UTF8.GetString(responseData);

            _authenticationToken = JsonConvert.DeserializeObject<Response<string>>(responseString).result.ToString();
        }

        T IContext.SendRequest<T>(object @params, string method)<commit_msg>Enable construction of Context without config file<commit_after>﻿using Newtonsoft.Json;
using ZabbixApi.Helper;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Configuration;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace ZabbixApi
{
    public interface IContext : IDisposable
    {
        T SendRequest<T>(object @params, string method);
    }

    public class Context : IContext
    {
        private readonly string _url;
        private readonly string _user;
        private readonly string _password;

        private string _authenticationToken;

        private WebClient _webClient;

        public Context()
        {
            _url = ConfigurationManager.AppSettings["ZabbixApi.url"];
            _user = ConfigurationManager.AppSettings["ZabbixApi.user"];
            _password = ConfigurationManager.AppSettings["ZabbixApi.password"];

            Check.NotEmpty(_url, "ZabbixApi.url");
            Check.NotEmpty(_user, "ZabbixApi.user");
            Check.NotEmpty(_password, "<PASSWORD>");

            _webClient = new WebClient();

            Authenticate();
        }
        
        public Context(string url, string user, string password)
        {
            _url = url;
            _user = user;
            _password = password;

            Check.NotEmpty(_url, "ZabbixApi.url");
            Check.NotEmpty(_user, "ZabbixApi.user");
            Check.NotEmpty(_password, "<PASSWORD>");

            _webClient = new WebClient();

            Authenticate();
        }
        
        private void Authenticate()
        {
            var request = new Request();
            request.method = "user.login";
            request.@params = new Dictionary<string, string>() { { "user", _user }, { "password", _password } };

            var values = new NameValueCollection();
            values.Add("content-type", "application/json-rpc");
            _webClient.Headers.Add(values);

            var responseData = _webClient.UploadData(_url, Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(request)));
            var responseString = Encoding.UTF8.GetString(responseData);

            _authenticationToken = JsonConvert.DeserializeObject<Response<string>>(responseString).result.ToString();
        }

        T IContext.SendRequest<T>(object @params, string method)
{'fraction_non_alphanumeric': 0.10236945019553716, 'fraction_numerical': 0.0009201748332183115, 'mean_word_length': 2.575657894736842, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, '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': '739082', 'n_tokens_mistral': 1130, 'n_tokens_neox': 1108, 'n_words': 246}