<commit_before>﻿using Bloomberglp.Blpapi;
using System;
using System.Collections.Generic;
using JetBlack.Bloomberg.Authenticators;
using JetBlack.Bloomberg.Messages;
using JetBlack.Bloomberg.Models;
using JetBlack.Bloomberg.Requesters;
using JetBlack.Promises;

namespace JetBlack.Bloomberg
{
    public class BloombergWrapper
    {
        public event EventHandler<SessionStatusEventArgs> SessionStatus;
        public event EventHandler<AdminStatusEventArgs> AdminStatus;
        public event EventHandler<AuthenticationStatusEventArgs> AuthenticationStatus;

        public Session Session { get; private set; }
        public Service MarketDataService { get; private set; }
        public Service ReferenceDataService { get; private set; }

        public TokenManager TokenManager { get; private set; }
        private readonly ServiceManager _serviceManager = new ServiceManager();
        private readonly SubscriptionManager _subscriptionManager = new SubscriptionManager();
        private readonly ReferenceDataManager _referenceDataManager = new ReferenceDataManager();
        private readonly HistoricalDataManager _historicalDataManager = new HistoricalDataManager();
        private readonly IntradayBarManager _intradayBarManager = new IntradayBarManager();
        private readonly IntraDayTickManager _intraDayTickManager = new IntraDayTickManager();

        public IAuthenticator Authenticator { get; private set; }

        public BloombergWrapper(SessionOptions sessionOptions)
        {
            TokenManager = new TokenManager();
            Session = new Session(sessionOptions, HandleMessage);
        }

        public void Start()
        {
            lock (Session)
            {
                if (!Session.Start())
                    throw new Exception("Failed to start session");

                MarketDataService = OpenService("//blp/mktdata");
                ReferenceDataService = OpenService("//blp/refdata");
            }
        }

        public void StartAsync()
        {
            Session.StartAsync();
        }

        public void AuthenticateAsync(IAuthenticator authenticator)
        {
            Authenticator = authenticator;

            _serviceManager.Request(Session, ServiceUris.AuthenticationService)
                .Then(service => Authenticator.Request(Session, service))
                .Then(isAuthenticated =>
                {
                    RaiseEvent(AuthenticationStatus, new AuthenticationStatusEventArgs(isAuthenticated));
                    return isAuthenticated ? Promise.Resolved() : Promise.Rejected(new ApplicationException("Authentication failed"));
                })
                .ThenAll(() => new[]
                {
                    _serviceManager.Request(Session, ServiceUris.ReferenceDataService).Then(service => Promise.Resolved(() => ReferenceDataService = service)),
                    _serviceManager.Request(Session, ServiceUris.MarketDataService).Then(service => Promise.Resolved(() => MarketDataService = service))
                });
        }


        public void Stop()
        {
            Session.Stop(AbstractSession.StopOption.SYNC);
        }

        public void StopAsync()
        {
            Session.Stop(AbstractSession.StopOption.ASYNC);
        }

        public string GenerateToken()
        {
            return TokenManager.GenerateToken(Session);
        }

        public IPromise<string> RequestToken()
        {<commit_msg>Fixed up for change in promise library<commit_after>﻿using Bloomberglp.Blpapi;
using System;
using System.Collections.Generic;
using JetBlack.Bloomberg.Authenticators;
using JetBlack.Bloomberg.Messages;
using JetBlack.Bloomberg.Models;
using JetBlack.Bloomberg.Requesters;
using JetBlack.Promises;

namespace JetBlack.Bloomberg
{
    public class BloombergWrapper
    {
        public event EventHandler<SessionStatusEventArgs> SessionStatus;
        public event EventHandler<AdminStatusEventArgs> AdminStatus;
        public event EventHandler<AuthenticationStatusEventArgs> AuthenticationStatus;

        public Session Session { get; private set; }
        public Service MarketDataService { get; private set; }
        public Service ReferenceDataService { get; private set; }

        public TokenManager TokenManager { get; private set; }
        private readonly ServiceManager _serviceManager = new ServiceManager();
        private readonly SubscriptionManager _subscriptionManager = new SubscriptionManager();
        private readonly ReferenceDataManager _referenceDataManager = new ReferenceDataManager();
        private readonly HistoricalDataManager _historicalDataManager = new HistoricalDataManager();
        private readonly IntradayBarManager _intradayBarManager = new IntradayBarManager();
        private readonly IntraDayTickManager _intraDayTickManager = new IntraDayTickManager();

        public IAuthenticator Authenticator { get; private set; }

        public BloombergWrapper(SessionOptions sessionOptions)
        {
            TokenManager = new TokenManager();
            Session = new Session(sessionOptions, HandleMessage);
        }

        public void Start()
        {
            lock (Session)
            {
                if (!Session.Start())
                    throw new Exception("Failed to start session");

                MarketDataService = OpenService("//blp/mktdata");
                ReferenceDataService = OpenService("//blp/refdata");
            }
        }

        public void StartAsync()
        {
            Session.StartAsync();
        }

        public void AuthenticateAsync(IAuthenticator authenticator)
        {
            Authenticator = authenticator;

            _serviceManager.Request(Session, ServiceUris.AuthenticationService)
                .Then(service => Authenticator.Request(Session, service))
                .Then(isAuthenticated =>
                {
                    RaiseEvent(AuthenticationStatus, new AuthenticationStatusEventArgs(isAuthenticated));
                    return isAuthenticated ? Promise.Resolved() : Promise.Rejected(new ApplicationException("Authentication failed"));
                })
                .ThenAll(() => new[]
                {
                    _serviceManager.Request(Session, ServiceUris.ReferenceDataService)
                        .Then(service =>
                        {
                            ReferenceDataService = service;
                            return Promise.Resolved();
                        }),
                    _serviceManager.Request(Session, ServiceUris.MarketDataService)
                        .Then(service =>
                        {
                            MarketDataService = service;
                            return Promise.Resolved();
                        })
                });
        }

        public void Stop()
        {
            Session.Stop(AbstractSession.StopOption.SYNC);
        }

        public void StopAsync()
        {
            Session.Stop(AbstractSession.StopOption.ASYNC);
        }

        public string GenerateToken()
        {
            return TokenManager.GenerateToken(Session);
        }

        public IPromise<string> RequestToken()
        {
{'fraction_non_alphanumeric': 0.07356194690265487, 'fraction_numerical': 0.0, 'mean_word_length': 2.2232620320855614, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 23, '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': '4538921', 'n_tokens_mistral': 1654, 'n_tokens_neox': 1574, 'n_words': 368}