<commit_before>﻿/**
 * Copyright 2016 d-fens GmbH
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

using System;
using System.Diagnostics.Contracts;
using biz.dfch.CS.Osram.Lightify.Client.Model;
using biz.dfch.CS.Web.Utilities.Rest;
using System.Collections.Generic;

namespace biz.dfch.CS.Osram.Lightify.Client
{
    public partial class Client
    {
        private Uri BaseUri { get; set; }

        internal SessionResponse SessionResponse { get; set; }

        public Client()
        {
            // N/A
        }

        public Client(Uri baseUri)
        {
            Contract.Requires(null != baseUri);
            Contract.Requires(baseUri.IsAbsoluteUri);

            this.BaseUri = baseUri;
        }

        public string GetToken(string userName, string password, string serialNumber)
        {
            Contract.Requires(!string.IsNullOrWhiteSpace(userName));
            Contract.Requires(!string.IsNullOrWhiteSpace(password));
            Contract.Requires(!string.IsNullOrWhiteSpace(serialNumber));
            Contract.Ensures(!string.IsNullOrWhiteSpace(Contract.Result<string>()));

            var result = GetToken(BaseUri, userName, password, serialNumber);
            return result;
        }

        public string GetToken(Uri baseUri, string userName, string password, string serialNumber)
        {
            Contract.Requires(null != baseUri);
            Contract.Requires(baseUri.IsAbsoluteUri);
            Contract.Requires(!string.IsNullOrWhiteSpace(userName));
            Contract.Requires(!string.IsNullOrWhiteSpace(password));
            Contract.Requires(!string.IsNullOrWhiteSpace(serialNumber));
            Contract.Ensures(!string.IsNullOrWhiteSpace(Contract.Result<string>()));

            this.BaseUri = baseUri;

            var sut = new SessionRequest
            {
                Username = userName,
                Password = password,
                SerialNumber = serialNumber
            };

            var body = sut.SerializeObject();
            var client = new RestCallExecutor();
            var requestUri = new Uri(baseUri, Constants.ApiSuffixes.SESSION);
            var result = client.Invoke(HttpMethod.Post, requestUri.AbsoluteUri, null, body);
            Contract.Assert(!string.IsNullOrWhiteSpace(result));

            var response = BaseDto.DeserializeObject<SessionResponse>(result);

            SessionResponse = response;

            return response.SecurityToken;
        }

        internal T Invoke<T>(HttpMethod httpMethod, Uri requestUriSuffix, Dictionary<string, string> headers, string body) 
            where T : BaseDto
        {
            var result = Invoke(httpMethod, requestUriSuffix, headers, body);
            return BaseDto.DeserializeObject<T>(result);
        }

        internal string Invoke(HttpMethod httpMethod, Uri requestUriSuffix, Dictionary<string, string> headers, string body)
        {
            // DFTODO - assert baseUri not null
            // DFTODO - assert username and password present
            // DFTODO - token refresh
<commit_msg>invoke method signature adjusted<commit_after>﻿/**
 * Copyright 2016 d-fens GmbH
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

using System;
using System.Diagnostics.Contracts;
using biz.dfch.CS.Osram.Lightify.Client.Model;
using biz.dfch.CS.Web.Utilities.Rest;
using System.Collections.Generic;

namespace biz.dfch.CS.Osram.Lightify.Client
{
    public partial class Client
    {
        private Uri BaseUri { get; set; }

        internal SessionResponse SessionResponse { get; set; }

        public Client()
        {
            // N/A
        }

        public Client(Uri baseUri)
        {
            Contract.Requires(null != baseUri);
            Contract.Requires(baseUri.IsAbsoluteUri);

            this.BaseUri = baseUri;
        }

        public string GetToken(string userName, string password, string serialNumber)
        {
            Contract.Requires(!string.IsNullOrWhiteSpace(userName));
            Contract.Requires(!string.IsNullOrWhiteSpace(password));
            Contract.Requires(!string.IsNullOrWhiteSpace(serialNumber));
            Contract.Ensures(!string.IsNullOrWhiteSpace(Contract.Result<string>()));

            var result = GetToken(BaseUri, userName, password, serialNumber);
            return result;
        }

        public string GetToken(Uri baseUri, string userName, string password, string serialNumber)
        {
            Contract.Requires(null != baseUri);
            Contract.Requires(baseUri.IsAbsoluteUri);
            Contract.Requires(!string.IsNullOrWhiteSpace(userName));
            Contract.Requires(!string.IsNullOrWhiteSpace(password));
            Contract.Requires(!string.IsNullOrWhiteSpace(serialNumber));
            Contract.Ensures(!string.IsNullOrWhiteSpace(Contract.Result<string>()));

            this.BaseUri = baseUri;

            var sut = new SessionRequest
            {
                Username = userName,
                Password = password,
                SerialNumber = serialNumber
            };

            var body = sut.SerializeObject();
            var client = new RestCallExecutor();
            var requestUri = new Uri(baseUri, Constants.ApiSuffixes.SESSION);
            var result = client.Invoke(HttpMethod.Post, requestUri.AbsoluteUri, null, body);
            Contract.Assert(!string.IsNullOrWhiteSpace(result));

            var response = BaseDto.DeserializeObject<SessionResponse>(result);

            SessionResponse = response;

            return response.SecurityToken;
        }

        internal T Invoke<T>(HttpMethod httpMethod, string requestUriSuffix, Dictionary<string, string> headers, string body) 
            where T : BaseDto
        {
            var result = Invoke(httpMethod, requestUriSuffix, headers, body);
            return BaseDto.DeserializeObject<T>(result);
        }

        internal string Invoke(HttpMethod httpMethod, string requestUriSuffix, Dictionary<string, string> headers, string body)
        {
            // DFTODO - assert baseUri not null
            // DFTODO - assert username and password present
            // DFTODO - token refresh

{'fraction_non_alphanumeric': 0.08733138645529134, 'fraction_numerical': 0.002225003476567932, 'mean_word_length': 2.4811229428848014, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '1208328', 'n_tokens_mistral': 1826, 'n_tokens_neox': 1789, 'n_words': 487}